主题:求教:如何提取数据库(SqLServer2000)中的图像列
请问我如何使用C#将数据库中的Image属性列存取的图像放到PictureBox中,前提是使用SqlDataReader,下面是我写的部分代码,请各位专家指教:谢谢
private void button3_Click(object sender, System.EventArgs e)
{
if(this.sqlCommand_Select.Connection.State==ConnectionState.Closed)
{
this.sqlCommand_Select.Connection.Open();
}
this.myReader=this.sqlCommand_Select.ExecuteReader();
myReader.Read();//假设只有一行,就不用while循环了
this.ShowDBImage();
}
//ShowDBImage方法如下:
private void ShowDBImage()
{
try
{
byte[] bytes=(byte[])this.myReader.GetValue(0);
MemoryStream memStream=new MemoryStream(bytes);
Bitmap MyImage = new Bitmap(memStream);
this.pictureBox1.Image= MyImage;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
this.pictureBox1.Image=null;
}
}
问题关键是,byte[] bytes=(byte[])this.myReader.GetValue(0);这句,这列的数据类型是image,但SqlDataReader没有GetImage()方法,我就使用了GetValue(0);但显然GetValue()方法不能正确将图片提取出来。
谢谢大家帮我解决这个问题,我已经郁闷了很多星期了。
private void button3_Click(object sender, System.EventArgs e)
{
if(this.sqlCommand_Select.Connection.State==ConnectionState.Closed)
{
this.sqlCommand_Select.Connection.Open();
}
this.myReader=this.sqlCommand_Select.ExecuteReader();
myReader.Read();//假设只有一行,就不用while循环了
this.ShowDBImage();
}
//ShowDBImage方法如下:
private void ShowDBImage()
{
try
{
byte[] bytes=(byte[])this.myReader.GetValue(0);
MemoryStream memStream=new MemoryStream(bytes);
Bitmap MyImage = new Bitmap(memStream);
this.pictureBox1.Image= MyImage;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
this.pictureBox1.Image=null;
}
}
问题关键是,byte[] bytes=(byte[])this.myReader.GetValue(0);这句,这列的数据类型是image,但SqlDataReader没有GetImage()方法,我就使用了GetValue(0);但显然GetValue()方法不能正确将图片提取出来。
谢谢大家帮我解决这个问题,我已经郁闷了很多星期了。