问题描述
我在访问数据库中存储了一些图像。但我无法检索并显示在图片框中。我的代码如下。一切都很好但是。打破数据集。
请更新我在哪里做错了。
private void LoadPicture()
{
con.Open();
cmd.CommandText =从图片中选择图片,其中id ='+ listBox1.Text.ToString()+' ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cbd = new OleDbCommandBuilder(da);
DataSet ds = new DataSet() ;
da.Fill(ds); //(在这里得到错误)
con.Close();
byte [] ap = (byte [])(ds.Tables [0] .Rows [0] [picture]);
MemoryStream ms = new MemoryStream(ap);
pictureBox1 .Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
label1.Text = listBox1.Text.ToString();
ms.Close();
}
//更新列表框如下。
private void listBox2_SelectedIndexChanged(object sender,EventArgs e)
{
ListBox l =发送者为ListBox ;
if(l.SelectedIndex!= -1)
{
listBox1.SelectedIndex = l.SelectedIndex;
listBox2.SelectedIndex = l.SelectedIndex;
LoadPicture();
}
}
I stored some images in access db. But i am not able to retrieve and display in picture box. My code is like below. Everything is good but. Breaking at data set.
Please update me where i am doing wrong.
private void LoadPicture()
{
con.Open();
cmd.CommandText = "select picture from pictures where id ='" + listBox1.Text.ToString() + "'";
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
OleDbCommandBuilder cbd = new OleDbCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds); //(getting Error Here)
con.Close();
byte[] ap = (byte[])(ds.Tables[0].Rows[0]["picture"]);
MemoryStream ms = new MemoryStream(ap);
pictureBox1.Image = Image.FromStream(ms);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.BorderStyle = BorderStyle.Fixed3D;
label1.Text = listBox1.Text.ToString();
ms.Close();
}
//Updated List Box like below.
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox l = sender as ListBox;
if(l.SelectedIndex != -1)
{
listBox1.SelectedIndex = l.SelectedIndex;
listBox2.SelectedIndex = l.SelectedIndex;
LoadPicture();
}
}
推荐答案
cmd.CommandText = "select picture from pictures where id ='" +
listBox1.Text.ToString() +
"'";
它应显示为:
It should read:
cmd.CommandText = "select picture from pictures where id ='" +
listBox1.SelectedItem.Text +
"'";
当然这假设在 listBox1
中选择了一个项目。 此外,你的代码犯了SQL注入罪,但这是一个完全不同的故事。
/ ravi
Of course this assumes an item is selected in listBox1
. Also, your code is guilty of SQL injection, but that's a whole different story.
/ravi
这篇关于如何使用列表框将图像检索到图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!