zoukankan      html  css  js  c++  java
  • 方法:C#在WinForm中如何将Image存入SQL2000和取出显示

    //保存   
      sql="insert   into   p_ry_photo(id,photo)   values   ('"+id+"',@i)";   
      SqlCommand   cmd=new   SqlCommand(sql,CommonVar.SqlConDataBase);   
      MemoryStream   s   =   new   MemoryStream();   
      picPhoto.Image.Save(s,System.Drawing.Imaging.ImageFormat.Jpeg);   
      byte[]   b   =   s.ToArray();   
      s.Close();   
      cmd.Parameters.Add("@i",SqlDbType.Image,(int)b.Length);   
      cmd.Parameters["@i"].Value=b;   
      cmd.ExecuteNonQuery();

    //读取图象   

    private   void   Readphoto(string   id)   
      {   
        try   
          {   
              string   ls_sql="select   photo   from   p_ry_photo   where   id='"+id+"'";   
              this.picPhoto.Image=null;   
              this.picPhoto.Refresh();   
              SqlCommand   cmd=new   SqlCommand(ls_sql,CommonVar.SqlConDataBase);   
              SqlDataReader   reader=cmd.ExecuteReader();   
              while(reader.Read())   
                  {   
                      byte[]   b   =   (byte[])reader[0];   
                      MemoryStream   s   =   new   MemoryStream(b);   
                      Bitmap   bmp   =   new   Bitmap(s);   
                      System.Drawing.Image   image   =   bmp;   
                      picPhoto.Image   =   image;   
                      s.Close();   
                      }   
                reader.Close();   
            }   
      catch(Exception   ex)   
      {   
            MessageBox.Show(ex.Message);   
      }   
      }

    建议将上传图片以文件形式保存,然后在数据库中存入路径。数据库读写图片耗时太长,很不划算。

  • 相关阅读:
    P3970 [TJOI2014]上升子序列
    受欢迎的牛(Tarjan缩点模板)
    Y15BeTa的乱搞方法(占坑待填)
    Luogu P4145 上帝造题的七分钟2 / 花神游历各国
    Luogu P1525 【关押罪犯】
    Luogu P1077 摆花 NOIP2012pjT3
    Nowcoder deco的abs
    CSP-S前的芝士清单
    普天同庆
    线段树区改区查标记永久化板子
  • 原文地址:https://www.cnblogs.com/zhaolizhe/p/6936698.html
Copyright © 2011-2022 走看看