การปรับปรุงข้อมูลใน .mdb ผ่าน microsoft.jet.oledb
คำแนะนำ
Source Code สำหรับจัดการข้อมูลในแฟ้ม c:\dthai97.mdb
ผ่าน listbox textbox และ button ในฟอร์มที่สร้างผ่าน design time

+ dthai97.mdb 70 KB
(tbthai : fid int, fname string, fsalary double)
Imports system.data.oledb
Public Class Form1
    Dim strdb As String = "c:\dthai97.mdb"
    Dim strconn As String = "provider=microsoft.jet.oledb.4.0;data source=" & strdb
    Dim strsql As String
    Dim cn As New System.Data.OleDb.OleDbConnection(strconn)
    Dim cm As New OleDbCommand(strsql, cn)
    Dim dr As OleDbDataReader
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        read_mdb()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        If (ListBox1.SelectedIndex.ToString <> -1) Then
            Dim s As String = ListBox1.Items(ListBox1.SelectedIndex.ToString)
            Dim ar() As String
            ar = Split(s, ",")
            TextBox1.Text = ar(0)
            TextBox2.Text = ar(1)
            TextBox3.Text = ar(2)
            TextBox4.Text = CInt(TextBox4.Text) + CInt(ar(2))
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button2.Click
        strsql = "insert into tbthai values(" & TextBox1.Text
        strsql &= ",'" & TextBox2.Text & "'"
        strsql &= "," & TextBox3.Text & ")"
        MsgBox(strsql)
        cm = New OleDbCommand(strsql, cn)
        cn.Open()
        dr = cm.ExecuteReader
        cn.Close()
        read_mdb()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button3.Click
        strsql = "delete from tbthai where fid=" & TextBox1.Text
        cm = New OleDbCommand(strsql, cn)
        cn.Open()
        dr = cm.ExecuteReader
        cn.Close()
        read_mdb()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button4.Click
        strsql = "update tbthai set "
        strsql &= "fname='" & TextBox2.Text & "',"
        strsql &= "fsalary=" & TextBox3.Text & " "
        strsql &= "where fid = " & TextBox1.Text
        cm = New OleDbCommand(strsql, cn)
        cn.Open()
        cm.ExecuteReader()
        cn.Close()
        read_mdb()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button5.Click
        Dim s As String = ""
        strsql = "select fid,fname,fsalary from tbthai"
        cm = New OleDbCommand(strsql, cn)
        cn.Open()
        dr = cm.ExecuteReader()
        While dr.Read()
            s &= dr.Item(0) & "," & dr.Item(1) & "," & dr.Item(2) & Chr(10)
        End While
        cn.Close()
        MsgBox(s)
    End Sub
    Sub read_mdb()
        ListBox1.Items.Clear()
        strsql = "select fid,fname,fsalary from tbthai"
        cm = New OleDbCommand(strsql, cn)
        cn.Open()
        dr = cm.ExecuteReader()
        While dr.Read()
            ListBox1.Items.Add(dr.Item(0) & "," & dr.Item(1) & "," & dr.Item(2))
        End While
        cn.Close()
    End Sub
End Class

http://goo.gl/72BPC