Imports System.Data
Imports System.Data.OleDb
Imports System.IO
Public Class Form1
Private _conStr As String = "Provider=Microsoft.Jet.OleDb.4.0;" & "Data Source=C:\Database\Northwind.mdb"
Private Sub showdata()
Dim conn As New OleDbConnection(_constr)
conn.Open()
Dim Sql As String = "SELECT * FROM Categories"
Dim cmd As New OleDbCommand(Sql, conn)
Dim adapter As New OleDbDataAdapter(cmd)
Dim data As New DataSet()
adapter.Fill(data, "cat")
DataGridView1.DataSource = data.Tables("cat")
conn.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
showdata()
End Sub
Private Sub BtnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBrowse.Click
With OpenFileDialog1
.Filter = "Image Files (*.gif;*jpg)/*.gif;*.jpg"
.FileName = ""
If (.ShowDialog() = DialogResult.OK) Then
TbxFile.Text = .FileName
End If
End With
End Sub
Private Sub BtnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnInsert.Click
Dim conn As New OleDbConnection(_conStr)
conn.Open()
Dim sql As String = "INSERT INTO " & _
"Categories(CategoryName, " & _
"Description, Picture) " & _
"VALUES(@name, @descr, @pic)"
Dim cmd As New OleDbCommand(sql, conn)
cmd.Parameters.AddWithValue("name", TbxName.Text)
cmd.Parameters.AddWithValue("descry", TbxDescr.Text)
Dim fiStream As New FileStream(TbxFile.Text, _
FileMode.Open, _
FileAccess.Read)
Dim binReader As New BinaryReader(fiStream)
Dim pic() As Byte = binReader.ReadBytes(fiStream.Length)
cmd.Parameters.AddWithValue("pic", pic)
Dim afftectedRows As Integer = cmd.ExecuteNonQuery()
If (afftectedRows = -1) Then
MsgBox("เกิดข้อความผิดพลาด!" & vbNewLine & "ไม่สามารถจัดเก็บข้อมูลได้")
End If
conn.Close()
showdata()
End Sub
End Class