SQL Server中的Image数据类型的操作

来源:互联网 发布:孙中山民族主义 知乎 编辑:程序博客网 时间:2024/05/16 06:19

准备工作,在库Im_Test中建立一张表Im_Info,此表中有两个字段,分别为Pr_Id (INT),Pr_Info (IMAGE),用来存储图形编号及图形信息。其语法如下:

[sql] view plain
  1. CREATE TEALE Im_Info (   
  2. Pr_Id INT NULL ,   
  3. Pr_Info IMAGE NULL   
  4. )   

第一步: 往表中插入一条记录,并初始化PR_INFO字段。其语法如下:

[sql] view plain
  1. INSERT INTO Im_Info VALUES (1 ,0xFFFFFFFF)  

第二步往表中写入图形信息。其语法如下:

[sql] view plain
  1. DECLARE @@ptrval varbinary(16)   
  2. SELECT @@ptrval = TEXTPTR(Pr_Info)   
  3. FROM Im_Info   
  4. WHERE Pr_Id = 1   
  5. WRITETEXT Im_Text.Im_Info   
  6. @@ptrval 0x624fd543fd…..   

其中0x624fd543fd….. 为图形的十六进制数据,可以通过C 、Java等工具获得。

注意在写入图形信息前必须先将此数据库的 'select into/bulkcopy' 属性设置为 True ,其语法如下: 

[sql] view plain
  1. use master   
  2. exec sp_dboption Im_Test ,'select into/bulkcopy' ,True   

 C#读取Image数据类型:

(1)控制台应用程序下演示插入图片

[csharp] view plain
  1. public void InsertIMG()  
  2.         {  
  3.   
  4.             //将需要存储的图片读取为数据流  
  5.             FileStream fs = new FileStream(@"E:\c.jpg", FileMode.Open,FileAccess.Read);  
  6.             Byte[] btye2 = new byte[fs.Length];  
  7.             fs.Read(btye2 , 0, Convert.ToInt32(fs.Length));  
  8.             fs.Close();  
  9.               
  10.             using (SqlConnection conn = new SqlConnection(sqlconnstr))  
  11.             {  
  12.                 conn.Open();  
  13.                 SqlCommand cmd = new SqlCommand();  
  14.                 cmd.Connection = conn;  
  15.                 cmd.CommandText = "insert into T_Img(imgfile) values(@imgfile)";  
  16.                 SqlParameter par = new SqlParameter("@imgfile", SqlDbType.Image);  
  17.                 par.Value = bt;  
  18.                 cmd.Parameters.Add(par);  
  19.   
  20.                 int t=(int)(cmd.ExecuteNonQuery());  
  21.                 if (t > 0)  
  22.                 {  
  23.                     Console.WriteLine("插入成功");  
  24.                 }  
  25.                 conn.Close();  
  26.             }  
  27.         }  

(2)控制台应用程序下读出并生成图片到物理位置

[csharp] view plain
  1. public void Read()  
  2.         {  
  3.             byte[] MyData = new byte[0];  
  4.             using (SqlConnection conn = new SqlConnection(sqlconnstr))  
  5.             {  
  6.                 conn.Open();  
  7.                 SqlCommand cmd = new SqlCommand();  
  8.                 cmd.Connection = conn;  
  9.                 cmd.CommandText = "select * from T_img";  
  10.                 SqlDataReader sdr = cmd.ExecuteReader();  
  11.                 sdr.Read();  
  12.                 MyData = (byte[])sdr["ImgFile"];//读取第一个图片的位流  
  13.                 int ArraySize= MyData.GetUpperBound(0);//获得数据库中存储的位流数组的维度上限,用作读取流的上限  
  14.   
  15.                 FileStream fs = new FileStream(@"c:\00.jpg", FileMode.OpenOrCreate, FileAccess.Write);  
  16.                 fs.Write(MyData, 0, ArraySize);  
  17.                 fs.Close();   //-- 写入到c:\00.jpg。  
  18.                 conn.Close();  
  19.                 Console.WriteLine("读取成功");//查看硬盘上的文件  
  20.             }  
  21.         }  

(3)Web下picshow.aspx页将图片读取出来并写入到浏览器上呈现

[csharp] view plain
  1. public void Read()  
  2.    {  
  3.        byte[] MyData = new byte[0];  
  4.        using (SqlConnection conn = new SqlConnection(sqlconnstr))  
  5.        {  
  6.            conn.Open();  
  7.            SqlCommand cmd = new SqlCommand();  
  8.            cmd.Connection = conn;  
  9.            cmd.CommandText = "select * from T_img";  
  10.            SqlDataReader sdr = cmd.ExecuteReader();  
  11.            sdr.Read();  
  12.            MyData = (byte[])sdr["ImgFile"];  
  13.            Response.ContentType = "image/gif";  
  14.            Response.BinaryWrite(MyData);  
  15.            conn.Close();  
  16.            Response.Write("读取成功");  
  17.        }  

(4)在web中可以如上picshow.aspx页面读取并显示图片,而真正引用该图片时如下示例

[html] view plain
  1. <img src="picshow.aspx" width="500" height="300" />  

(5)Winform下将图片写入到sql数据库image类型字段中的方法和以上方法基本一致,仅区别于可以利用多个对话框来帮助选取存储图片等,各个属性可以方便的利用上

(6)Winform下读取图片在picturebox控件中显示出来

方法一:利用MemoryStream 和System.Drawing.Image

[csharp] view plain
  1. public void Read()  
  2.         {  
  3.             byte[] MyData = new byte[0];  
  4.             using (SqlConnection conn = new SqlConnection(sqlconnstr))  
  5.             {  
  6.                 conn.Open();  
  7.                 SqlCommand cmd = new SqlCommand();  
  8.                 cmd.Connection = conn;  
  9.                 cmd.CommandText = "select * from T_img";  
  10.                 SqlDataReader sdr = cmd.ExecuteReader();  
  11.                 sdr.Read();  
  12.                 MyData = (byte[])sdr["ImgFile"];  
  13.   
  14.                 MemoryStream mystream = new MemoryStream(MyData);  
  15.                 //用指定的数据流来创建一个image图片  
  16.                 System.Drawing.Image img = System.Drawing.Image.FromStream(mystream, true);  
  17.                   
  18.                 System.Windows.Forms.PictureBox picbox = new PictureBox();  
  19.                 picbox.Image = img;  
  20.                 picbox.Left = 30;  
  21.                 picbox.Top = 80;  
  22.                 picbox.Width = 800;  
  23.                 picbox.Height = 500;  
  24.                 this.Controls.Add(picbox);  
  25.   
  26.                 mystream.Close();  
  27.                 conn.Close();  
  28.             }  
  29.         }  

方法二:将流直接读取成图片并写入到物理位置,然后再行利用该图片呈现

[csharp] view plain
  1. void Read()  
  2.         {  
  3.             using (SqlConnection conn = new SqlConnection(sqlconnstr))  
  4.             {  
  5.                 conn.Open();  
  6.                 SqlCommand cmd = new SqlCommand();  
  7.                 cmd.Connection = conn;  
  8.                 cmd.CommandText = "select * from T_img";  
  9.                 SqlDataReader sdr = cmd.ExecuteReader();  
  10.                 sdr.Read();  
  11.   
  12.                 byte[] Image_img = (byte[])sdr["ImgFile"];  
  13.                 if (Image_img.Length == 0)  
  14.                 {  
  15.                     return;  
  16.                 }  
  17.   
  18.                 int filelength = Image_img.Length;  
  19.                 string imageName = "1.jpg";  
  20.                 string myUrl = Environment.CurrentDirectory + "\\" + imageName;  
  21.                 FileStream fs = new FileStream(myUrl, FileMode.OpenOrCreate,FileAccess.Write);  
  22.                 BinaryWriter BW = new BinaryWriter(fs);  
  23.                 BW.BaseStream.Write(Image_img, 0, filelength);  
  24.                 BW.Flush();  
  25.                 BW.Close();  
  26.                 System.Windows.Forms.PictureBox picbox = new PictureBox();  
  27.                   
  28.                 //为picbox添加图片方法一  
  29.                 //picbox.ImageLocation = myUrl;  
  30.                 //picbox.Width = 800;  
  31.                 //picbox.Height = 300;  
  32.   
  33.    
  34.   
  35.                 //为picbox添加图片方法二  
  36.                 Bitmap bitmap = new Bitmap(myUrl);  
  37.                 picbox.Width = 100;//bitmap.Width;  
  38.                 picbox.Height = 80;//bitmap.Height;  
  39.                 picbox.Image = (Image)bitmap;  
  40.                 picbox.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;  
  41.                 picbox.Left = 20;  
  42.                 picbox.Top = 30;  
  43.   
  44.                 this.Controls.Add(picbox);  
  45.                 conn.Close();                  
  46.             }  
0 0