日期:2014-05-20  浏览次数:20959 次

求教:如何提取数据库(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()方法不能正确将图片提取出来。
谢谢大家帮我解决这个问题,我已经郁闷了很多星期了。

------解决方案--------------------
没看出什么问题,当然不可能直接读出image...

一般的做法就是将图片转成二进制流存到数据库中,然后取出来的时候再先读出二进制数据,然后再转成图片...

byte[] bytes=(byte[])this.myReader.GetValue(0);你这样做没什么问题...

你是怎么将图片存进去的,可能是你存的不对,所以读出来也就出现错误了..