vbnet同時對兩個ACCESS數據庫進行增刪改查?
見下面的代碼,自己換成真實的數據庫及表即可
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Module Module1
Public cn1 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS數據庫1.mdb") '定義連接1---這里請更改為實際數據庫路徑及名稱
Public cn2 As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\ACCESS數據庫2.mdb") '定義連接2---這里請更改為實際數據庫路徑及名稱
Public DataBaseRST1 As Integer '用來返回數據庫執行結果
Public DataBaseRST2 As Integer '用來返回數據庫執行結果
Public Function DataModify(ByVal str_cmd1 As String, ByVal str_cmd2 As String) As Boolean '進行數據庫修改操作函數
Dim cmdinsert1 As New OleDbCommand
Dim cmdinsert2 As New OleDbCommand
Try
cmdinsert1.CommandText = str_cmd1
cmdinsert2.CommandText = str_cmd2
cmdinsert1.Connection = cn1
cmdinsert2.Connection = cn2
If cn1.State = ConnectionState.Closed Then cn1.Open()
If cn2.State = ConnectionState.Closed Then cn2.Open()
DataBaseRST1 = cmdinsert1.ExecuteNonQuery() '用來返回執行的結果
DataBaseRST2 = cmdinsert2.ExecuteNonQuery() '用來返回執行的結果
cn1.Close()
cn2.Close()
Return True
Catch ex As Exception
MessageBox.Show(Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End Try
End Function
End Module
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataModify("insert into aa values ('1','2')", "insert into aa values ('1','2')") '調用方法
End Sub
End Class