我试图同时将数据插入所有这三个表中。我有一个数据库类,我称之为。我的代码如下:
'Add Parameters
DBAccess.AddParam("@clientID", txtID.Text)
DBAccess.AddParam("@firstName", txtName.Text)
DBAccess.AddParam("@lastName", txtLastName.Text)
DBAccess.AddParam("@cell", txtCell.Text)
DBAccess.AddParam("@homeTel", txtHomeTel.Text)
DBAccess.AddParam("@workTel", txtWorkTel.Text)
DBAccess.AddParam("@email", txtEmail.Text)
DBAccess.AddParam("@homeAddress", txtHomeAddress.Text)
DBAccess.AddParam("@postalAddress", txtPostalAddress.Text)
DBAccess.AddParam("@code", txtCode.Text)
'Execute Insert Command
DBAccess.ExecQuery("INSERT INTO CLIENT ([Client_ID], [First_Name], [Last_Name]) "
& VALUES (@clientID, @firstName, @lastName); ")
DBAccess.ExecQuery("INSERT INTO CONTACTS (Client_ID.C_TEL_H, Client_ID.C_TEL_W, Client_ID.C_Cell, Client_ID.C_Email) " &
"VALUES (@homeTel, @workTel, @cell, @email, SELECT @clientID FROM CLIENT); ")
'DBAccess.ExecQuery("INSERT INTO ADDRESS ([Client_ID], [Residential_A], [Postal_A], [P_Code]) " &
' "VALUES (@clientID, @homeAddress, @postalAddress, @code);
数据库类
Imports System.Data.OleDb
Public Class DatabaseControl
'Database connection
Private DBConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=C:\Users\lance\Documents\BophelongCare.accdb;")
'Prepare DB Command
Private DBCommand As OleDbCommand
'DB Data
Public DBDAdapter As OleDbDataAdapter
Public DBDTable As DataTable
'Query Parameters
Public Params As New List(Of OleDbParameter)
'Query Statistics
Public RecordCount As Integer
Public Exception As String
Public Sub ExecQuery(Query As String)
'Rest query Statistics
RecordCount = 0
Exception = ""
Try
'Open Connection
DBConnection.Open()
'Create DB Command
DBCommand = New OleDbCommand(Query, DBConnection)
'Load Parameters Into DB Command
Params.ForEach(Sub(p) DBCommand.Parameters.Add(p))
'Clear Parameter List
Params.Clear()
'Execute Command & Fill DataTable
DBDTable = New DataTable
DBDAdapter = New OleDbDataAdapter(DBCommand)
RecordCount = DBDAdapter.Fill(DBDTable)
Catch ex As Exception
Exception = ex.Message
End Try
'Close Connection
If DBConnection.State = ConnectionState.Open Then DBConnection.Close()
End Sub
' Include query & Command Parameters
Public Sub AddParam(Name As String, Value As Object)
Dim NewParam As New OleDbParameter(Name, Value)
Params.Add(NewParam)
End Sub
End Class
请协助