我有在查看故障或从数据库中显示的图像(MySQL的),以datagriview麻烦在显示图像的datagridview
我的数据库中的表,我试图找回被命名为样品与领域ID = INT(10),小学,自动增加和IMG = BLOB
任何人谁可以帮助我?这将是非常赞赏
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
以下是我的代码保存在数据库中的图像。但它不保存任何东西。我想从DataGrid中获取图像,然后将其保存到数据库中
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
首先,你不应该在你的数据库中存储图像。您可以将它们存储在服务器上,然后使用映像的位置(将映像的存储位置存储在数据库中)进行引用。你能不能显示你的aspx? –
@AmneshGoel我一直在使用longblob保存和检索数据库中的图像。我还没有看到你之前说过的任何引用,所以我将它保存在数据库中。我没有任何问题,但现在我需要同步我的数据库,我遇到了这样的问题:( –
@AmneshGoel,因为你说的很容易数据丢失,我没有这样做 –