[英]How can I insert an image from SQL Server onto a button in VB.net?
I am having issues assigning an image to every button according to my database.根据我的数据库,我在为每个按钮分配图像时遇到问题。
My current code:我当前的代码:
Dim strsql As String
Dim ImgSQl As Image
Dim con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=@ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSQl = myreader("Image")
con.Close()
btn1.Image = ImgSQl
This is the error I'm getting:这是我得到的错误:
Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image'
无法将“System.Byte[]”类型的 object 转换为类型“System.Drawing.Image”
All I had to do was dim as Byte, also adding "()" is really important for it to work.我所要做的就是像 Byte 一样暗淡,添加“()”对于它的工作也非常重要。 Hope answering my own question helps a VB.net beginner out there.
希望回答我自己的问题可以帮助 VB.net 初学者。
Dim strsql As String
Dim ImgSql() As Byte
Using con As New SqlConnection("constring")
con.Open()
strsql = "SELECT Image FROM Inventory WHERE ID=@ID"
Dim cmd As New SqlCommand(strsql, con)
ItemID = 1
cmd.Parameters.Add("@ID", SqlDbType.VarChar).Value = ItemID
Dim myreader As SqlDataReader
myreader = cmd.ExecuteReader
myreader.Read()
ImgSql = myreader("Imagen")
Dim ms As New MemoryStream(ImgSql)
btn1.Image = Image.FromStream(ms)
con.Close()
End Using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.