2011-12-13 62 views
2

我在C#中有一個客戶端應用程序,其目的是獲取圖像的位置(DataType: VarChar)。然後,該應用程序應該調用一個Web服務,該服務又將圖像(不是位置)存儲在本地MySQL數據庫中。通過C#應用程序在MySQL中保存圖像

問題是,我意識到我能夠將所有其他數據從表單傳遞到數據庫除了圖像......任何人都可以請指出我在做什麼錯在這裏?任何幫助將不勝感激。

ps - 我知道很多人會建議我將圖像保存在文件系統中,而不是數據庫本身上....但事實是,我的數據庫不是那麼大,所以保存數據庫本身上的圖像不會是一個大問題的希望:)

這是我在MySQL表,

create table testImage(
id int not null auto_increment, 
name varchar(50), 
age int, 
image blob, 
primary key(id)); 

這裏是這是爲了將數據插入到表中的WebMethod。當image字段被註釋掉時,它可以工作。

[WebMethod] 
     public string sendDataToMySql(string get_name, int get_age, byte[] buffer) 
     { 
      string MyConString = "SERVER=localhost;" + 
        "DATABASE=test;" + 
        "UID=root;" + 
        "PASSWORD=password;"; 

      string name_new = get_name; 
      int age_new = get_age; 
      byte[] buffer_new = buffer; 


      MySqlConnection connection = new MySqlConnection(MyConString); 
      connection.Open(); 
      MySqlCommand command = new MySqlCommand("", connection); 
      command.CommandText = "insert into testdata(name, age, image) values(@name, @age, @image);"; 

      command.Parameters.AddWithValue("@name", name_new); 
      command.Parameters.AddWithValue("@age", age_new); 
      command.Parameters.AddWithValue("@image", buffer_new); 

      command.ExecuteNonQuery(); 

      connection.Close(); 

      return "Task Performed!"; 

     } 
+0

Blob存儲,讓我仔細檢查,還你怎麼生成的字節數據..需要看到你這樣做是爲了確保您生成的byte []正確 – MethodMan 2011-12-13 20:12:36

+0

代碼圖像存儲爲BLOB ... – BurninatorDor 2011-12-13 20:13:18

回答

4

我不認爲你需要在所有申報buffer_new變量,你可以簡單地使用buffer參數,因爲它是。

我的猜測是,你應該MySql.Data.MySqlClient.MySqlDbType.Blob數據類型分配給@Image參數不只是AddWithValue ...

在這裏獲得一個完整的例子:Insert blob into MySQL

+0

謝謝!鏈接是一個很好的幫助!但我對某事感到困惑。該鏈接上給出的最後一個代碼片段....爲什麼這是必要的?另外,我如何引用`FileToArray`,`MySQL_File_Save`和`MimeType` ...? – BurninatorDor 2011-12-13 20:53:11

0
System.IO.BufferedStream bf = new BufferedStream(httpFile.InputStream); 
byte[] buffer = new byte[bf.Length]; 
bf.Read(buffer,0,buffer.Length);  

然後傳遞緩衝到您的輸入語句。數據類型需要是BLOB來處理它。 您還需要獲取mime類型,並對您要支持的MIME類型進行過濾,否則可能會遇到數據庫中遇到的各種問題。

private string getFileExtension(string mimetype) 
{ 
    mimetype = mimetype.Split('/')[1].ToLower(); 
    Hashtable hTable = new Hashtable(); 
    hTable.Add("pjpeg","jpg"); 
    hTable.Add("jpeg","jpg"); 
    hTable.Add("gif","gif"); 
    hTable.Add("x-png","png"); 
    hTable.Add("bmp","bmp"); 

    if(hTable.Contains(mimetype)) 
    { 
     return (string)hTable[mimetype]; 
    } 
    else 
    { 
     return null; 
    }   
} 
0

BLOB是hexagecimal值

http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-literals.html

的一種方式是通過將字節數組中插入查詢一個十六進制字符串而不使用Parameters.Add( 「@ Fileimage」 MySqlDbType.MediumBlob);

你可能需要做的BinaryWriter
 MySqlConnection con = new MySqlConnection("Server=localhost;Database=bd_prueba;Uid=root;Pwd=Intel-IT;"); 
     FileStream fs = new FileStream(@"D:\proyectos2.jpg", FileMode.Open, FileAccess.Read); 

     byte[] rawData = new byte[fs.Length];   
     fs.Read(rawData, 0, (int)fs.Length); 
     fs.Close(); 
     //byte[] to HEX STRING 
     string hex = BitConverter.ToString(rawData); 
     //'F3-F5-01-A3' to 'F3F501A3' 
     hex = hex.Replace("-", ""); 

     con.Open(); 
     //Standart VALUE HEX x'F3F501A3' 
     string SQL = @"INSERT INTO tabla(id,fileimage) VALUES ('stringhex',x'"+hex+"')"; 
     MySqlCommand cmd = new MySqlCommand(); 
     cmd.Connection = con;    
     cmd.CommandText = SQL; 
     cmd.ExecuteNonQuery(); 

     con.Close(); 
相關問題