现在抛出的错误出现在我的Insert语句中。我无法弄清楚它在哪里。我已经使用该演示来适应我的程序但最终失败了。这是重写的代码。 Imports System.Data.OleDb 公共类Form1
Private data As New DataSet
Private connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TholusizoMoneyS.mdb;User Id=admin;Password=;")
Private WithEvents CustomerDataAdapter As New OleDbDataAdapter("Select * From Customer", connection) With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
Private WithEvents LoanDataAdapter As New OleDbDataAdapter("Select * From Loan", connection) With {.MissingSchemaAction = MissingSchemaAction.AddWithKey}
Private CustomerCommandBuilder As New OleDbCommandBuilder(Me.CustomerDataAdapter) With {.QuotePrefix = "[",
.QuoteSuffix = "]"}
Private LoanCommandBuilder As New OleDbCommandBuilder(Me.LoanDataAdapter) With {.QuotePrefix = "]",
.QuoteSuffix = "]"}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
LoadSchema()
ConfigureAutoIncrements()
ConfigureRelation()
LoadData()
BindData()
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Error")
End Try
End Sub
Private Sub CustomerDataAdapter_RowUpdated(ByVal sender As Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles CustomerDataAdapter.RowUpdated
If e.StatementType = StatementType.Insert Then
Using Command As New OleDbCommand("SELECT @@IDENTITY", Me.connection)
e.Row("CustomerID") = CInt(Command.ExecuteScalar())
End Using
End If
End Sub
Private Sub LoanDataAdapter_RowUpdated(ByVal sender As Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles LoanDataAdapter.RowUpdated
If e.StatementType = StatementType.Insert Then
Using Command As New OleDbCommand("SELECT @@IDENTITY", Me.connection)
e.Row("LoanID") = CInt(Command.ExecuteScalar())
End Using
End If
End Sub
Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
If Me.Validate() Then
Me.CustomerBindingSource.EndEdit()
Me.LoanBindingSource.EndEdit()
Me.CustomerDataAdapter.Update(Me.data, "Customer")
Me.LoanDataAdapter.Update(Me.data, "Loan")
End If
End Sub
Private Sub LoadSchema()
Me.CustomerDataAdapter.FillSchema(Me.data, SchemaType.Source, "Customer")
Me.LoanDataAdapter.FillSchema(Me.data, SchemaType.Source, "Loan")
End Sub
Private Sub ConfigureAutoIncrements()
ConfigureAutoIncrement(Me.data.Tables("Customer").Columns("CustomerID"))
ConfigureAutoIncrement(Me.data.Tables("Loan").Columns("LoanID"))
End Sub
Private Sub ConfigureAutoIncrement(ByVal Column As DataColumn)
With Column
.AutoIncrement = True
.AutoIncrementSeed = 0
.AutoIncrementStep = -1
End With
End Sub
Private Sub ConfigureRelation()
Me.data.Relations.Add("TholusizoMoneyS",
Me.data.Tables("Customer").Columns("CustomerID"),
Me.data.Tables("Loan").Columns("CustomerID"),
True).ChildKeyConstraint.UpdateRule = Rule.Cascade
End Sub
Private Sub LoadData()
Me.CustomerDataAdapter.Fill(Me.data, "Customer")
Me.LoanDataAdapter.Fill(Me.data, "Loan")
End Sub
Private Sub BindData()
Me.CustomerBindingSource.DataSource = Me.data.Tables("Customer")
Me.LoanBindingSource.DataMember = "TholusizoMoneyS"
Me.LoanBindingSource.DataSource = Me.CustomerBindingSource
Me.CustomerDataGridView.DataSource = Me.CustomerBindingSource
Me.LoanDataGridView.DataSource = Me.LoanBindingSource
End Sub
结束班
答案 0 :(得分:0)
我知道仅仅通过链接对答案感到皱眉,但我创建了一个演示项目来展示如何做你想做的事情,并在前一段时间将其上传到其他地方,这样我就不必继续展示同样的东西了。一遍又一遍。这是该项目的链接:
http://www.vbforums.com/showthread.php?659052-Retrieve-Access-AutoNumber-Value-After-Insert