主旨思想:数据库(增,删,改,查)
资源管理器目的:实现基本人员信息 存储,调用,查看头像,修改内容。
注意事项: 1.建立两个表格 (人员表(cold,name,bumen,phone,topphone),头像表images(头像表两列cold,picture))
2.单独建两个类 ,存放列名的封装,放法(增,删,改,查)
3.listview 呈现表结够
//第一个表 using System;using System.Collections.Generic;using System.Text;namespace 晨启电子考勤系统.Cold{ public class useDATA { private string Name; public string Name1 { get { return Name; } set { Name = value; } } private string ID; public string ID1 { get { return ID; } set { ID = value; } } private string Chejian; public string Chejian1 { get { return Chejian; } set { Chejian = value; } } private string Topphone; public string Topphone1 { get { return Topphone; } set { Topphone = value; } } private string Phone; public string Phone1 { get { return Phone; } set { Phone = value; } } private string qq; public string Qq { get { return qq; } set { qq = value; } } }}
第二个比表可以直接调用方法
public class UserDA { private SqlConnection _Conn; private SqlCommand _Cmd; public UserDA() { _Conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); } // 添加方法 public void Insert(useDATA data) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "insert into Chenqidianzi values (@Cold,@Name,@Phone,@Chejian,@Topphone)"; _Cmd.Parameters.Add("@Cold", data.ID1); _Cmd.Parameters.Add("@Name", data.Name1); _Cmd.Parameters.Add("@Phone", data.Phone1); _Cmd.Parameters.Add("@Chejian", data.Chejian1); _Cmd.Parameters.Add("@Topphone", data.Topphone1); _Cmd.ExecuteNonQuery(); _Conn.Close(); } // 查询方法 点击查询 所有的员工信息 public ListSelect() { List list = new List (); _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "select *from Chenqidianzi"; SqlDataReader dr = _Cmd.ExecuteReader(); while (dr.Read()) { useDATA data = new useDATA(); data.ID1 = dr["Cold"].ToString(); data.Name1 = dr["Name"].ToString(); data.Phone1 = dr["Phone"].ToString(); data.Chejian1 = dr["Chejian"].ToString(); data.Topphone1 = dr["Topphone"].ToString(); list.Add(data); } return list; } /// /// 根据用户名查 /// ///public List Select(string name) { List list = new List (); _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "select *from Chenqidianzi where uname like '%" + name + "%'"; SqlDataReader dr = _Cmd.ExecuteReader(); while (dr.Read()) { useDATA data = new useDATA(); data.ID1 = dr["Cold"].ToString(); data.Name1 = dr["Name"].ToString(); data.Phone1 = dr["Phone"].ToString(); data.Chejian1 = dr["Chejian"].ToString(); data.Topphone1 = dr["Topphone"].ToString(); list.Add(data); } return list; } public void Delete(string code) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "delete from Chenqidianzi where Cold=" + code; //_Cmd.CommandText = "delete from images where cc=" + code; _Cmd.ExecuteNonQuery(); _Conn.Close(); } public void update(useDATA data) { _Conn.Open();//@Name,@Cold,@Phone,@Chejian,@Topphone _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "update Chenqidianzi set Name=@Name,Phone=@Phone,Chejian=@Chejian,Topphone=@Topphone where cold=@cold"; _Cmd.Parameters.Clear(); _Cmd.Parameters.Add("@Cold", data.ID1); _Cmd.Parameters.Add("@Name", data.Name1); _Cmd.Parameters.Add("@Phone", data.Phone1); _Cmd.Parameters.Add("@Chejian", data.Chejian1); _Cmd.Parameters.Add("@Topphone", data.Topphone1); _Cmd.ExecuteNonQuery(); _Conn.Close(); } public void pic(string code) { _Conn.Open(); _Cmd = _Conn.CreateCommand(); _Cmd.CommandText = "delete from images where cc=" +code; _Cmd.ExecuteNonQuery(); _Conn.Close(); } //public void appare(string cc) //{ // _Conn.Open(); // _Cmd = _Conn.CreateCommand(); // SqlConnection conn = new SqlConnection("server=.;database=images;user=sa;pwd=123"); // SqlCommand cmd = conn.CreateCommand(); // cmd.CommandText = "select *from imgtable where cc=" + cc; // SqlDataReader dr = cmd.ExecuteReader(); // dr.Read(); // byte[] buffer = (byte[])dr["qq"]; // cmd.Dispose(); // conn.Close(); //} } }
主界面 删除直接在 本界面进行
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using 晨启电子考勤系统.Cold;using WindowsFormsApplication7.App_Code;using System.IO;namespace 晨启电子考勤系统{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void label1_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void panel3_Paint(object sender, PaintEventArgs e) { } SqlConnection conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); public void Binddata(Listlist) { //第一步:构建listview"' listView1.Columns.Clear(); listView1.Columns.Add("员工编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("联系电话"); listView1.Columns.Add("所在部门"); listView1.Columns.Add("领导电话"); listView1.Items.Clear(); //读取数据,通过数据构建listview中的行 foreach (useDATA data in list) { ListViewItem lt = new ListViewItem(); //lt.Text = data.ToString(); lt.SubItems.Add(data.Name1); lt.SubItems.Add(data.ID1); lt.SubItems.Add(data.Phone1); lt.SubItems.Add(data.Chejian1); lt.SubItems.Add(data.Topphone1); listView1.Items.Add(lt); } } private void button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); if (cname.Text == "" && cid.Text == "") { cmd.CommandText = "select *from Chenqidianzi"; //cmd.Parameters.Add("@id", "%" + cid.Text + "%"); SqlDataReader rd = cmd.ExecuteReader(); //开始构建listview表头 listView1.Columns.Clear(); listView1.Columns.Add("员工编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("联系方式"); listView1.Columns.Add("所在部门"); listView1.Columns.Add("领导电话"); //读取数据库,根据数据库内容往listview中添加行数据 listView1.Items.Clear(); while (rd.Read()) { //构建一行listview 的 ListViewItem lt = new ListViewItem(); lt.Text = rd["Cold"].ToString(); lt.SubItems.Add(rd["Name"].ToString()); lt.SubItems.Add(rd["Phone"].ToString()); lt.SubItems.Add(rd["Chejian"].ToString()); lt.SubItems.Add(rd["Topphone"].ToString()); listView1.Items.Add(lt); } conn.Close(); } if (cname.Text != "" && cid.Text == "") { cmd.CommandText = "select *from Chenqidianzi where name like @name"; cmd.Parameters.Add("@name", "%" + cname.Text + "%"); SqlDataReader rd = cmd.ExecuteReader(); //开始构建listview表头 listView1.Columns.Clear(); listView1.Columns.Add("员工编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("联系方式"); listView1.Columns.Add("所在部门"); listView1.Columns.Add("领导电话"); //读取数据库,根据数据库内容往listview中添加行数据 listView1.Items.Clear(); while (rd.Read()) { //构建一行listview 的 ListViewItem lt = new ListViewItem(); lt.Text = rd["Cold"].ToString(); lt.SubItems.Add(rd["Name"].ToString()); lt.SubItems.Add(rd["Phone"].ToString()); lt.SubItems.Add(rd["Chejian"].ToString()); lt.SubItems.Add(rd["Topphone"].ToString()); listView1.Items.Add(lt); } //else // { // cmd.CommandText = "select *from Chenqidianzi where cold like @id"; // cmd.Parameters.Add("@id", "%" + cid.Text + "%"); // SqlDataReader rd = cmd.ExecuteReader(); // //开始构建listview表头 // listView1.Columns.Clear(); // listView1.Columns.Add("姓名"); // listView1.Columns.Add("员工编号"); // listView1.Columns.Add("联系方式"); // listView1.Columns.Add("所在车间"); // listView1.Columns.Add("领导电话"); // //读取数据库,根据数据库内容往listview中添加行数据 // listView1.Items.Clear(); // while (rd.Read()) // { //构建一行listview 的 // ListViewItem lt = new ListViewItem(); // lt.Text = rd["Name"].ToString(); // lt.SubItems.Add(rd["Cold"].ToString()); // lt.SubItems.Add(rd["Phone"].ToString()); // lt.SubItems.Add(rd["Chejian"].ToString()); // lt.SubItems.Add(rd["Topphone"].ToString()); // listView1.Items.Add(lt); // } // conn.Close(); ////} } if (cname.Text == "" && cid.Text != "") { cmd.CommandText = "select *from Chenqidianzi where cold like @cold"; cmd.Parameters.Add("@cold", "%" + cid.Text + "%"); SqlDataReader rd = cmd.ExecuteReader(); //开始构建listview表头 listView1.Columns.Clear(); listView1.Columns.Add("员工编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("联系方式"); listView1.Columns.Add("部门"); listView1.Columns.Add("领导电话"); //读取数据库,根据数据库内容往listview中添加行数据 listView1.Items.Clear(); while (rd.Read()) { //构建一行listview 的 ListViewItem lt = new ListViewItem(); lt.Text = rd["Cold"].ToString(); lt.SubItems.Add(rd["Name"].ToString()); lt.SubItems.Add(rd["Phone"].ToString()); lt.SubItems.Add(rd["Chejian"].ToString()); lt.SubItems.Add(rd["Topphone"].ToString()); listView1.Items.Add(lt); } } } private void button4_MouseClick(object sender, MouseEventArgs e) { insert it = new insert(); it.Show(); } private void button4_Click(object sender, EventArgs e) { } private void button4_Leave(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { string code = listView1.SelectedItems[0].Text; new UserDA().Delete(code); new UserDA().pic(code);//方法调用 SqlConnection conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); conn.Open(); SqlCommand cmd = conn.CreateCommand(); if (cname.Text == "" && cid.Text == "") { cmd.CommandText = "select *from Chenqidianzi"; //cmd.Parameters.Add("@id", "%" + cid.Text + "%"); SqlDataReader rd = cmd.ExecuteReader(); //开始构建listview表头 listView1.Columns.Clear(); listView1.Columns.Add("员工编号"); listView1.Columns.Add("姓名"); listView1.Columns.Add("联系方式"); listView1.Columns.Add("所在部门"); listView1.Columns.Add("领导电话"); //读取数据库,根据数据库内容往listview中添加行数据 listView1.Items.Clear(); while (rd.Read()) { //构建一行listview 的 ListViewItem lt = new ListViewItem(); lt.Text = rd["Cold"].ToString(); lt.SubItems.Add(rd["Name"].ToString()); lt.SubItems.Add(rd["Phone"].ToString()); lt.SubItems.Add(rd["Chejian"].ToString()); lt.SubItems.Add(rd["Topphone"].ToString()); listView1.Items.Add(lt); } conn.Close(); } } } private void button2_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { useDATA data = new useDATA(); data.ID1 = listView1.SelectedItems[0].SubItems[0].Text; data.Name1 = listView1.SelectedItems[0].SubItems[1].Text; data.Phone1 = listView1.SelectedItems[0].SubItems[2].Text; data.Chejian1 = listView1.SelectedItems[0].SubItems[3].Text; data.Topphone1 = listView1.SelectedItems[0].SubItems[4].Text; Update f = new Update(data); f.Show(); } else { MessageBox.Show("请选择,再修改"); } } private void Form1_Load(object sender, EventArgs e) { } private void button3_Leave(object sender, EventArgs e) { } private void label3_Click(object sender, EventArgs e) { } private void button5_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { //判断是否选中listview 行数据 string code = listView1.SelectedItems[0].Text; SqlConnection conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand(); //根据 code查询 images 数据表里面的 qq列数据 cmd.CommandText = "select qq from images where cc=" + code; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); dr.Read(); //将“qq”放入数组里 byte[] buffer = (byte[])dr["qq"]; cmd.Dispose(); conn.Close(); //将二进制数据buffer显示为图片 MemoryStream ms = new MemoryStream(buffer);//构建对象 ms.Write(buffer, 0, buffer.Length);//写到内存流中 Image img = System.Drawing.Image.FromStream(ms); pictureBox2.Image = img; } else { MessageBox.Show("请您选中后再查看!!"); //pictureBox2.Image = ""; } } }}
添加界面 insert
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Text;using System.Windows.Forms;using WindowsFormsApplication7.App_Code;using 晨启电子考勤系统.Cold;using System.Data.SqlClient;namespace 晨启电子考勤系统{ public partial class insert : Form { public insert() { InitializeComponent(); } private void label4_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { useDATA data = new useDATA(); if (Iname.Text != "" && icode.Text != "" && Iphone.Text != "" && Ichejian.Text != "" && Itopphone.Text != "") { data.Name1 = Iname.Text; data.ID1 = icode.Text; data.Phone1 = Iphone.Text; data.Chejian1 = Ichejian.Text; data.Topphone1 = Itopphone.Text; //new 连接().insert(data); new UserDA().Insert(data); openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*"; DialogResult isok = openFileDialog1.ShowDialog(); if (isok == DialogResult.OK) { //开始使用流读取 FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); //使用流读取器,把文件流对象中的内容读取出来,转换成字符串或者其他对应的数据 BinaryReader br = new BinaryReader(fs);//二进制读取器 byte[] by = br.ReadBytes((int)fs.Length);//将流中数据读取成byte数组存入数组变量中 //连接数据库,新增数据 SqlConnection conn = new SqlConnection("server=.;database=qq;user=sa;pwd=123"); SqlCommand cmd = conn.CreateCommand();//cc值跟cold的值对应 cmd.CommandText = "insert into images values(@cc,@qq)"; cmd.Parameters.Clear(); cmd.Parameters.Add("@cc",data.ID1); cmd.Parameters.Add("@qq", by); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*"; //DialogResult qq = openFileDialog1.ShowDialog(); if (isok == DialogResult.OK) { Image img = Image.FromFile(openFileDialog1.FileName); pictureBox2.Image = img; } MessageBox.Show("录入成功"); } else { MessageBox.Show("请输入完整的信息 "); } } } private void button2_Click_1(object sender, EventArgs e) { Iname.Text = ""; icode.Text = ""; Iphone.Text = ""; Ichejian.Text = ""; Itopphone.Text = ""; } private void insert_Load(object sender, EventArgs e) { } }}
添加界面 两个表格 一个存入基本信息 一个存图片
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using 晨启电子考勤系统.Cold;using WindowsFormsApplication7.App_Code;namespace 晨启电子考勤系统{ public partial class Update : Form { public Update() { InitializeComponent(); } public Update(useDATA data) { InitializeComponent(); Uname.Text = data.Name1; Uid.Text = data.ID1; Uphone.Text = data.Phone1; Uchejian.Text = data.Chejian1; Utopphone.Text = data.Topphone1; } private void Update_Load(object sender, EventArgs e) { } private void 确认修改_Click(object sender, EventArgs e) { useDATA data = new useDATA(); data.ID1 = Uid.Text; data.Name1 = Uname.Text; data.Phone1 = Uphone.Text; data.Chejian1 = Uchejian.Text; data.Topphone1 = Utopphone.Text; new UserDA().update(data); } private void panel1_Paint(object sender, PaintEventArgs e) { } }}
修改界面 员工编号 不能修改 cold 是调用images 和 人员基本信息表 的判断健
主界面 查询 用到模糊查询 等