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

来源:互联网 发布:sim800 网络连接失败 编辑:程序博客网 时间:2024/06/05 09:29

该例子是一个对SQL Server数据类型的一个操作例子,具有写入、读取功能。

1:准备数据库

1)创建数据库 Test

2)创建表 Table_1 (分别有2个字段:id(Int)photo(Image)

如图:




2:用C#进行读写操作,完整代码如下:

[csharp] view plain copy
 print?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10. using System.IO;  
  11.   
  12. namespace imageTest  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         public Form1()  
  17.         {  
  18.             InitializeComponent();  
  19.         }  
  20.   
  21.         private void btn_brewse_Click(object sender, EventArgs e)  
  22.         {  
  23.             OpenFileDialog op = new OpenFileDialog();  
  24.             op.Title = "浏览图像文件";  
  25.             op.Filter = "图像文件(*.jpg)|*.jpg";  
  26.             op.ShowDialog();  
  27.             txt_ImageAddress.Text = op.FileName;  
  28.         }  
  29.   
  30.         private void btn_Insert_Click(object sender, EventArgs e)  
  31.         {  
  32.             FileStream fs = new FileStream(txt_ImageAddress.Text,FileMode.Open,FileAccess.Read);  
  33.             byte[] byteArray = new byte[fs.Length];  
  34.             fs.Read(byteArray,0,Convert.ToInt32(fs.Length));  
  35.             fs.Close();  
  36.   
  37.             string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";  
  38.             SqlConnection conn = new SqlConnection(connectionStr);  
  39.             conn.Open();  
  40.             SqlCommand cmd = new SqlCommand("INSERT INTO Table_1(photo) VALUES(@photo)",conn);  
  41.             SqlParameter parmeter = new SqlParameter("@photo", SqlDbType.Image);  
  42.             parmeter.Value = byteArray;  
  43.             cmd.Parameters.Add(parmeter);  
  44.             int result = cmd.ExecuteNonQuery();  
  45.             if (result > 0)  
  46.                 MessageBox.Show("插入成功");  
  47.             conn.Close();  
  48.         }  
  49.   
  50.         private void btn_ReadImage_Click(object sender, EventArgs e)  
  51.         {  
  52.             string connectionStr = "Server=.;Database=Test;Uid=sa;Pwd=123456";  
  53.             SqlConnection conn = new SqlConnection(connectionStr);  
  54.             conn.Open();  
  55.             SqlCommand cmd = new SqlCommand("SELECT * FROM Table_1",conn);  
  56.             SqlDataReader dr = cmd.ExecuteReader();  
  57.             dr.Read();  
  58.             byte[] image = (byte[])dr["photo"];  
  59.             conn.Close();  
  60.             if (image.Length == 0)  
  61.                 return;  
  62.             string photoUrl = Environment.CurrentDirectory + "\\1.jpg";  
  63.             FileStream fs = new FileStream(photoUrl, FileMode.OpenOrCreate, FileAccess.Write);  
  64.             BinaryWriter bw = new BinaryWriter(fs);  
  65.             bw.BaseStream.Write(image, 0, image.Length);  
  66.             bw.Flush();  
  67.             bw.Close();  
  68.   
  69.             picbox_image.ImageLocation = photoUrl;  
  70.         }  
  71.   
  72.     }  
  73. }  


效果如图:


完整项目(包含数据库文件)下载地址:http://download.csdn.net/source/3576866

0 0