C#讀取Mysql blob字段 (轉帖)

http://blog.csdn.net/config_man/article/details/6123191mysql

開發環境:Windows XP Professional SP三、VS200八、Winform、MySQL5.0、MySQL.Data.dll 6.2.3.0sql

 

一、從硬盤上讀取一圖片,將其轉化爲流,而後存儲到此BLOB字段中數據庫

 

[csharp] view plain copy print ?
  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     byte[] bytes = null;  
  4.     bytes = File.ReadAllBytes(@"C:/Documents and Settings/user/My Documents/My Pictures/11.jpg");  
  5.     using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection())  
  6.     {  
  7.         conn.ConnectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;  
  8.         MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();  
  9.         cmd.CommandText = "insert into test(id,picture) values(@id,@picture)";  
  10.         cmd.CommandType = CommandType.Text;  
  11.         cmd.Parameters.Add("@id", MySql.Data.MySqlClient.MySqlDbType.Int32);  
  12.         cmd.Parameters.Add("@picture", MySql.Data.MySqlClient.MySqlDbType.Blob);  
  13.   
  14.         cmd.Parameters[0].Value = 15;  
  15.         cmd.Parameters[1].Value = bytes;  
  16.         cmd.Connection = conn;  
  17.         conn.Open();  
  18.   
  19.         int affectedrows = cmd.ExecuteNonQuery();  
  20.   
  21.         cmd.Dispose();//此處能夠不用調用,  
  22.         conn.Close();// 離開 using 塊, connection 會自行關閉  
  23.     }  
  24. }  
 

 

 

二、讀取此BLOB字段,將其轉化爲圖片顯示在Picturebox控件上服務器

[csharp] view plain copy print ?
  1. private void button2_Click(object sender, EventArgs e)  
  2. {  
  3.     using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection())  
  4.     {  
  5.         conn.ConnectionString = ConfigurationManager.ConnectionStrings["test"].ConnectionString;  
  6.         conn.Open();  
  7.   
  8.         MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();  
  9.         cmd.CommandType = CommandType.Text;  
  10.         cmd.CommandText = "select id,picture from test where id = 11";  
  11.         cmd.Connection = conn;  
  12.   
  13.         System.Data.Common.DbDataReader reader = cmd.ExecuteReader();  
  14.         byte[] buffer = null;  
  15.         if (reader.HasRows)  
  16.         {  
  17.             reader.Read();  
  18.             long len = reader.GetBytes(1, 0, null, 0, 0);//1是picture  
  19.             buffer = new byte[len];  
  20.             len = reader.GetBytes(1, 0, buffer, 0, (int)len);  
  21.   
  22.             System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);  
  23.             System.Drawing.Image iamge = System.Drawing.Image.FromStream(ms);  
  24.             pictureBox1.Image = iamge;  
  25.         }  
  26.     }  
  27. }  
 

 

 

數據庫相關文件配置在App.config中,若是不用配置文件,能夠寫成:spa

string remote = "Persist Security Info=False;database=數據庫名;server=服務器IP;user id=用戶名;pwd=密碼";.net

而後conn.ConnectionString = remote;便可。code

 

 

後記:orm

 

   以前在.net中用的mysql庫是:MySQLDriverCS,可是一直沒有搞定,並且用官方給的讀取blob字段也失敗。因而改用MySql.Data.dll ,注意Mysql.Data5.0版本不支持讀取Blob字段,因此須要用較高版本,我用的是MySQL.Data.dll 6.2.3.0。server

 

MySql.Data.dll 6.2.3.0下載地址:http://download.csdn.net/source/2968152blog

MySql.Data.dll 5.0.9.0下載地址:http://download.csdn.net/source/2968157

相關文章
相關標籤/搜索