//浏览图片

         private void btnUp_Click(object sender, EventArgs e)

         {

             OpenFileDialog ofd = new OpenFileDialog();

             ofd.Title = "选择要上传的图片";

             ofd.Filter = "All Files(*.*)|*.*|位图(*.bmp)|*.bmp|JPEG(*.jpg)|*.jpg";

             ofd.ShowDialog();

             textBox1.Text = ofd.FileName;

             if (!File.Exists(ofd.FileName))

             {

                 MessageBox.Show("照片为空");

                 return;

             }

         }

         //上传保存到数据库

         private void btnUpLoad_Click(object sender, EventArgs e)

         {

             string strPath = txtbImage.Text.Trim();

             FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);

             byte[] byteFile = new byte[fs.Length];

             fs.Read(byteFile, , (int)fs.Length);

             fs.Close();

             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");

                 try

                 {

                     SqlCommand cmd = new SqlCommand();

                     cmd.Connection = conn;

                     string strSql = "insert into test(FileName,Img) Values(@FileName,@Img)";

                     cmd.CommandText =strSql ;

                     //cmd.Parameters.AddWithValue("@FileName", strPath);

                     //cmd.Parameters.AddWithValue("@Img", byteFile);

                     //或者

                     SqlParameter[] parameters = new SqlParameter[];

                     parameters[] = new SqlParameter("@FileName", SqlDbType.NVarChar, );

                     parameters[].Value = strPath;

                     parameters[] = new SqlParameter("@Img", SqlDbType.Image,int.MaxValue);

                     parameters[].Value = byteFile;

                     cmd.Parameters.AddRange(parameters);

                     conn.Open();

                     cmd.ExecuteNonQuery();

                     conn.Close();

                     MessageBox.Show("上传成功");

                 }

                 catch

                 {

                     conn.Close();

                     MessageBox.Show("上传失败!");

                 }

         }

 从数据库读取图片显示到窗体:

 //读到图片显示到PictureBox

         private void btnDownLoad_Click(object sender, EventArgs e)

         {

             byte[] bytFile;

             SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True");

                 try

                 {

                     SqlCommand cmd = new SqlCommand();

                     string strSql = "select img from test where ID=3";

                     cmd.Connection = conn;

                     cmd.CommandText = strSql;

                     conn.Open();

                     SqlDataReader sdr = cmd.ExecuteReader();

                     if (sdr.Read())

                     {

                         bytFile = (Byte[])sdr["Img"];

                     }

                     else

                     {

                         bytFile = new byte[];

                     }

                     sdr.Close();

                     conn.Close();

                     //通过内存流MemoryStream,

                     //把byte[]数组fileContent加载到Image中并赋值给图片框的Image属性,

                     //让数据库中的图片直接显示在窗体上。

                     MemoryStream ms = new MemoryStream(bytFile, , bytFile.Length);

                     this.picImage.Image = Image.FromStream(ms);

                     //关闭内存流

                     ms.Close();

                 }

                 catch

                 {

                     conn.Close();

                     MessageBox.Show("失败");

                 }

         }

代码转自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. X-Plosives

    uvaLive 3644:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&pa ...

  2. Buffer Sort

    BUFFER (SORT) Description Performs a memory sort on a row source CREATE TABLE t1 (c01 NUMBER); CREAT ...

  3. linux配置端口转发

    一.使用rinted进行端口转发 将10.50.13.13 80请求转到10.50.13.11 80上 1.安装rinetd $ tar zxf rinetd.tar.gz $ cd rinetd $ ...

  4. 网络流(最大流) POJ 1637 Sightseeing tour

    Sightseeing tour Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8628   Accepted: 3636 ...

  5. 解决mysql不能远程登录的问题

    1. 改表法.可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 "mysql" 数据库里的 " ...

  6. springMVC整合jedis+redis

    http://www.cnblogs.com/zhengbn/p/4140549.html 前两天写过 springMVC+memcached 的整合,我从这个基础上改造一下,把redis和sprin ...

  7. docker安装lnmp 环境

    docker基础知识请转 docker中文文档:http://docker-doc.readthedocs.io/zh_CN/latest/index.html docker英文文档: https:/ ...

  8. 关于openoffice英文乱码的问题

    首先选中乱码的部分,然后在右边的侧栏中看到其字体,尝试改变它的字体,看会不会显示正常,如果可以,先记住这两种字体.然后:    工具->选项->字体        然后在使用替换表打上勾, ...

  9. java 吞吐量

    jvm中 ,执行用户的代码占 的时间/总时间 ,假如前者是99 分钟,后者一分钟,则吞吐量为99% ,吞吐量越大.系统越好,如何设计系统,导致系统吞吐量高,因为我们知道,垃圾回收,7种垃圾收集器,也不 ...

  10. ubuntu开机黑屏

    以下皆在VM虚拟机下 (1)ctr+alt+f4 进入命令行 (2) sudo apt-get update sudo apt-get install xserver-xorg-lts-quantal ...