1. //浏览图片
  2.  
  3. private void btnUp_Click(object sender, EventArgs e)
  4.  
  5. {
  6.  
  7. OpenFileDialog ofd = new OpenFileDialog();
  8.  
  9. ofd.Title = "选择要上传的图片";
  10.  
  11. ofd.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";
  12.  
  13. ofd.ShowDialog();
  14.  
  15. textBox1.Text = ofd.FileName;
  16.  
  17. if (!File.Exists(ofd.FileName))
  18.  
  19. {
  20.  
  21. MessageBox.Show("照片为空");
  22.  
  23. return;
  24.  
  25. }
  26.  
  27. }
  28.  
  29. //上传保存到数据库
  30.  
  31. private void btnUpLoad_Click(object sender, EventArgs e)
  32.  
  33. {
  34.  
  35. string strPath = txtbImage.Text.Trim();
  36.  
  37. FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
  38.  
  39. byte[] byteFile = new byte[fs.Length];
  40.  
  41. fs.Read(byteFile, , (int)fs.Length);
  42.  
  43. fs.Close();
  44.  
  45. SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
  46.  
  47. try
  48.  
  49. {
  50.  
  51. SqlCommand cmd = new SqlCommand();
  52.  
  53. cmd.Connection = conn;
  54.  
  55. string strSql = "insert into test(FileName,Img) Values(@FileName,@Img)";
  56.  
  57. cmd.CommandText =strSql ;
  58.  
  59. //cmd.Parameters.AddWithValue("@FileName", strPath);
  60.  
  61. //cmd.Parameters.AddWithValue("@Img", byteFile);
  62.  
  63. //或者
  64.  
  65. SqlParameter[] parameters = new SqlParameter[];
  66.  
  67. parameters[] = new SqlParameter("@FileName", SqlDbType.NVarChar, );
  68.  
  69. parameters[].Value = strPath;
  70.  
  71. parameters[] = new SqlParameter("@Img", SqlDbType.Image,int.MaxValue);
  72.  
  73. parameters[].Value = byteFile;
  74.  
  75. cmd.Parameters.AddRange(parameters);
  76.  
  77. conn.Open();
  78.  
  79. cmd.ExecuteNonQuery();
  80.  
  81. conn.Close();
  82.  
  83. MessageBox.Show("上传成功");
  84.  
  85. }
  86.  
  87. catch
  88.  
  89. {
  90.  
  91. conn.Close();
  92.  
  93. MessageBox.Show("上传失败!");
  94.  
  95. }
  96.  
  97. }
  98.  
  99. 从数据库读取图片显示到窗体:
  100.  
  101. //读到图片显示到PictureBox
  102.  
  103. private void btnDownLoad_Click(object sender, EventArgs e)
  104.  
  105. {
  106.  
  107. byte[] bytFile;
  108.  
  109. SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");
  110.  
  111. try
  112.  
  113. {
  114.  
  115. SqlCommand cmd = new SqlCommand();
  116.  
  117. string strSql = "select img from test where ID=3";
  118.  
  119. cmd.Connection = conn;
  120.  
  121. cmd.CommandText = strSql;
  122.  
  123. conn.Open();
  124.  
  125. SqlDataReader sdr = cmd.ExecuteReader();
  126.  
  127. if (sdr.Read())
  128.  
  129. {
  130.  
  131. bytFile = (Byte[])sdr["Img"];
  132.  
  133. }
  134.  
  135. else
  136.  
  137. {
  138.  
  139. bytFile = new byte[];
  140.  
  141. }
  142.  
  143. sdr.Close();
  144.  
  145. conn.Close();
  146.  
  147. //通过内存流MemoryStream,
  148.  
  149. //把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,
  150.  
  151. //让数据库中的图片直接显示在窗体上。
  152.  
  153. MemoryStream ms = new MemoryStream(bytFile, , bytFile.Length);
  154.  
  155. this.picImage.Image = Image.FromStream(ms);
  156.  
  157. //关闭内存流
  158.  
  159. ms.Close();
  160.  
  161. }
  162.  
  163. catch
  164.  
  165. {
  166.  
  167. conn.Close();
  168.  
  169. MessageBox.Show("失败");
  170.  
  171. }
  172.  
  173. }

代码转自IT学习广场http://www.itxxgc.com/net/detail/30

                 来自凌波小屋-----冯和超的笔记-------

C#(WinForm)上传图片保存到数据库和从数据库读取图片显示到窗体的更多相关文章

  1. C# 保存PictureBox中的图片到数据库,并从数据库读取图片显示到PictrueBox,解决报错 “无效参数”

    下面是两段关键代码: /// <summary> /// 将一张图片转换为字节 /// </summary> /// <param name="img" ...

  2. MySQL数据库写入图片并读取图片显示到JLabel上的详解

    相较于Oracle,MySQL作为一个轻量级的开源的数据库,可谓是大大简化了我们的操作.这次我就来写一个关于数据库存入图片,获取图片的例子吧,也为了今后的复习使用.(我们一般采取存入路径的方式,而不是 ...

  3. VS.C#如何向数据数据库中存入和读取图片的

    写入图片部分代码:假设图片为 test.gifbyte [] bytes = File.ReadAllBytes(@"c:\test.gif");SqlConnection con ...

  4. [转]asp.net mvc 从数据库中读取图片

    本文转自:http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html 首先是创建一个类,继承于ActionResult,记住要引用Syste ...

  5. asp.net mvc 从数据库中读取图片的实现代码

    首先是创建一个类,继承于ActionResult,记住要引用System.Web.Mvc命名空间,如下: public class ImageResult : ActionResult { publi ...

  6. php实现上传图片保存到数据库的方法

    http://www.jb51.net/article/61034.htm 作者:傲雪星枫 字体:[增加 减小] 类型:转载   这篇文章主要介绍了php实现上传图片保存到数据库的方法,可通过将图片保 ...

  7. C# 图片保存到数据库和从数据库读取图片并显示

    图片保存到数据库的方法: public void imgToDB(string sql)        {   //参数sql中要求保存的imge变量名称为@images            //调 ...

  8. 基于Enterprise Library的Winform开发框架实现支持国产达梦数据库的扩展操作

    由于一个客户朋友的需求,需要我的Winform开发框架支持国产达梦数据库的操作,这个数据库很早就听过,但是真正一般项目用的很少,一般在一些特殊的项目可能需要用到.由于我的Winform开发框架,是基于 ...

  9. winform里dataGridView分页代码,access数据库

    winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...

随机推荐

  1. 转一篇NGINX+UWSGI+PYTHON+DJANGO部署文档

    高远弄的,,专业,明晓..感谢哈哈. http://blog.csdn.net/tmpbook/article/details/42873667

  2. network: Android 网络判断(wifi、3G与其他)

    package mark.zeng; import Java.util.List; import Android.content.Context; import android.location.Lo ...

  3. ♫【Underscore.js】

    Underscore.js Underscore.js 1.5.2 中文文档 Underscore 是一个JavaScript实用库,提供了类似Prototype.js (或 Ruby)的一些功能,但 ...

  4. 图论(费用流):BZOJ 4514 [Sdoi2016]数字配对

    4514: [Sdoi2016]数字配对 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 820  Solved: 345[Submit][Status ...

  5. kNN

    传统的kNN模型 为了获得用户对产品的评分预测值,kNN模型一般包括以下三步: 1.计算相似度 这步中计算每对产品之间的相似度 Person correlation: \[S _ {mn} ^{P} ...

  6. 在pcDuino上刷了AndDroid,Ubuntu,XBMC

    一.Android.Ubuntu.XBMC播放高清视频得比较 1.Andrioid上播放1080P 无压力,硬件解码 2.Ubuntu上用Mplayer播放视频会很卡,可能是没有硬解的原因 3.Ubu ...

  7. Java中的字符串流的读取和写入(创建文件并判断重复账户)

    各位我又来了!!哎!好心酸!我还没注册到三天!!没法登上博客的首页!!心累!! import java.io.BufferedOutputStream; import java.io.Buffered ...

  8. 《HTML5 从入门到精通--7.6.3 单元格垂直跨度——rowspan》

    单元格除了能够在水平方向上跨列,还能够垂直方向上跨行.跨行设置须要使用rowspan參数. 语法 <td rowspan="单元格跨行数"> 语法解释 与水平跨度相相应 ...

  9. android AppWidgwtProvider学习

    实现AppWidgwtProvider: onUpdate() //在达到制定的更新时间之后或者当用户向桌面添加   App Widget时会调用该方法. onDeleted() //当App Wid ...

  10. 微信支付bug

    1.最基本的操作就是检查各项参数正确2.确保将测试微信号加入测试白名单 3.目录正确:发起授权请求的页面必须是在授权目录下的页面,而不能是存在与子目录中.否则会返回错误,Android返回“Syste ...