private void button2_Click_1(object sender, System.EventArgs e)

{

string pathName;

if (this.openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)

{

pathName = this.openFileDialog1.FileName;

System.Drawing.Image img = System.Drawing.Image.FromFile(pathName);

this.pictureBox1.Image = img;

//将图像读入到字节数组

System.IO.FileStream fs = new System.IO.FileStream(pathName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

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

fs.Read(buffByte,0,(int)fs.Length);

fs.Close();

fs = null;

//建立Command命令

string comm = @"Insert into table1(img,name) values(@img,@name)";

this.sqlCommand1 = new System.Data.SqlClient.SqlCommand ();

this.sqlCommand1.CommandType = System.Data.CommandType.Text ;

this.sqlCommand1.CommandText = comm;

this.sqlCommand1.Connection = this.sqlConnection1 ;

//创建Parameter

this.sqlCommand1.Parameters.Add("@img",System.Data.SqlDbType.Image);

this.sqlCommand1.Parameters[0].Value = http://www.jb51.net/ianakin/archive/2012/02/02/buffByte;

this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar);

this.sqlCommand1.Parameters[1].Value =http://www.jb51.net/ianakin/archive/2012/02/02/pathName.Substring(pathName.LastIndexOf("\\")+1);

try

{

this.sqlConnection1.Open();

this.sqlCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

}

catch(System.Exception ee)

{

MessageBox.Show(ee.Message );

}

buffByte = null;

this.FillListBox();

}

读取:

从数据库读图片到picturebox

SqlConnection conn=new SqlConnection(@"data source=chenyuming2004VSdotNET;uid=sa;pwd=cym;database=lhf");

conn.Open();

SqlCommand cmd=new SqlCommand("select 照片 from fuser where password='1b'",conn);

SqlDataReader reader=cmd.ExecuteReader();

reader.Read();

MemoryStream buf=new MemoryStream((byte[])reader[0]);

Image image=Image.FromStream(buf,true);

pictureBox1.Image=image;

C#保存图片到数据库并读取显示图片的方法的更多相关文章

  1. C#从SQL server数据库中读取l图片和存入图片

    原文:C#从SQL server数据库中读取l图片和存入图片 本实例主要介绍如何将图片存入数据库.将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStr ...

  2. MySQL数据库之插入显示图片

    图书馆系统项目需要用到好多图片,并且要求存入到数据库中,对这个特别感兴趣,于是上网查了资料,采用C#语言,进行了具体实现. 说明: 功能:往MySQL数据库插入并显示图片: 验证:执行插入功能后,我把 ...

  3. Java中显示图片的方法

    最近在做一个swing小项目,其中需要把存储在硬盘中的图片文件显示出来,总结了如下方法: 1. Graphics g = getGraphics();String name = "E:/Ca ...

  4. vue中通过定义的数组循环将img的src引入图片却不显示图片问题解决方法

    需要前端循环图片数组将其放到页面中去. 需要将src渲染到页面中,如果单纯写src的路径会出现不显示图片的问题 因为图片路径在assets,所以需要require一下.

  5. QT+OpenGL读取显示图片,OpenGL在QT里的刷新问题(好几篇)

    GLuint readImage(char *filename){    GLuint tex_id;    GLint alignment; QImage tex, buf;    buf.load ...

  6. Android实例-操作sqlite数据库之Grid显示图片(XE8+小米2)

    结果: 1.数据库文件,记得打包到程序中(assets\internal\). 操作方法: 1.新建firemonkey mobile application①菜单->File->New- ...

  7. OpenCV基础篇之读取显示图片

    程序及分析 /* * FileName : read.cpp * Author : xiahouzuoxin @163.com * Version : v1.0 * Date : Tue 13 May ...

  8. android打开,保存图片到sd卡,显示图片

    1.打开根目录下test.jpg Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getA ...

  9. 解决CHROME中画布中无法显示图片的方法

    最终效果图如下 我按照W3SCHOOL里面的方法,代码如下 <!DOCTYPE html> <html> <body> <script type=" ...

随机推荐

  1. nginx+uwsgi01---flask部署

    1.uwsgi.ini [uwsgi] # uwsgi 启动时所使用的地址与端口注意socket与http不同,http可以被外网访问0. socket = # 指向网站目录 chdir = /hom ...

  2. POJ1056 IMMEDIATE DECODABILITY & POJ3630 Phone List

    题目来源:http://poj.org/problem?id=1056   http://poj.org/problem?id=3630 两题非常类似,所以在这里一并做了. 1056题目大意: 如果一 ...

  3. Java快速IO(ACM)必备

    en.... 无非用到的是 1. new Scanner(System.in); 2.new BUfferReader(new InputStreamReader(System.in); 3.Syst ...

  4. 【学习笔记】jQuery的基础学习

    [学习笔记]jQuery的基础学习 新建 模板 小书匠  什么是jQuery对象? jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果 ...

  5. Diophantus of Alexandria

    Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...

  6. A. Minimizing the String

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  7. UDP通讯模型简单示例

    1. UDP通讯模型 2. 服务器端 ① 创建一个socket,用函数socket() ② 绑定IP地址.端口等信息到socket上,用函数bind() ③ 循环接收数据,用函数recvfrom() ...

  8. 搭建 Django 平台

    1.使用Pycharm.创建一个Django项目 2.图示 3.在终端输入: 4.结果

  9. 原 tomcat的server.xml配置文件中三个端口的作用

    以Tomcat7.0为例, 在安装目录下. conf/server.xml 中可以配置三个端口号, 如果使用多个tomcat 是需要配置这三个. 该Connector 用于监听请求. protocol ...

  10. 9.JSP进阶

    1.JSP内置对象 JSP容器在_jspService()方法中声明并初始化9个内置对象. 名称 作用 接口/类 out 客户端打开的输出流 javax.servlet.jsp.JspWriter 接 ...