[英]Trouble in displaying an image to datagridview
I'm having a trouble in viewing or displaying an image from the database (mysql) to datagriview 我在查看或显示从数据库(mysql)到datagriview的图像时遇到问题
The table in my database that I'm trying to retrieve is named as sample with fields ID = Int(10), primary, auto increment and IMG = blob 我尝试检索的数据库中的表被命名为示例 ,其字段ID = Int(10),primary,自动递增且IMG = blob
Anyone who can help me with this? 有人可以帮助我吗? It will be so much appreciated
将不胜感激
Sub getData()
Try
Dim Sql = "Select ID, IMG from sample"
connectionOn()
Dim cmd = New MySqlCommand(Sql, ConOn)
Dim dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
DataGridView1.Rows.Clear()
While dr.Read = True
Dim mybytearray As Byte() = dr.Item("IMG")
Dim myimage As Image
Dim ms As System.IO.MemoryStream = New System.IO.MemoryStream(mybytearray)
myimage = System.Drawing.Image.FromStream(ms)
DataGridView1.Rows.Add(dr(0), myimage)
End While
ConOn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Below is my code saving the image in the database. 下面是我的代码将图像保存在数据库中。 But it doesn't save anything.
但这并不能节省任何东西。 I want to get the image from the datagrid then save it to the database
我想从数据网格中获取图像,然后将其保存到数据库中
Try
connectionSync()
Dim a, b As String
Dim Sql = "INSERT INTO SAMPLE (ID, IMG)values(@a,@b)"
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
a = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()
Dim cmd As New MySqlCommand(Sql, ConSync)
Dim memorystream1 As New MemoryStream()
Dim filename As String = DataGridView1.Rows(i).Cells(1).Value
Dim bitmaps As New Bitmap(filename)
bitmaps.Save(memorystream1, Imaging.ImageFormat.Jpeg)
Dim pic() As Byte = memorystream1.GetBuffer()
cmd.Parameters.AddWithValue("@a", a)
cmd.Parameters.AddWithValue("@b", bitmaps)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
Next
ConSync.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Since MySql BLOB Type is used to stored SqlServer IMAGE Type
I think you can adapt this code using MySql
classes (ie: MySqlDataAdapter
instead of SqlDataAdapter
): 由于MySql BLOB类型用于存储
SqlServer IMAGE Type
我认为您可以使用MySql
类(即: MySqlDataAdapter
而不是SqlDataAdapter
)来修改此代码:
Try
Me.DataGridView1.DataSource = Nothing
Dim dgvID As New DataGridViewTextBoxColumn
dgvID.DataPropertyName = "ID"
Dim dgvIMG As New DataGridViewImageColumn
dgvIMG.DataPropertyName = "IMG"
Me.DataGridView1.Columns.Add(dgvID)
Me.DataGridView1.Columns.Add(dgvIMG)
connectionOn()
Dim cmdSample As SqlCommand = ConOn.CreateCommand
cmdSample.CommandText = "SELECT ID, " _
& "IMG " _
& "FROM Sample"
Dim dtSample As New DataTable
Dim daSample As New SqlDataAdapter(cmdSample)
daSample.Fill(dtSample)
Me.DataGridView1.DataSource = dtSample
ConOn.Close()
ConOn.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.