OP 中提供的信息相当有限。正如您所说,由于您计划继续使用依赖于 DBStatus 中信息的旧版应用程序,因此有必要使这些信息保持最新,并在您执行操作时防止更新。在不了解有关表和表关系的更多信息的情况下,使用事务可能会起作用 - 如果事务中的任何命令失败,则任何更改都将回滚(撤消)。可能有其他方法可以实现您的目标,但这需要有关您的表(和数据库关系)的更多信息。
尝试以下方法,看看是否满足您的需求:
注意:您必须添加在创建订单时更新其他表格的代码。
Public Function CreateOrder() As Integer
'ToDo: add desired code
Dim invoiceNumber As Double = 0
Dim paymentNumber As Double = 0
Dim rowsAffected As Integer = 0
Try
'create new instance
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
'create transaction
Using trx As OleDbTransaction = cn.BeginTransaction()
Try
Try
Using cmd As OleDbCommand = New OleDbCommand("SELECT [Invoice Number], [Payment Number] from DBStatus;", cn, trx)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While reader.Read()
'read by position in SQL statement
'If reader(0) IsNot Nothing AndAlso Not IsDBNull(reader(0)) Then
'invoiceNumber = CDbl(reader(0))
'End If
'If reader(1) IsNot Nothing AndAlso Not IsDBNull(reader(1)) Then
'paymentNumber = CDbl(reader(1))
'End If
'read by column name
If reader("Invoice Number") IsNot Nothing AndAlso Not IsDBNull(reader("Invoice Number")) Then
invoiceNumber = CDbl(reader("Invoice Number"))
End If
If reader("Payment Number") IsNot Nothing AndAlso Not IsDBNull(reader("Payment Number")) Then
paymentNumber = CDbl(reader(1))
End If
End While
End If
End Using
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 1) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 1) - " & ex.Message)
Throw ex 're-throw exception
End Try
'increment values
invoiceNumber += 1
paymentNumber += 1
'update DBStatus - Invoice Number and Payment Number
Try
Using cmd As OleDbCommand = New OleDbCommand("UPDATE DBStatus set [Invoice Number] = ?, [Payment Number] = ?", cn, trx)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
With cmd.Parameters
.Add("@invoiceNumber", OleDbType.Double).Value = invoiceNumber
.Add("@paymentNumber", OleDbType.Integer).Value = paymentNumber
End With
'Debug.WriteLine(cmd.CommandText)
'ToDo: remove the following code that is for debugging
'For Each p As OleDbParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 2) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 2) - " & ex.Message)
Throw ex 're-throw exception
End Try
Try
'ToDo: add code to insert order information
Dim sqlText As String = String.Empty
If Not String.IsNullOrEmpty(sqlText) Then
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn, trx)
End Using
End If
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 3) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 3) - " & ex.Message)
Throw ex 're-throw exception
End Try
'successful
'commit transaction, if not commited, any changes (inserts/updates) will be undone
trx.Commit()
Catch ex As OleDbException
'rollback transaction
trx.Rollback()
Debug.WriteLine("Error (CreateOrder - OleDbException trx) - " & ex.Message & "; Transaction rolled back.")
Throw ex
Catch ex As Exception
'rollback transaction
trx.Rollback()
Debug.WriteLine("Error (CreateOrder trx) - " & ex.Message & "; Transaction rolled back.")
Throw ex
End Try
End Using
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException) - " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder) - " & ex.Message)
Throw ex
End Try
Return invoiceNumber
End Function
用法:
Dim invoiceNumber As Integer = CreateOrder()
这里有一些可能有用的附加代码:
Imports System.Data.OleDb
Public Class HelperAccess2000
Private _accessFilename As String = String.Empty
Private _connectionStr As String = String.Empty
Public Property AccessFilename
Get
Return _accessFilename
End Get
Set(value)
_accessFilename = value
'initialize
Initialize(value)
End Set
End Property
Sub New(accessFilename As String)
'initialize
Initialize(accessFilename)
End Sub
Public Function CreateOrder() As Integer
'ToDo: add desired code
Dim invoiceNumber As Double = 0
Dim paymentNumber As Double = 0
Dim rowsAffected As Integer = 0
Try
'create new instance
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
Using trx As OleDbTransaction = cn.BeginTransaction()
Try
Try
Using cmd As OleDbCommand = New OleDbCommand("SELECT [Invoice Number], [Payment Number] from DBStatus;", cn, trx)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While reader.Read()
'read by position in SQL statement
'If reader(0) IsNot Nothing AndAlso Not IsDBNull(reader(0)) Then
'invoiceNumber = CDbl(reader(0))
'End If
'If reader(1) IsNot Nothing AndAlso Not IsDBNull(reader(1)) Then
'paymentNumber = CDbl(reader(1))
'End If
'read by column name
If reader("Invoice Number") IsNot Nothing AndAlso Not IsDBNull(reader("Invoice Number")) Then
invoiceNumber = CDbl(reader("Invoice Number"))
End If
If reader("Payment Number") IsNot Nothing AndAlso Not IsDBNull(reader("Payment Number")) Then
paymentNumber = CDbl(reader(1))
End If
End While
End If
End Using
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 1) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 1) - " & ex.Message)
Throw ex 're-throw exception
End Try
'increment values
invoiceNumber += 1
paymentNumber += 1
'update DBStatus - Invoice Number and Payment Number
Try
Using cmd As OleDbCommand = New OleDbCommand("UPDATE DBStatus set [Invoice Number] = ?, [Payment Number] = ?", cn, trx)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
With cmd.Parameters
.Add("@invoiceNumber", OleDbType.Double).Value = invoiceNumber
.Add("@paymentNumber", OleDbType.Integer).Value = paymentNumber
End With
'Debug.WriteLine(cmd.CommandText)
'ToDo: remove the following code that is for debugging
'For Each p As OleDbParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 2) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 2) - " & ex.Message)
Throw ex 're-throw exception
End Try
Try
'ToDo: add code to insert order information
Dim sqlText As String = String.Empty
If Not String.IsNullOrEmpty(sqlText) Then
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn, trx)
End Using
End If
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException 3) - " & ex.Message)
Throw ex 're-throw exception
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder 3) - " & ex.Message)
Throw ex 're-throw exception
End Try
'successful
'commit transaction, if not commited, any changes (inserts/updates) will be undone
trx.Commit()
Catch ex As OleDbException
'rollback transaction
trx.Rollback()
Debug.WriteLine("Error (CreateOrder - OleDbException trx) - " & ex.Message & "; Transaction rolled back.")
Throw ex
Catch ex As Exception
'rollback transaction
trx.Rollback()
Debug.WriteLine("Error (CreateOrder trx) - " & ex.Message & "; Transaction rolled back.")
Throw ex
End Try
End Using
End Using
Catch ex As OleDbException
Debug.WriteLine("Error (CreateOrder - OleDbException) - " & ex.Message)
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (CreateOrder) - " & ex.Message)
Throw ex
End Try
Return invoiceNumber
End Function
Public Sub Initialize(accessFilename As String)
'set value
'_connectionStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", accessFilename) 'deprecated
_connectionStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};", accessFilename)
End Sub
Private Sub ExecuteNonQuery(sqlText As String)
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn)
'execute
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Private Function TblDBStatusExecuteNonQuery(sqlText As String, invoiceNumber As Double, paymentNumber As Integer) As Integer
'when specifying value for a string, to pass a null or empty value, specify: "" or Nothing
Dim rowsAffected As Integer = 0
'create new instance
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
'create new instance
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
With cmd.Parameters
.Add("@invoiceNumber", OleDbType.Double).Value = invoiceNumber
.Add("@paymentNumber", OleDbType.Integer).Value = paymentNumber
End With
'ToDo: remove the following code that is for debugging
'For Each p As OleDbParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
Return rowsAffected
End Function
Private Function TblDBStatusExecuteNonQuery(sqlText As String, fileYear As String, archiveYear As Integer, catalogYear As String, salesChanged As DateTime, lastId As Double, invoiceNumber As Double, poNumber As Integer, paymentNumber As Integer, saveTransaction As Boolean, lastInventory As DateTime, nextDeposit As DateTime, nextPastDue As DateTime, nextElectronic As DateTime, nextConference As DateTime, ss As Integer, companyName As String, address1 As String, address2 As String, phone As String, email As String, taxId As String) As Integer
'use for INSERT / UPDATE
'when specifying value for a string, to pass a null or empty value, specify: "" or Nothing
Dim rowsAffected As Integer = 0
'create new instance
Using cn As OleDbConnection = New OleDbConnection(_connectionStr)
'open
cn.Open()
'create new instance
Using cmd As OleDbCommand = New OleDbCommand(sqlText, cn)
'OLEDB doesn't use named parameters in SQL. Any names specified will be discarded and replaced with '?'
'However, specifying names in the parameter 'Add' statement can be useful for debugging
'Since OLEDB uses anonymous names, the order which the parameters are added is important
'if a column is referenced more than once in the SQL, then it must be added as a parameter more than once
'parameters must be added in the order that they are specified in the SQL
'if a value is null, the value must be assigned as: DBNull.Value
With cmd.Parameters
.Add("@fileYear", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(fileYear), DBNull.Value, fileYear)
.Add("@archiveYear", OleDbType.Integer).Value = archiveYear
.Add("@catalogYear", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(catalogYear), DBNull.Value, catalogYear)
.Add("@salesChanged", OleDbType.Date).Value = salesChanged
.Add("@lastId", OleDbType.Double).Value = lastId
.Add("@invoiceNumber", OleDbType.Double).Value = invoiceNumber
.Add("@poNumber", OleDbType.Integer).Value = poNumber
.Add("@paymentNumber", OleDbType.Integer).Value = paymentNumber
.Add("@saveTransaction", OleDbType.Boolean).Value = saveTransaction
.Add("@lastInventory", OleDbType.Date).Value = lastInventory
.Add("@nextDeposit", OleDbType.Date).Value = nextDeposit
.Add("@nextPastDue", OleDbType.Date).Value = nextPastDue
.Add("@nextElectronic", OleDbType.Date).Value = nextElectronic
.Add("@nextConference", OleDbType.Date).Value = nextConference
.Add("@ss", OleDbType.Integer).Value = ss
.Add("@companyName", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(companyName), DBNull.Value, companyName)
.Add("@address1", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(address1), DBNull.Value, address1)
.Add("@address2", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(address2), DBNull.Value, address2)
.Add("@phone", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(phone), DBNull.Value, phone)
.Add("@email", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(email), DBNull.Value, email)
.Add("@taxId", OleDbType.VarWChar).Value = If(String.IsNullOrEmpty(taxId), DBNull.Value, taxId)
End With
'ToDo: remove the following code that is for debugging
'For Each p As OleDbParameter In cmd.Parameters
'Debug.WriteLine(p.ParameterName & ": " & p.Value.ToString())
'Next
'execute
rowsAffected = cmd.ExecuteNonQuery()
End Using
End Using
Return rowsAffected
End Function
Public Function TblDBStatusInsert(invoiceNumber As Double, paymentNumber As Integer) As Integer
'insert Invoice Number and Payment Number into DBStatus
Dim sqlText As String = "INSERT INTO DBStatus([Invoice Number], [Payment Number]) VALUES (?,?);"
Try
'insert
Return TblDBStatusExecuteNonQuery(sqlText, invoiceNumber, paymentNumber)
Catch ex As OleDbException
Debug.WriteLine("Error (TblDBStatusInsert - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblDBStatusInsert) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
End Function
Public Function TblDbStatusGetInvoiceNumberAndPaymentNumber() As DataTable
'get Invoice Number and Payment Number from DBStatus
Dim dt As DataTable = New DataTable()
Dim sqlText As String = "SELECT [Invoice Number], [Payment Number] from DBStatus;"
Try
Using con As OleDbConnection = New OleDbConnection(_connectionStr)
'open
con.Open()
Using cmd As OleDbCommand = New OleDbCommand(sqlText, con)
Using da As OleDbDataAdapter = New OleDbDataAdapter(cmd)
'fill DataTable from database
da.Fill(dt)
End Using
End Using
End Using
Return dt
Catch ex As OleDbException
Debug.WriteLine("Error (TblDbStatusGetInvoiceNumberAndPaymentNumber - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblDbStatusGetInvoiceNumberAndPaymentNumber) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
End Function
Public Function TblDBStatusUpdate(invoiceNumber As Double, paymentNumber As Integer) As Integer
'update Invoice Number and Payment Number in DBStatus
Dim sqlText As String = "UPDATE DBStatus set [Invoice Number] = ?, [Payment Number] = ?;"
Try
'update
Return TblDBStatusExecuteNonQuery(sqlText, invoiceNumber, paymentNumber)
Catch ex As OleDbException
Debug.WriteLine("Error (TblDBStatusUpdate - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblDBStatusUpdate) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
End Function
Public Function TblDBStatusUpdate(fileYear As String, archiveYear As Integer, catalogYear As String, salesChanged As DateTime, lastId As Double, invoiceNumber As Double, poNumber As Integer, paymentNumber As Integer, saveTransaction As Boolean, lastInventory As DateTime, nextDeposit As DateTime, nextPastDue As DateTime, nextElectronic As DateTime, nextConference As DateTime, ss As Integer, companyName As String, address1 As String, address2 As String, phone As String, email As String, taxId As String) As Integer
'update Invoice Number and Payment Number in DBStatus
Dim sqlText As String = "UPDATE DBStatus set [File Year] = ?, [Archive Year] = ?, [Catalog Year] = ?, [Sales Changed] = ?, [Last ID] = ?, [Invoice Number] = ?, [PO Number] = ?, [Payment Number] = ?, [Save Transaction] = ?, [Last Inventory] = ?, [Next Deposit] = ?, [Next Past Due] = ?, [Next Electronic] = ?, [Next Conference] = ?, SS = ?, [Company Name] = ?, Address1 = ?, Address2 = ?, Phone = ?, EMail = ?, [Tax ID] = ?;"
Try
'update
Return TblDBStatusExecuteNonQuery(sqlText, fileYear, archiveYear, catalogYear, salesChanged, lastId, invoiceNumber, poNumber, paymentNumber, saveTransaction, lastInventory, nextDeposit, nextPastDue, nextElectronic, nextConference, ss, companyName, address1, address2, phone, email, taxId)
Catch ex As OleDbException
Debug.WriteLine("Error (TblDBStatusUpdate - OleDbException) - " & ex.Message & "(" & sqlText & ")")
Throw ex
Catch ex As Exception
Debug.WriteLine("Error (TblDBStatusUpdate) - " & ex.Message & "(" & sqlText & ")")
Throw ex
End Try
End Function
End Class
用法:
Private _accessFilename As String = "C:\Temp\TestAccess2000.mdb"
Private _helper As HelperAccess2000 = Nothing
Private Sub InitializeHelper()
If _helper Is Nothing Then
'create new instance
_helper = New HelperAccess2000(_accessFilename)
End If
End Sub
...
InitializeHelper()
Dim rowsAffected As Integer = 0
rowsAffected = _helper.TblDBStatusInsert("1", "3")
rowsAffected = _helper.TblDBStatusUpdate("2", "4")
Dim invoiceNumber As Integer = _helper.CreateOrder()
Dim dt As DataTable = _helper.TblDbStatusGetInvoiceNumberAndPaymentNumber()