我在做一个winform项目时,要把一个图片存入SQL 中,然后再读入图片控件中,我的存入代码是:
  
   byte [] imageData;
   foreach(string filePath in open.FileNames)
 {
    FileInfo fi=new FileInfo(filePath);
      imageData=new byte[fi.Length];
      FileStream fs=fi.OpenRead();
     fs.Read(imageData,0,ConvertInt32(fi.Length));
}
 然后我执行了一个SQl语句,insert into aa values('123',imageData)   ,其中123为图片编号,imageData在SQL中是image类型,,, 执行后操作成功 现在问题是,我想把图片从SQl中读出来,又写了以下代码,,可读出来却不行,读出代码如下:
  object Tempob=CommonA.ExecuteScalar("select imageData from aa where id='123'");// 本人省去SQL操作的一段
   byte[] imgs=(byte[])Tempob;
   MemoryStream ms=new  MemoryStream(imgs);
   picMyPic.Image=Image.FromStream(ms);执行后没有任何语法错误,但就是读不出图片来,也没法在 picMyPic 图片控件中显示出来有谁知道是什么原因啊??  我测试过了,SQL中是写入成功的,好像读出来后的文件是不完整的(我的猜测),
  

解决方案 »

  1.   

    OpenFileDialog of = new OpenFileDialog();  //创建OpenFileDialog对象
                of.ShowDialog ();                         //打开文件
              string filename=  of.FileName;              //获取文件路径                  
              FileStream fs = new FileStream(filename ,FileMode .Open );   //创建文件流对象
                byte []bytes=new byte [fs.Length ];                      //创建字节数组
              fs.Read(bytes ,0,(int )fs .Length );                     //打开Read方法
              string sql = "insert into stu values(@images)";          //编写sql语句
              SqlCommand cmd = new SqlCommand(sql,DB .con );            //创建SqlCommand对象
              DB.con.Open();                                          //打开数据库
              cmd.Parameters.Add("@images", SqlDbType.Image).Value = bytes;    //为参数赋值
              cmd.ExecuteNonQuery();                                          //执行sql命令
              DB.con.Close();                                                 //关闭数据库 //从数据库中提取图像string sql = "select top 1 * from stu order by id desc ";              //编写sql语句
                SqlCommand cmd = new SqlCommand(sql,DB .con );              //创建SqlCommand对象            DB.con.Open();                                              //打开数据库
                SqlDataReader read = cmd.ExecuteReader();                   //创建SqlDataReader对象
                read.Read();                                                 //打开读取的方法
                byte []bytes=(byte[])read ["image"];                         //读取
                read.Close();                                                //关闭读取的方法
               DB.con.Close();                                                //关闭数据库
               
                MemoryStream m = new MemoryStream(bytes );                    //创建支持读取区的内存流
                pictureBox1.Image = Image.FromStream(m);                      //显示在图片框上
      

  2.   


    FileStream fs=fi.OpenRead();
    fs.Seek(0, SeekOrigin.Begin);
    fs.Read(imageData,0,ConvertInt32(fi.Length));你再检查下,读出来的byte有值吗?
      

  3.   

    http://hi.baidu.com/yjh6016/blog/item/ef876f611cfaa6d88cb10d75.html
    http://topic.csdn.net/u/20100310/17/c4bbf36c-ff98-4472-aeec-3618fac448d3.html
    http://blog.163.com/tuibianzhilv001/blog/static/167563877201081013131221/
    http://www.7747.net/kf/201007/52486.html
    参考
      

  4.   

    http://topic.csdn.net/u/20100310/17/c4bbf36c-ff98-4472-aeec-3618fac448d3.html
      

  5.   

    读的代码应该没有问题
    是不是存的时候出的问题,换一种方式试试
    直接FileStream fs = new FileStream(filename ,FileMode.Open )试试
      

  6.   

    存图片,要不就是BLOB,小图片可以存成base64的字符串。
      

  7.   

    在使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来 一,上传并存入SqlServer 
     数据库结构 
      create table test 
      { 
         id identity(1,1), 
         FImage image 
      } 
      相关的存储过程 
      Create proc UpdateImage 
      ( 
         @UpdateImage Image 
      ) 
      As 
      Insert Into test(FImage) values(@UpdateImage) 
      GO 在UpPhoto.aspx文件中添加如下: 
    <input id="UpPhoto" name="UpPhoto" runat="server" type="file"> 
    <asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button> 然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码: 
    private void btnAdd_Click(object sender, System.EventArgs e) 

            //获得图象并把图象转换为byte[] 
            HttpPostedFile upPhoto=UpPhoto.PostedFile; 
            int upPhotoLength=upPhoto.ContentLength; 
            byte[] PhotoArray=new Byte[upPhotoLength]; 
            Stream PhotoStream=upPhoto.InputStream; 
            PhotoStream.Read(PhotoArray,0,upPhotoLength);         //连接数据库 
            SqlConnection conn=new SqlConnection(); 
            conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";         SqlCommand cmd=new SqlCommand("UpdateImage",conn); 
            cmd.CommandType=CommandType.StoredProcedure;         cmd.Parameters.Add("@UpdateImage",SqlDbType.Image); 
            cmd.Parameters["@UpdateImage"].Value=PhotoArray;         //如果你希望不使用存储过程来添加图片把上面四句代码改为: 
            //string strSql="Insert into test(FImage) values(@FImage)"; 
            //SqlCommand cmd=new SqlCommand(strSql,conn); 
            //cmd.Parameters.Add("@FImage",SqlDbType.Image); 
            //cmd.Parameters["@FImage"].Value=PhotoArray;  conn.Open(); 
     cmd.ExecuteNonQuery(); 
     conn.Close(); 
      

  8.   

    二,从SqlServer中读取并显示出来 
    在需要显示图片的地方添加如下代码: 
    <asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image> ShowPhoto.aspx主体代码: 
    private void Page_Load(object sender, System.EventArgs e) 

         if(!Page.IsPostBack) 
         { 
                    SqlConnection conn=new SqlConnection() 
                    conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa"; 
                    
                    string strSql="select * from test where id=2";//这里假设获取id为2的图片 
                    SqlCommand cmd=new SqlCommand() 
                    reader.Read(); 
                    Response.ContentType="application/octet-stream"; 
                    Response.BinaryWrite((Byte[])reader["FImage"]); 
                    Response.End(); 
                    reader.Close(); 
         }