[英]Parameter Not valid” Error in VB.NET While retrive image to picturebox from sql database
[英]“Parameter Not valid” Error in VB.NET While retrive image to picturebox from ms access database
我在VB.NET中使用以下代碼從MS ACCESS數據庫中檢索具有OLE OBJECT類型的圖像字段的特定用戶圖像
Dim strSql As String = ""
'For Image
strSql = "Select pic from emp_tb WHERE userid='" + textbox1.Text + "'"
Dim sqlCmd As New OleDbCommand(strSql, con)
'Get image data from DB
Dim imageData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())
'Initialize image variable
Dim newImage As Image = Nothing
If Not imageData Is Nothing Then
'Read image data into a memory stream
Using ms As New MemoryStream(imageData, 0, imageData.Length)
ms.Write(imageData, 0, imageData.Length)
'Set image variable value using memory stream.
newImage = Image.FromStream(ms, True)
End Using
End If
我將其檢索到圖片框
picturebox1.image=newImage
我的錯誤:
行號300是
newImage = Image.FromStream(ms, True)
但是我得到的錯誤信息是“參數無效” 。請幫助我如何使用參數避免sql注入以及如何解決此錯誤........
RE:嘗試加載圖像時出現“參數無效”錯誤
在Access中打開數據庫文件,然后打開包含要顯示的圖像的表。
如果圖像列顯示描述Long binary data
則圖像已保存為原始二進制數據(有時稱為“ BLOB”),您應該能夠使用現有代碼(或非常接近的代碼)進行填充PictureBox。
但是,如果圖像列顯示的描述類似於“位圖圖像”或“包”,則圖像已存儲為OLE嵌入式對象。 如果您提取原始二進制數據(如您的代碼一樣)並嘗試直接將其轉換為圖像,則您將收到錯誤消息,因為二進制數據包括包圍實際圖像數據的OLE“包裝器”。 (有關更多詳細信息,請在此處查看我的答案。)
關於如何刪除OLE“包裝器”並檢索原始圖像數據,已經進行了許多討論,但是不幸的是,OLE非常復雜,並且(顯然)沒有特別有據可查。 在閱讀了數十個線程之后,共識似乎是:
如果要在Access本身中存儲和檢索圖像,則只需讓Access“做它的事”並為您處理所有OLE復雜性。
如果您將從任何其他應用程序獲取圖像,然后確保你保存為原始二進制數據(即不保存訪問本身內的圖像)的圖像。
試圖涵蓋以上兩種使用情況可能是一個真正的麻煩。 最好只堅持一個。
如果您需要從Access數據庫中提取OLE“包裝的”對象,那么Stephen Lebans的OLEtoDisk實用程序將被證明非常有用。
RE:使用參數化查詢來防止SQL注入(和其他麻煩)
這很容易。 只需將代碼更改為...
strSql = "Select pic from emp_tb WHERE userid=?"
Dim sqlCmd As New OleDbCommand(strSql, con)
sqlCmd.Parameters.AddWithValue("?", textbox1.Text)
'Get image data from DB
Dim imageData As Byte() = DirectCast(sqlCmd.ExecuteScalar(), Byte())
com.CommandText = "Select * from [Admin_House_Head_Boy] where HouseID=" & HouseID.Text
Dim dr As OleDbDataReader = com.ExecuteReader()
dr.Read()
If dr.HasRows Then
Dim data As Byte() = DirectCast(dr("Stud_Pic"), Byte())
Dim ms As New MemoryStream(data)
PictureBox1.Image = Image.FromStream(ms, True)
Else
MsgBox("Record not found")
End If
please solve this error : "Parameter is not valid."
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.