sqlserver数据库的image字段存入方法 ,读取
来源:互联网 发布:贾巴里帕克体测数据 编辑:程序博客网 时间:2024/05/12 20:05
存入方法
private void button1_Click(object sender, EventArgs e) { openFileImage.Filter = "*.jpg|*.JPG|*.gif|*.GIF|*.bmp|*.BMP"; if (openFileImage.ShowDialog() == DialogResult.OK) { try { this.pictureBox1.Image = Image.FromStream(this.openFileImage.OpenFile()); string strimg = openFileImage.FileName.ToString(); //获取文件对话框中选定的文件名的字符串,包括文件路径 FileStream fs = new FileStream(strimg, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); imgBytesIn = br.ReadBytes((int)fs.Length); //fs.Length文件流的长度,用字节表示 //从当前流中将count个字节读入字节数组中 //打开数据库 SqlConnection con = new SqlConnection("server=192.168.1.164,1433;uid=sa;pwd=mas_lxw;database=Relocation_DT"); con.Open(); SqlCommand cmd = new SqlCommand("update gytd set xytp=@Image", con); cmd.Parameters.Add("@Image", SqlDbType.Image); cmd.Parameters["@Image"].Value = imgBytesIn; cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("图片上传成功"); } catch { MessageBox.Show("您选择的图片不能被读取或文件类型不对!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning); this.pictureBox1.Image = null; } } }
读取:http://www.cnblogs.com/samcn/articles/1275072.html
准备工作,在库Im_Test中建立一张表Im_Info,此表中有两个字段,分别为Pr_Id (INT),Pr_Info (IMAGE),用来存储图形编号及图形信息。其语法如下: CREATE TEALE Im_Info ( Pr_Id INT NULL , Pr_Info IMAGE NULL ) 第一步: 往表中插入一条记录,并初始化PR_INFO字段。其语法如下: INSERT INTO Im_Info VALUES (1 ,0xFFFFFFFF) 第二步往表中写入图形信息。其语法如下: DECLARE @@ptrval varbinary(16) SELECT @@ptrval = TEXTPTR(Pr_Info) FROM Im_Info WHERE Pr_Id = 1 WRITETEXT Im_Text.Im_Info @@ptrval 0x624fd543fd….. 其中0x624fd543fd….. 为图形的十六进制数据,可以通过C 、Java等工具获得。 注意在写入图形信息前必须先将此数据库的 'select into/bulkcopy' 属性设置为 True ,其语法如下: use master exec sp_dboption Im_Test ,'select into/bulkcopy' ,True C#读取Image数据类型: (1)控制台应用程序下演示插入图片public void InsertIMG() { //将需要存储的图片读取为数据流 FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read); Byte[] btye2 = new byte[fs.Length]; fs.Read(btye2 , 0, Convert.ToInt32(fs.Length)); fs.Close(); using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)"; SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image); par.Value = bt; cmd.Parameters.Add(par); int t=(int)(cmd.ExecuteNonQuery()); if (t > 0) { Console.WriteLine("插入成功"); } conn.Close(); } }(2)控制台应用程序下读出并生成图片到物理位置public void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"];//读取第一个图片的位流 int ArraySize= MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限 FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write); fs.Write(MyData, 0, ArraySize); fs.Close(); //-- 写入到c:\00.jpg。 conn.Close(); Console.WriteLine("读取成功");//查看硬盘上的文件 } }(3)Web下picshow.aspx页将图片读取出来并写入到浏览器上呈现 public void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"]; Response.ContentType = "image/gif"; Response.BinaryWrite(MyData); conn.Close(); Response.Write("读取成功"); }(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例<img src="picshow.aspx" width="500" height="300" /> (5)Winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上(6)Winform下读取图片在picturebox控件中显示出来方法一:利用MemoryStream 和System.Drawing.Imagepublic void Read() { byte[] MyData = new byte[0]; using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); MyData = (byte[])sdr["ImgFile"]; MemoryStream mystream = new MemoryStream(MyData); //用指定的数据流来创建一个image图片 System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true); System.Windows.Forms.PictureBox picbox = new PictureBox(); picbox.Image = img; picbox.Left = 30; picbox.Top = 80; picbox.Width = 800; picbox.Height = 500; this.Controls.Add(picbox); mystream.Close(); conn.Close(); } } 方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现void Read() { using (SqlConnection conn = new SqlConnection(sqlconnstr)) { conn.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from T_img"; SqlDataReader sdr = cmd.ExecuteReader(); sdr.Read(); byte[] Image_img = (byte[])sdr["ImgFile"]; if (Image_img.Length == 0) { return; } int filelength = Image_img.Length; string imageName = "1.jpg"; string myUrl = Environment.CurrentDirectory + "\\" + imageName; FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write); BinaryWriter BW = new BinaryWriter(fs); BW.BaseStream.Write(Image_img, 0, filelength); BW.Flush(); BW.Close(); System.Windows.Forms.PictureBox picbox = new PictureBox(); //为picbox添加图片方法一 //picbox.ImageLocation = myUrl; //picbox.Width = 800; //picbox.Height = 300; //为picbox添加图片方法二 Bitmap bitmap = new Bitmap(myUrl); picbox.Width = 100;//bitmap.Width; picbox.Height = 80;//bitmap.Height; picbox.Image = (Image)bitmap; picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; picbox.Left = 20; picbox.Top = 30; this.Controls.Add(picbox); conn.Close(); } }
0 0
- sqlserver数据库的image字段存入方法 ,读取
- VB把文件存入数据库IMAGE字段
- 将图片存入sqlserver中并读取显示的方法
- SqlServer数据库实现图片存入与读取
- 如何操作sqlserver 数据库的image字段[C#]
- C#将图片存入SQLServer数据库并读取
- 从数据库中字段为byte的图片读取处理存入磁盘
- 使用textcopy对sqlserver的Image字段进行读取和写入
- 将带特殊串单引号存入sqlserver的某个字段
- sqlserver image字段默认值
- 数组类型存入数据库字段及通过数组查询的实现方法(PHP)
- 如何将图片以Image类型存储到数据库 如何从数据库中读取Image类型的字段
- 二进制存入读取数据库
- .Net Winform和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法
- .Net Winform和.Net webform(asp.net)中将图片存入sqlserver中并读取显示的方法
- hibernate从sqlserver数据库中读取图片 Image (Blob)
- 从数据库读取image字段到网页(BLOB型的)
- c# sqlserver blod字段的读取
- Docker Swarm
- CRC8查表法中表的具体计算过程
- 检查一个字符串是文本还是二进制
- Intent以及IntentFilter详解
- PHP文件操作(文件移动和复制、删除、重命名)
- sqlserver数据库的image字段存入方法 ,读取
- idea默认情况下从svn checkout项目报错解决方法
- 配置maven及创建maven项目
- eclipse远程服务器dubug
- Android----Intent打开各种文件的方法
- JAVA实例化的本质
- svn的原理和使用
- JavaWeb项目中加入ehcache缓存
- 6.21