//浏览图片

         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. Area

    http://poj.org/problem?id=1265 #include<cstdio> #include<istream> #include<algorithm& ...

  2. Sequence《优先队列》

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  3. VBA读取word中的内容到Excel中

    原文:VBA读取word中的内容到Excel中 Public Sub Duqu()      Dim myFile As String     Dim docApp As Word.Applicati ...

  4. The Same Game(模拟)

    http://poj.org/problem?id=1027 题意:给一个10*15的地图,里面填充R,G,B三种颜色,每次找到当前地图的同色最大区域M,并将其删除,删除M后,上面的小球自然下落,当有 ...

  5. 【转】Java中字符串中子串的查找共有四种方法(indexof())

    原文网址:http://wfly2004.blog.163.com/blog/static/1176427201032692927349/ Java中字符串中子串的查找共有四种方法,如下:1.int ...

  6. 用SD卡下载uboot、linux内核和文件系统

    1. 移植mtd-utils: a) 下载utd-utils 下载地址为ftp://ftp.infradead.org/pub/mtd-utils/b) 交叉编译mtd-utilsi   修改Make ...

  7. 动态规划(斜率优化):[CEOI2004]锯木厂选址

    锯木场选址(CEOI2004) 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂. 木材只能按照一个方向运输:朝山下运.山脚下有 ...

  8. 【数学规律】Vijos P1582 笨笨的L阵游戏

    题目链接: https://vijos.org/p/1582 题目大意: 就是o(o<=50)个人在n*m(n,m<=2000)的格子上放L型的东西(有点像俄罗斯方块的L,可对称旋转),问 ...

  9. 汇编学习笔记(7)call和ret指令

    ret和retf CPU执行ret指令时进行以下两步操作: (IP)=((ss)*16+(sp)) (sp)=(sp)+2 这相当于pop IP CPU执行retf指令时进行以下四步操作: (IP)= ...

  10. oracle常用命令总结

    声明:本文为博主在做项目中用到的一些常用命令,请勿转载,只为保存. oracle常用命令总结 创建表空间: --create tablespace vms--datafile 'e:\vms.dbf' ...