//浏览图片

         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. 模拟I2C从机程序

    ;Slave.asm SCL BIT P1. SDA BIT P1. ;---------------------------- ORG RESET: SETB SCL SETB SDA CALL I ...

  2. Lua参考手册

    英文原版: http://www.lua.org/manual/5.1/ 中文版下面2个地址都有:一样的 manual.luaer.cn lua在线手册 lua参考手册Lua参考手册的中文翻译(云风翻 ...

  3. Integer Intervals(贪心)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12123   Accepted: 5129 Description An i ...

  4. 「Poetize3」导弹防御塔

    描述 Description Freda控制着N座可以发射导弹的防御塔.每座塔都有足够数量的导弹,但是每座塔每次只能发射一枚.在发射导弹时,导弹需要T1秒才能从防御塔中射出,而在发射导弹后,发射这枚导 ...

  5. Java编译原理

    http://wenku.baidu.com/view/f9b1734b87c24028915fc3a3.html Java编译原理 1. 关于动态加载机制 学习Java比C++更容易理解OOP的思想 ...

  6. 【转】图说Android的8年演变史

    原文网址:http://tech.gmw.cn/2016-02/17/content_18916338_14.htm 图说Android的8年演变史 我有话说    0      2011年10月谷歌 ...

  7. CodeForces 595A

    题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...

  8. MVC4 中Remote的使用

    相信当你看到这篇文章的时候对remote是有一些了解了, 起码知道这个东西是来干嘛用的. 这里也就不废话了 直接上代码  看看Remote的一些使用方式. 1.判断表单上输入的数据是否存在 [Syst ...

  9. eclipse中的项目导出成Androidstudio的识别的项目,so文件打包不进去

    需要加入jniLibs.srcDirs = ['libs']才可以把so文件打入包内 sourceSets { main { manifest.srcFile 'AndroidManifest.xml ...

  10. windows下python安装paramiko

    Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是 ...