Imports System.Data.SqlClient
Dim connstring As String
Dim sql As String
Dim sqlcon As New SqlConnection
Private Sub PopulateImginDataGridView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PopulateImginDataGridView.Click
connstring = "Data Source=localhost;Initial Catalog=Test;User id=sa;password=password;"
sqlcon.ConnectionString = connstring
sqlcon.Open()
DataGridView1.Rows.Clear()
Dim dr As SqlDataReader
Dim sqlcmd As New SqlCommand
Dim imgPath As Image
sql = "Select * From Image_test"
With sqlcmd
.CommandText = sql
.Connection = sqlcon
dr = .ExecuteReader()
End With
If dr.HasRows Then
DataGridView1.ColumnCount = dr.FieldCount - 1
DataGridView1.Columns(0).Name = "ID"
DataGridView1.Columns(1).Name = "Name"
End If
Dim ImgCol As New DataGridViewImageColumn
DataGridView1.Columns.Add(ImgCol)
While dr.Read
Dim imgarray As Byte() = dr("FieldImage")
Dim memorystream As IO.MemoryStream = New IO.MemoryStream(imgarray, False)
imgPath = Image.FromStream(memorystream)
ImgCol.Image = imgPath
ImgCol.HeaderText = "Image"
ImgCol.Name = "ImgCol"
DataGridView1.Rows.Add(dr.Item(0), dr.Item(1), imgPath)
End While
sqlcon.Close()
End Sub
No comments:
Post a Comment