C#本地数据库连接工资管理
所用工具:
- SqlServer2008R2
- vs2015
一些功能:
- * 用户登陆*
- * dataGridView数据填充*
- * 数据库的增删改查*
- * 页面数据传递*
- * 数据库数据填充TextBox(不能写成类来调用方法,我也不知道为什么>_<,希望大家跟我讲讲)*
- * 结束当前界面,唤醒隐藏页面(不然会后来会内存爆炸,有很多跟我一样的刚刚学这门课一样,总是新开启页面,隐藏上一个页面,这个姿势是会出事的,会怀孕的)*
步骤:
- SqlServer需要开启的服务
- 配置
App.config
文件 - vs2015
我将数据库分离出来了,以及建表的命令,还有SalaryManagement工程挂载在GitHub上了,需要源码的可以自己去Fork, https://github.com/xiaheshun/SalaryManagement.git
1.开启服务
有需要软件的,我已经放到自己的百度云盘里,网络不好的同学可以自己下载【百度云链接】
2.配置 App.config
文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<add name ="XiaHeShun" connectionString="server=EYRE-PC;database=db_user;integrated security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
name看自己心情写就行了,后面会用到,server=EYRE-PC,这里是你本地数据库的server名称;database=db_user是你建立的数据库名称
3.建立DBConect.cs类
该类中只写了增、删、改、查和登陆验证
注意:后面有一个搜索用户界面的操作里,由于需要填充TextBox,无法写到此类中,直接写到窗口类的下面了(中间我也不知道为什么,反正写这里里面就是错的,下面我会提到)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;
namespace SalaryManagment
{
public class DBConnect
{
string strcon = ConfigurationManager.ConnectionStrings["XiaHeShun"].ConnectionString.ToString().Trim();
public DBConnect()
{
}
public Boolean create(Employee emp)
{
string sql = string.Format("insert into [db_User](Uid,Uname,Usex,Uwork,Ubonus,Usalary,UrealSalary,Uhobby,Uimage) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", emp.Id,emp.Name,emp.Sex,emp.Work,emp.Bonus.ToString(),emp.Salary.ToString(),emp.RealSalary.ToString(),emp.Hobby,emp.Image);
using (SqlConnection con = new SqlConnection(this.strcon))
{
SqlCommand cmd = new SqlCommand(sql,con);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
return true;
else
return false;
}
}
public Boolean delete(string id)
{
string sql = string.Format("delete from [db_User] where Uid='{0}'", id);
using (SqlConnection con = new SqlConnection(this.strcon))
{
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
return true;
else
return false;
}
}
public Boolean updata(string id ,string name ,string salary,string bonus )
{
string realSalary = new Employee().ReckonRealSalary(double.Parse(salary), double.Parse(bonus)).ToString();
string sql = string.Format("update [db_User] set Uname='{0}',Usalary='{1}',Ubonus='{2}',UrealSalary='{3}' where Uid='{4}'",name,salary,bonus,realSalary,id);
using (SqlConnection con = new SqlConnection(this.strcon))
{
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
int result = cmd.ExecuteNonQuery();
con.Close();
if (result > 0)
return true;
else
return false;
}
}
public DataTable find()
{
string sql = string.Format("select * from [db_User]");
using (SqlConnection con = new SqlConnection(this.strcon))
{
DataSet ds = new DataSet();
con.Open();
SqlDataAdapter cmd = new SqlDataAdapter(sql, con);
cmd.Fill(ds);
con.Close();
return ds.Tables[0];
}
}
public Boolean login(string name,string pwd)
{
string sql = string.Format("select * from [db_Manager] where Mid='{0}' and Mpwd='{1}'", name,pwd);
using (SqlConnection con = new SqlConnection(this.strcon))
{
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
object result = cmd.ExecuteScalar();
con.Close();
if (result != null)
return true;
else
return false;
}
}
}
}
4.数据库数据填充TextBox
在这里,自己看代码学习哦~
private void Btn_search_Click(object sender, EventArgs e){}
(不能写成类来调用方法,我也不知道为什么>_<,希望大家跟我讲讲)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SalaryManagment
{
public partial class UpdataEmployeeForm : Form
{
string strcon = ConfigurationManager.ConnectionStrings["XiaHeShun"].ConnectionString.ToString().Trim();
public UpdataEmployeeForm()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Btn_back_Click(object sender, EventArgs e)
{
MainForm.f0.Show();
MainForm.f0.update();
MainForm.f0.Show();
this.Close();
}
private void Btn_search_Click(object sender, EventArgs e)
{
string id = this.UEF_id.Text;
string sql = string.Format("select * from [db_User] where Uid='{0}'", id);
using (SqlConnection con = new SqlConnection(this.strcon))
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.HasRows)
{
sdr.Read();
this.UEF_name.Text = sdr["Uname"].ToString();
this.UEF_salary.Text = sdr["Usalary"].ToString();
this.UEF_bonus.Text = sdr["Ubonus"].ToString();
MessageBox.Show("查询成功!", "提示");
}
else
{
MessageBox.Show("查无此人!", "提示");
}
sdr.Close();
con.Close();
}
}
private void UpdataEmployeeForm_Load(object sender, EventArgs e)
{
}
private void Btn_correct_Click(object sender, EventArgs e)
{
DBConnect dbc = new DBConnect();
string id = this.UEF_id.Text;
string name = this.UEF_name.Text;
string salary = this.UEF_salary.Text;
string bonus = this.UEF_bonus.Text;
if (dbc.updata(id, name, salary, bonus) == true)
{
MessageBox.Show("更改成功!", "提示");
}
else
{
MessageBox.Show("更改失败!", "提示");
}
}
private void Btn_delete_Click(object sender, EventArgs e)
{
DBConnect dbc = new DBConnect();
string id = this.UEF_id.Text;
if (dbc.delete(id) == true)
{
MessageBox.Show("删除成功!", "提示");
}
else
{
MessageBox.Show("删除失败!", "提示");
}
}
}
}
5.dataGridView数据填充
先配置一下你的dataGridView属性-集合
然后调用DBConnect类中的find()方法就行了
private void MainForm_Load(object sender, EventArgs e) {}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SalaryManagment
{
public partial class MainForm : Form
{
public static MainForm f0 = null; //用来引用主窗口
public string LoginName="";
public MainForm()
{
InitializeComponent();
this.update();
f0 = this;
}
private void 帮助hToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void fdfToolStripMenuItem_Click(object sender, EventArgs e)
{
AddEmployeeForm add = new AddEmployeeForm();
this.Hide();
add.Show();
}
private void MainForm_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = new DBConnect().find();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
public void update()
{
this.toolStripStatusLabel1.Text = "时间状态: " + DateTime.Now.ToLocalTime().ToString() + " 当前用户: "+this.LoginName;
this.dataGridView1.DataSource = new DBConnect().find();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.update();
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)
{
}
private void 更改员工信息ToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdataEmployeeForm updata = new UpdataEmployeeForm();
this.Hide();
updata.Show();
}
}
}
5.页面数据传递和结束当前窗口唤醒之前的窗口
然后在你需要传递到的页面(比如我的是MainForm窗口)
你需要在这个界面开启的时候,也就是生成此对象,就是对象引用的意思,我的语文不好,感觉说的不是很清楚,见谅呀(其实是自己面对对象没有学好哈哈哈哈emmmm*_*)
打个比方,现有窗口A和窗口B
5.1 窗口B要将数据传到窗口A
//我是A类中一段代码
public static MainForm f0 = null; //用来引用主窗口
public string strReceive="";//接收字符串变量
public MainForm()
{
InitializeComponent();
f0 = this;
}
//我是B类中的一段代码
private void Main_Login_MouseClick(object sender, MouseEventArgs e)
{
string strSend= "testData";
A a= new A();
a.strReceive= strSend;
this.Hide();
a.Show();
}
5.2 结束当前B窗口唤醒之前的A窗口
//我是A类中一段代码
public static MainForm f0 = null; //用来引用主窗口
public MainForm()
{
InitializeComponent();
f0 = this;
}
//我是B类中的一段代码
private void Main_Login_MouseClick(object sender, MouseEventArgs e)
{
A.f0.Show();
this.Close();
}
具体的源码请进我的GitHub里面,自己下载看源码,这里不贴了,有点饿了,快饿死了,写下去,我就gg了 ^_^
需要源码的可以自己去Fork(其实也可以下载啦,我这么辛苦,fork一下啦), https://github.com/xiaheshun/SalaryManagement.git **