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.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方法,以实现信息及照片的存储和更改 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) {
FileStream fs = new FileStream(fullpath, FileMode.Open); byte[] imagebytes = new byte[fs.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\");
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(\"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 void 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.Text); 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).Rows[0][1].ToString(); txtName.Text =.Find(cbItem.Text).Rows[0][2].ToString(); txtRemark.Text =.Find(cbItem.Text).Rows[0][3].ToString(); pb.Image =.img(txtEmpNO.Text, txtName.Text);
} else {
MessageBox.Show(\"此员工不存在\", \"提示º?\", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }
Find方法(即返回一个datatable)
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= \" + 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()) {
if (dr[\"PhotoContent\"]== System.DBNull.Value) {
string message = string.Format(\"没?有®D{0}的Ì?照?片?,ê?请?添¬¨ª加¨®\",txtname); MessageBox.Show(message,\"提¬¨¢示º?\",MessageBoxButtons.OK,MessageBoxIcon.Information); } else {
MemoryStream ms = new MemoryStream((byte[])dr[0]); Image img = Image.FromStream(ms); return img; } }
return null; }
点击datagridview显示数据
private void dataGridView1_Click(object sender, EventArgs e) {
txtEmpNO.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1].Value.ToString(); txtName.Text = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[2].Value.ToString(); txtRemark .Text =dataGridView1 .Rows [dataGridView1 .CurrentCell .RowIndex].Cells[3].Value.ToString(); pb.Image =img(txtEmpNO.Text, txtName.Text); button1.Enabled = true; btSave.Enabled = true; }
因篇幅问题不能全部显示,请点此查看更多更全内容