回覆列表
  • 1 # 邢州巔峰數碼

    見下面的程式碼,自己換成真實的資料庫及表即可

    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

  • 中秋節和大豐收的關聯?
  • 一鍵備份系統有什麼用?