Fork me on GitHub

简单员工管理实例

数据库设计视图:

窗体界面设计:

实例展示:

全部代码(含注释,本例只是练手,有很多功能可以去扩展,比如说将数据导入、导出等)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Data.SqlClient;
  6 using System.Drawing;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Windows.Forms;
 10 
 11 namespace StaffManagement
 12 {
 13     public partial class Form1 : Form
 14     {
 15         //公有字段
 16         //public static string str = "";
 17         //此处定义静态数据库连接字符串,方便后面调用
 18         public static string strConn = "Server=(local);Database=db_Staff;Integrated Security=true";
 19         public Form1()
 20         {
 21             InitializeComponent();
 22         }
 23 
 24         private void Form1_Load(object sender, EventArgs e)
 25         {
 26             //启动窗体时自动调用Showinf()方法
 27             Showinf();
 28         }
 29         //及时将数据返回到DataGridView窗口
 30         private void Showinf()
 31         {
 32             using (SqlConnection con = new SqlConnection(strConn))
 33             {
 34                 DataTable dt = new DataTable();
 35                 //使用SqlDataAdapter提交查询命令
 36                 SqlDataAdapter da = new SqlDataAdapter("select * from 员工表 order by 员工编号", con);
 37                 //将查询结果数据集提交到数据库中
 38                 da.Fill(dt);
 39                 //同时将数据在DataGridView中更新
 40                 this.dataGridView1.DataSource = dt.DefaultView;
 41             }
 42         }
 43         /// <summary>
 44         /// 在控件中填充选中的DataGridView控件的数据
 45         /// </summary>
 46         //private void FillControls()
 47         //{
 48         //    try
 49         //    {
 50         //        this.txtNo.Text = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
 51         //        this.txtName.Text = this.dataGridView1[1, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
 52         //        this.txtSalary.Text=this.dataGridView1[2,this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
 53         //        this.txtEstimate.Text=this.dataGridView1[3,this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
 54         //    }
 55         //    catch { }
 56         //}
 57         private void ClearTxt()
 58         {
 59             this.txtNo.Text = null;
 60             this.txtName.Text = null;
 61             this.txtSalary.Text = null;
 62             this.txtEstimate.Text = null;
 63         }
 64         /// <summary>
 65         /// 添加记录
 66         /// </summary>
 67         /// <param name="sender"></param>
 68         /// <param name="e"></param>
 69         private void btnAdd_Click(object sender, EventArgs e)
 70         {
 71             if (this.txtNo.Text == "")
 72             {
 73                 MessageBox.Show("员工编号不能为空!");
 74                 return;
 75             }
 76             if (this.txtName.Text == "")
 77             {
 78                 MessageBox.Show("员工姓名不能为空!");
 79                 return;
 80             }
 81             if (this.txtSalary.Text == "")
 82             {
 83                 MessageBox.Show("基本工资不能为空!");
 84             }
 85             if (this.txtEstimate.Text == "")
 86             {
 87                 MessageBox.Show("员工评价不能为空!");
 88             }
 89             using (SqlConnection con = new SqlConnection(strConn))
 90             {
 91                 //首先判断连接是否存在
 92                 if (con.State == ConnectionState.Closed)
 93                 {
 94                     con.Open();
 95                 }
 96                 try
 97                 {
 98                     StringBuilder strSQL = new StringBuilder();
 99                     strSQL.Append("insert into 员工表(员工编号,员工姓名,基本工资,工作评价)");
100                     strSQL.Append("values('"+this.txtNo.Text.Trim().ToString()+"','"+
101                         this.txtName.Text.Trim().ToString()+"',");
102                     strSQL.Append("'" + Convert.ToSingle(this.txtSalary.Text.Trim().ToString())+"','"+
103                         this.txtEstimate.Text.Trim().ToString()+"')");
104 
105                     using (SqlCommand cmd = new SqlCommand(strSQL.ToString(), con))
106                     {
107                         cmd.ExecuteNonQuery();
108                         MessageBox.Show("信息增加成功!");
109                     }
110                     //清空插入的代码
111                    // strSQL.Remove(0,strSQL.Length);
112                 }
113                 catch(Exception ex)
114                 {
115                     MessageBox.Show("错误:"+ex.Message,"错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
116                 }
117                 //不论是否发生异常都会执行finally中的语句,关闭连接。
118                 finally
119                 { 
120                     if(con.State==ConnectionState.Open)
121                     {
122                         con.Close();
123                         con.Dispose();
124                     }
125                     Showinf();
126                     ClearTxt();
127                 }
128             }
129         }
130 
131         private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
132         {
133             //str = this.dataGridView1.SelectedCells[0].Value.ToString();
134         }
135         /// <summary>
136         /// 更新操作
137         /// </summary>
138         /// <param name="sender"></param>
139         /// <param name="e"></param>
140         private void btnUpdate_Click(object sender, EventArgs e)
141         {
142             using(SqlConnection con=new SqlConnection(strConn))
143             {
144                 if (this.txtNo.Text.ToString() != null)
145                 {
146                     string str_condition = "";
147                     string str_cmdtxt = "";
148                     //记录员工编号
149                     str_condition = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();
150                     str_cmdtxt = "update 员工表 set 员工姓名='" + this.txtName.Text.Trim() + "'," + "基本工资=" +
151                        Convert.ToSingle(this.txtSalary.Text.Trim()) + "," + "工作评价='" + this.txtEstimate.Text.Trim() +
152                        "'  where 员工编号='" + str_condition + "'";
153                     try
154                     {
155                         if (con.State == ConnectionState.Closed)
156                         {
157                             con.Open();
158                         }
159                         using (SqlCommand cmd = new SqlCommand(str_cmdtxt, con))
160                         {
161                             cmd.ExecuteNonQuery();
162                             MessageBox.Show("数据修改成功!");
163                         }
164                     }
165                     catch (Exception ex)
166                     {
167                         MessageBox.Show("错误:" + ex.Message, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
168                     }
169                     finally
170                     {
171                         if (con.State == ConnectionState.Open)
172                         {
173                             con.Close();
174                             con.Dispose();
175                         }
176                     }
177                     Showinf();
178                     ClearTxt();
179                 }
180                 else
181                 {
182                     MessageBox.Show("员工编号为空啊!","提示对话框",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
183                 }
184             }
185         }
186         /// <summary>
187         /// 删除记录
188         /// </summary>
189         /// <param name="sender"></param>
190         /// <param name="e"></param>
191         private void btnDelete_Click(object sender, EventArgs e)
192         {
193             if (this.txtNo.Text == "")
194             {
195                 MessageBox.Show("请输入员工编号!");
196             }
197             else
198             {
199                 if (MessageBox.Show("你确定要删除吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
200                 {
201                     using (SqlConnection con = new SqlConnection(strConn))
202                     {
203                         con.Open();
204                         string str_cmdtxt = "delete from 员工表 where 员工编号= '" + this.txtNo.Text.Trim() + "'";
205                         SqlCommand cmd = new SqlCommand(str_cmdtxt, con);
206                         cmd.ExecuteNonQuery();
207                         con.Close();
208                         Showinf();
209                         MessageBox.Show("删除成功!");
210                     }
211                     ClearTxt();
212                 }
213                 else
214                 {
215                     ClearTxt();
216                 }
217             }
218         }
219         /// <summary>
220         /// 清空文本框
221         /// </summary>
222         /// <param name="sender"></param>
223         /// <param name="e"></param>
224         private void btnClear_Click(object sender, EventArgs e)
225         {
226             ClearTxt();
227         }
228         /// <summary>
229         /// 退出操作
230         /// </summary>
231         /// <param name="sender"></param>
232         /// <param name="e"></param>
233         private void btnExit_Click(object sender, EventArgs e)
234         {
235             Application.Exit();
236         }
237 
238         private void btnFind_Click(object sender, EventArgs e)
239         {
240             //调用清空文本的方法
241            // ClearTxt();
242             using(SqlConnection con=new SqlConnection(strConn))
243             {
244                 con.Open();
245                 string str_cmdtxt = "select * from 员工表 where 员工编号='" + this.txtNo.Text.Trim() + "'";
246                 SqlCommand cmd = new SqlCommand(str_cmdtxt,con);
247                 SqlDataReader dr = cmd.ExecuteReader();
248                 while(dr.Read())
249                 {
250                     this.txtName.Text = dr["员工姓名"].ToString();
251                     this.txtSalary.Text = dr["基本工资"].ToString();
252                     this.txtEstimate.Text = dr["工作评价"].ToString();
253                     //txtName.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[1].Value.ToString();
254                     //txtSalary.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString();
255                     //txtEstimate.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[3].Value.ToString();
256                 //txtNo.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0].Value.ToString();
257                 }
258                 if (con.State ==ConnectionState.Open)
259                 {
260                     con.Close();
261                     con.Dispose();
262                 }
263             }
264 
265         }
266     }
267 }

 

posted @ 2013-03-13 11:10  种花生的读书人  阅读(744)  评论(0编辑  收藏  举报

该博客仅作为记录笔记,转载随意