博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
方法:C#在WinForm中如何将Image存入SQL2000和取出显示
阅读量:6909 次
发布时间:2019-06-27

本文共 1002 字,大约阅读时间需要 3 分钟。

//保存   

  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);   
  }   
  }

 

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

转载于:https://www.cnblogs.com/zhaolizhe/p/6936698.html

你可能感兴趣的文章
一年获得10项专利 常州一护士成“发明达人”
查看>>
中国就业市场当前仍呈整体向好、结构不断优化态势
查看>>
8 月编程语言排行榜:Python 强势逼近 Java,C 已穷途末路
查看>>
人工智能已经说烂,DuerOS告诉你是如何工作的
查看>>
一盘很大的棋,阿里3年内要招揽200名青年科学家
查看>>
正确面对跨域,别慌
查看>>
关于input的一些问题解决方法分享
查看>>
【译】Effective TensorFlow Chapter8——控制流操作:条件和循环
查看>>
骗子或许比你更了解网络攻防
查看>>
从贝叶斯定理到概率分布:综述概率论基本定义
查看>>
Satoshis Vision大会:‘乱局’之中的Bitcoin Cash
查看>>
前端中的 IoC 理念
查看>>
Android开源框架源码鉴赏:VirtualAPK
查看>>
在 V8 引擎中设置原型(prototypes)
查看>>
源码|并发一枝花之ReentrantLock与AQS(2):lockInterruptibly
查看>>
Lumen 使用 throttle 限制接口访问频率
查看>>
怎样给文件命名才能显得更加专业
查看>>
python多线程
查看>>
原来云数据库也是有思想的...
查看>>
GitHub 项目徽章的添加和设置
查看>>