我正在尝试使用列表框选择事件
从访问数据库中检索图像我使用以下代码将数据保存到数据库:
command.CommandText = "insert into EmployeeInfo (FirstName,LastName,Pay,Pic) values ('" + txt_fname.Text + "' , '" + txt_lname.Text + "' , '" + txt_pay.Text + "' , @productpic)";
MemoryStream stream = new MemoryStream();
pb1.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] pic = stream.ToArray();
command.Parameters.AddWithValue("@productpic", pic);
我正在使用以下代码检索数据:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string query = "select * from EmployeeInfo where FirstName = '" + listBox1.Text +"'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read()) {
list_fname.Text = reader["FirstName"].ToString();
list_lname.Text = reader["LastName"].ToString();
list_dob.Text = reader["DoB"].ToString();
}
connection.Close();
}
现在我要做的是检索与每一行相关联的图片,将字段名称Pic改为图片框,并在while循环中显示其他数据。我已经看到其他人提出了几个问题,但每个人都在使用datagridview来检索数据,在我的情况下,我试图在listbox select事件中执行此操作。
任何形式的帮助都将受到高度赞赏。提前致谢 。
答案 0 :(得分:0)
在列表框选择事件中的while循环中调用此函数:
void loadpicture()
{
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "select pic from EmployeeInfo where FirstName = '" + listBox1.Text + "'";
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
byte[] content = (byte[])ds.Tables[0].Rows[0].ItemArray[0];
MemoryStream stream = new MemoryStream(content);
pb1.Image = Image.FromStream(stream);
}