2016-02-29 70 views
0

我使用此代碼來檢索我的圖片,它正常工作與一個簡單的表,只包含blob,但是當我試圖適應它爲我的表用戶containt(cin,nom, prenom ....,圖像)異常指示從MySQL數據庫檢索blob圖片c#

「Paramétre非有效」(不是一個有效參數

 int bufferSize = 1000; 
     try 
     { 
      string SQL = "Select image from user "; 

      MySqlCommand cmd = new MySqlCommand(SQL, db.Connection); 
      MySqlDataAdapter da = new MySqlDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "image"); 
      int c = ds.Tables["image"].Rows.Count; 
      db.CloseConnection(); 

      if (c > 0) 
      { 
       Byte[] byteBLOBData = new Byte[bufferSize]; 
       byteBLOBData = (Byte[])(ds.Tables["image"].Rows[c - 1]["image"]); 
       MemoryStream stmBLOBData = new MemoryStream(byteBLOBData); 

       pictureBox1.Image = Image.FromStream(stmBLOBData); 
       MessageBox.Show("bien chargée"); 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Connection Error!\n" + ex.Message, "Error Message", 
        MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 
+1

在哪一行出現異常? – Gusman

+0

我認爲你不必填寫數據集,但直接使用SqlDataReader –

+0

如果我刪除捕獲我沒有得到錯誤,沒有結果... –

回答

0

試試這個...

DataTable userTable; 
DataTable ds; 

int cin; 
string nom; 
string prenom; 
Byte[] ImageByte; 

userTable = ds; 
if (userTable == null) 
    return false; 
else 
{ 
    if (userTable.Rows.Count > 0) 
    { 
     foreach (DataRow userRow in userTable.Rows) 
     { 
      cin = Convert.ToInt32(userRow["cin"]); 
      nom = userRow["nom"].ToString(); 
      prenom = userRow["prenom"].ToString(); 
      ImageByte = (Byte[])(userRow["image"]); 
     } 
    } 
} 
if (ImageByte != null) 
{ 
    // You need to convert it in bitmap to display the imgage 
    pictureBox1.Image = ByteToImage(ImageByte); 
    pictureBox1.Refresh(); 
} 

public static Bitmap ByteToImage(byte[] blob) 
{ 
    MemoryStream mStream = new MemoryStream(); 
    byte[] pData = blob; 
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length)); 
    Bitmap bm = new Bitmap(mStream, false); 
    mStream.Dispose(); 
    return bm; 

} 
+0

謝謝,但我只需要一個blob圖片到picturebox –

+0

請檢查我更新的答案。 您需要將您的字節數組轉換爲位圖。請檢查** ByteToImage(byte [] byteArray)**方法 – Riz

+0

它仍然不工作! :( –

0
byteBLOBData = ((Byte[])ds.Tables["image"].Rows[c - 1]["image"]); 

這應該解決它。

+1

你可以在你的代碼中添加一些解釋,而不是隻發佈答案嗎?試着幫助人們明確哪些部分是錯的,爲什麼錯了,以及爲什麼你的解決方案是他們需要應用的。 – LordWilmore