2013-09-24 23 views
0

我在MySQL DB中有一個名爲「Foto」的字段。這個領域是一個blob。當我使用SQL Server中,下面的代碼工作:VB.NET:從MySQL DB返回一個圖像(blob)

Dim conn As New MySqlConnection 
    conn.ConnectionString = ConnectionString 
    Dim cmd As New MySqlCommand 
    cmd.Connection = conn 
    conn.Open() 
    cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = '" & IDtxt.ToString & "'" 
    Dim reader As MySqlDataReader 

     reader = cmd.ExecuteReader 
     While reader.Read 
      If (IsDBNull(reader("Foto"))) Then 
       frmCartaIdentitaView.pctImage.Image = Nothing 
      Else 
       Dim byteImage() As Byte = reader("Foto") 
       Dim frmImageView stmFoto As New System.IO.MemoryStream(byteImage) 
       frmImageView.pctImage.Image = Image.FromStream(stmFoto) 
       frmImageView.pctImage.SizeMode = PictureBoxSizeMode.Zoom 
       frmImageView.Show() 
      End If 
     End While 

但現在,我正在使用MySQL,產生以下錯誤:無效的參數。

+0

你的「ID」字段是性格?不是數字? – Steve

+1

我的ID字段是整數。 – Pinturikkio

回答

1

如果你的ID字段是一個整數,看看這是否讓你過去的錯誤。取而代之的是:

cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = '" & IDtxt.ToString & "'" 

試試這個:

cmd.CommandText = "SELECT Foto FROM MyTable WHERE ID = " & ctype(int32,IDtxt).ToString 
+0

這不是問題不幸的 – Pinturikkio

+0

那麼也許你可以發佈什麼行給你的錯誤和堆棧信息。 – Steve

+0

問題在於串聯,而不是您如何驗證/投射ID。最好使用一個int參數,而不是將其轉換爲int –

0

我有同樣的問題。我得到這個錯誤時,BLOB列是空的,然後 我解決了這個辦法: 很抱歉,但我發現這個職位剛纔

Dim conn As New MySqlConnection 
conn.ConnectionString = ConnectionString 
Dim cmd As New MySqlCommand 
cmd.Connection = conn 
conn.Open() 
cmd.CommandText = "SELECT Foto, length(Foto) AS picLen FROM MyTable WHERE ID = '" & IDtxt.ToString & "'" 
Dim reader As MySqlDataReader 

    reader = cmd.ExecuteReader 
    While reader.Read 
     If (reader("picLen "))=0 Then 
      frmCartaIdentitaView.pctImage.Image = Nothing 
     Else 
      Dim byteImage() As Byte = reader("Foto") 
      Dim frmImageView stmFoto As New System.IO.MemoryStream(byteImage) 
      frmImageView.pctImage.Image = Image.FromStream(stmFoto) 
      frmImageView.pctImage.SizeMode = PictureBoxSizeMode.Zoom 
      frmImageView.Show() 
     End If 
    End While 
+0

解決方法如何?這與其他原始代碼或其他答案有何不同?此外,這個代碼對SQL注入是開放的,如果'ID'是一個整數,它可以隨機失敗。 –