C#图片存入数据库并读取显示在picturebox中

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实现功能:把图片及人员信息存入数据库并可通过编号查找显示在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.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, rmation);
}
点击“查找”按钮,调用Find和img方法实现查找信息,并把文本信息显示在textbox中,照片显示在picturebox中
private void tbFind_Click(object sender, EventArgs e)
{
if (cbItem.Text == "")
{
MessageBox.Show("请输入员工编号", "提示º?", MessageBoxButtons.OK, rmation);
}
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, rmation); }
}
}
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,rmation);
}
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;
}。

相关文档
最新文档