如何在 vb.net 中使用数据库更新图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15052552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:29:47  来源:igfitidea点击:

how to update image using database in vb.net

vb.netwinforms

提问by Rodmar Pusung

what is the problem of this code?? i got a error for the GDI+ and i dont know to solve.

这段代码有什么问题??我的 GDI+ 出现错误,我不知道如何解决。

Private Sub saveEmployee()
   Dim ms As New MemoryStream

   PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)--i got error in this line..A generic error occurred in GDI+.

   Dim arrPic() As Byte = ms.GetBuffer()

   Dim cmdEmp As New MySqlCommand
   Dim sqlEmp As String
   sqlEmp = "update tbl_employee set  emPic=@emPic where emID='" & lbl_empID.Text & "'"

   With cmdEmp
      .Parameters.AddWithValue("@emPic", arrPic)
      .ExecuteNonQuery()
   End With
End Sub

采纳答案by Abdusalam Ben Haj

Try replacing that line with this :

尝试用这个替换该行:

Dim bm as Bitmap = new Bitmap(PictureBox1.Image)
bm.Save(ms, PictureBox1.Image.RawFormat)

The reason could be that the Image is being used by the PictureBox. Its also good practice to have the following instead :

原因可能是 Image 正在被PictureBox. 改为使用以下内容也是一种很好的做法:

Using ms As MemoryStream = New MemoryStream()

   Dim bm as Bitmap = new Bitmap(PictureBox1.Image)
   bm.Save(ms, PictureBox1.Image.RawFormat)

   Dim arrPic() As Byte = ms.GetBuffer()

   Dim cmdEmp As New MySqlCommand
   Dim sqlEmp As String
   sqlEmp = "update tbl_employee set  emPic=@emPic where emID=@emID"

   With cmdEmp
     .Parameters.AddWithValue("@emPic", arrPic)
     .Parameters.AddWithValue("@emID", int.Parse(lbl_empID.Text))
     .ExecuteNonQuery()
   End With

End Using

This way the MemoryStreamwill get Disposedafter being used. And adding @emIDas a parameter is safer.

这样使用后MemoryStream就会得到Disposed。并且添加@emID为参数更安全。

回答by Samuel Darteh

RESOLVED!!! Bind picture box to the datasource field using the Advance tab in the DataBindings section. Use the ImageLocation property instead of image. Then change the Update mode to NEVER. but how abt the update ??? Use a textbox and hide behind the Picture box [with visible = true]. Bind textbox to same datasource. THAT IS!!!

解决!!!使用数据绑定部分中的高级选项卡将图片框绑定到数据源字段。使用 ImageLocation 属性而不是图像。然后将更新模式更改为从不。但是更新怎么样???使用文本框并隐藏在图片框后面 [visible = true]。将文本框绑定到相同的数据源。那是!!!

image displays directly in picture box whilst the textbox TextChanged property handles whether there is UPDATE or Not. took me a while but it works fine now. good luck as u code... ~ SAM, GHANA

图像直接显示在图片框中,而文本框 TextChanged 属性处理是否有更新。我花了一段时间,但现在工作正常。祝你好运,因为你的代码......〜SAM,加纳