为了正常的体验网站,请在浏览器设置里面开启Javascript功能!
首页 > 保存图片到数据库

保存图片到数据库

2018-03-18 3页 doc 15KB 16阅读

用户头像

is_191127

暂无简介

举报
保存图片到数据库保存图片到数据库 //将图像保存到SQL server2000的Image字段中 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.Drawin...
保存图片到数据库
保存图片到数据库 //将图像保存到SQL server2000的Image字段中 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 = buffByte; this.sqlCommand1.Parameters.Add("@name",System.Data.SqlDbType.VarChar); this.sqlCommand1.Parameters[1].Value =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;
/
本文档为【保存图片到数据库】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索