Imports
System.Data.OleDb
Public
Class
Form1
Private
Mcon
As
OleDbConnection =
Nothing
Private
Adapter
As
OleDbDataAdapter =
Nothing
Private
Mcom
As
OleDbCommand =
Nothing
Private
Table
As
DataTable
Private
Sub
Button1_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
Try
Mcon =
New
OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Clients.mdb;Persist Security Info=False"
)
Mcon.Open()
Adapter =
New
OleDbDataAdapter
Mcom =
New
OleDbCommand
Mcom.Connection = Mcon
Mcom.CommandType = CommandType.Text
Mcom.CommandText =
"Select * from Client"
Adapter.SelectCommand = Mcom
Table =
New
DataTable
Adapter.Fill(Table)
DataGridView1.DataSource = Table
MsgBox(
"数据已取回,共"
& Table.Rows.Count &
"条"
)
Catch
ex
As
Exception
MsgBox(ex.Message)
End
Try
End
Sub
Private
Sub
Button2_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button2.Click
Dim
CB
As
OleDbCommandBuilder
Dim
tempDT
As
DataTable
tempDT = Table.GetChanges()
MsgBox(Table.GetChanges.ToString)
If
tempDT
Is
Nothing
Then
MsgBox(
"数据未修改。"
)
Exit
Sub
End
If
CB =
New
OleDbCommandBuilder
CB.DataAdapter = Adapter
Adapter.UpdateCommand = CB.GetUpdateCommand
Adapter.InsertCommand = CB.GetInsertCommand
Adapter.DeleteCommand = CB.GetDeleteCommand
Adapter.Update(Table)
MsgBox(
"数据已更新"
)
End
Sub
End
Class