简体   繁体   中英

Trouble in displaying an image to datagridview

I'm having a trouble in viewing or displaying an image from the database (mysql) to 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

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 ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM