Imports System.Data.OleDb
Public Class Form1
Dim str As String = "provider=microsoft.ace.oledb.12.0;data source=|datadirectory|\db.accdb"
Dim con As New OleDbConnection(str)
Dim ds As New DataSet
Private Sub gd()
If con.State = ConnectionState.Open Then
con.Close()
End If
ds.Clear()
d1.DataSource = Nothing
d2.DataSource = Nothing
d3.DataSource = Nothing
Dim da1 As New OleDbDataAdapter("select * from tb1", con)
Dim da2 As New OleDbDataAdapter("select * from tb2", con)
Dim da3 As New OleDbDataAdapter("select * from tb3", con)
da1.Fill(ds, "tb1")
da2.Fill(ds, "tb2")
da3.Fill(ds, "tb3")
d1.DataSource = ds.Tables(0)
d2.DataSource = ds.Tables(1)
d3.DataSource = ds.Tables(2)
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If con.State = ConnectionState.Open Then
con.Close()
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
gd()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim cm As New OleDbCommand("select tn from tb1", con)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim dr As OleDbDataReader = cm.ExecuteReader
While dr.Read
Dim sql As New OleDbCommand("select tn from tb2 where tn='" & dr.Item("tn") & "'", con)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim dr2 As OleDbDataReader = sql.ExecuteReader
If dr2.Read = False Then
Dim ins As New OleDbCommand("insert into tb3 (tn) values(@tn)", con)
ins.Parameters.AddWithValue("@tn", dr.Item("tn"))
If con.State = ConnectionState.Closed Then
con.Open()
End If
ins.ExecuteNonQuery()
End If
End While
gd()
End Sub
End Class