C#图片存入数据库并读取显示在picturebox中_第1页
C#图片存入数据库并读取显示在picturebox中_第2页
C#图片存入数据库并读取显示在picturebox中_第3页
C#图片存入数据库并读取显示在picturebox中_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实现功能:把图片及人员信息存入数据库并可通过编号查找显示在picturebox中,同时实现照片及信息的更新。数据库构建:PhotoID int 4;EmployeeNO Varchar 20;EmployeeName Varchar 50 ;PhotoContent image 16;Ramrk text 16点击“选择图片”打开一个对话框,选择一个图片放到picturebox中预览private void button1_Click(object sender, EventArgs e) using (OpenFileDialog ofd = new OpenFileDialog() ofd

2、.Title = "请选择要插入的图片" ofd.Filter = "*jpg|*.JPG|*.GIF|*.GIF|*.BMP|*.BMP" ofd.CheckFileExists = true; ofd.CheckPathExists = true; ofd.Multiselect = false; if (ofd.ShowDialog(this) = DialogResult.OK) txtfile = ofd.FileName; this.pb.ImageLocation = ofd.FileName; 点击“按钮”,调用update方法,以实现信

3、息及照片的存储和更改 private void btSave_Click(object sender, EventArgs e) if (txtfile = "") HrBll.AppFrame.BllPhoto.update(txtRemark.Text,txtEmpNO .Text); else Update(txtfile, txtRemark.Text, txtEmpNO.Text); Empty(); Update方法的代码Private void Update(string fullpath,string remark,string empno) FileStr

4、eam fs = new FileStream(fullpath, FileMode.Open); byte imagebytes = new bytefs.Length; BinaryReader br = new BinaryReader(fs); imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length); string SQLstr = string.Format("update T_Photo set PhotoContent=photo,Remark=remark where EmployeeNO=empno");

5、SqlCommand com = new SqlCommand(SQLstr, Common.DataAccess.GetConn(); com.Parameters.Add("photo", SqlDbType.Image); com.Parameters"photo".Value = imagebytes; com.Parameters.Add("empno",SqlDbType.Char); com.Parameters"empno".Value = empno; com.Parameters.Add(&qu

6、ot;remark", SqlDbType.Char); com.Parameters"remark".Value = remark; com.ExecuteNonQuery(); Common.DataAccess.GetConn().Close(); MessageBox.Show("更新成功", MessageBoxButtons.OK, MessageBoxIcon.Information); 点击“查找”按钮,调用Find和img方法实现查找信息,并把文本信息显示在textbox中,照片显示在picturebox中private vo

7、id tbFind_Click(object sender, EventArgs e) if (cbItem.Text = "") MessageBox.Show("请输入员工编号", "提示º?", MessageBoxButtons.OK, MessageBoxIcon.Information); else string sql = string.Format("select count(*) from T_Photo where EmployeeNO='0'", cbItem.Tex

8、t); SqlCommand cmd = new SqlCommand(sql, Common.DataAccess.GetConn(); int i = (int)cmd.ExecuteScalar(); if (i = 1) txtEmpNO.Text = "" txtName.Text = "" txtRemark.Text = "" pb.Image = null; dataGridView1.DataSource =.Find(cbItem.Text); txtEmpNO.Text =.Find(cbItem.Text).R

9、ows01.ToString(); txtName.Text =.Find(cbItem.Text).Rows02.ToString(); txtRemark.Text =.Find(cbItem.Text).Rows03.ToString(); pb.Image =.img(txtEmpNO.Text, txtName.Text); else MessageBox.Show("此员工不存在", "提示º?", MessageBoxButtons.OK, MessageBoxIcon.Information); Find方法(即返回一个data

10、table)Private static DataTable Find(string s) return Common.DataAccess.GetDatatable("select T_Photo.PhotoID,T_Photo.EmployeeNO,T_Employee.EmployeeName," + "T_Photo.Remark from T_Photo inner join T_Employee on T_Photo.EmployeeNO=T_Employee.EmployeeNO where T_Photo.EmployeeNO= " +

11、s); Img方法Private static Image img(string txtno,string txtname) string sql1 = string.Format("select PhotoContent from T_Photo where EmployeeNO='0'", txtno); SqlCommand cmd1 = new SqlCommand(sql1, Common.DataAccess.GetConn(); SqlDataReader dr = cmd1.ExecuteReader(); while (dr.Read()

12、if (dr"PhotoContent"= System.DBNull.Value) string message = string.Format("没?有®D0的Ì?照?片?,ê?请?添¬¨ª加¨®",txtname); MessageBox.Show(message,"提¬¨¢示º?",MessageBoxButtons.OK,MessageBoxIcon.Information); else MemoryStr

13、eam ms = new MemoryStream(byte)dr0); Image img = Image.FromStream(ms); return img; return null; 点击datagridview显示数据private void dataGridView1_Click(object sender, EventArgs e) txtEmpNO.Text = dataGridView1.RowsdataGridView1.CurrentCell.RowIndex.Cells1.Value.ToString(); txtName.Text = dataGridView1.RowsdataGridView1.CurrentCell.RowIndex.Cells2.V

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论