手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#保存图片到数据库并读取显示图片的方法
C#保存图片到数据库并读取显示图片的方法
摘要:复制代码代码如下:privatevoidbutton2_Click_1(objectsender,System.EventArgse){st...

复制代码 代码如下:

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#保存图片到数据库并读取显示图片的方法】相关文章:

C#访问应用程序配置文件的方法

C# 获取属性名的方法

C#访问PostGreSQL数据库的方法

C# 将字节流转换为图片的实例方法

gridview 显示图片的实例代码

简介C#读取XML的两种方式

C#中时间的几种格式转换方法

使用VS2005自带的混淆器防止你的程序被反编译的方法

C#操作注册表的方法

基于C#中XmlReader读取Xml的深入分析

精品推荐
分类导航