2014-03-25 46 views
1

現在,拋出的錯誤位於我的插入語句處。我不知道它在哪裏。我已經使用該演示來適應我的程序,但最終失敗。這是重做的代碼。 進口System.Data.OleDb 公共類Form1中創建行後,將1表中的id值插入另一個表中VB

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

我知道,與剛剛鏈接的答案SO皺眉,但我創建了一個示範項目,以顯示如何做你想做的,並上傳它在前一段時間是專門用於我不必一再重複顯示相同的內容。這是該項目的鏈接:

http://www.vbforums.com/showthread.php?659052-Retrieve-Access-AutoNumber-Value-After-Insert

+0

它的作品完全像一個魅力。感謝百萬 – Mpixify

+0

請按照演示檢查代碼。它告訴我我有一個不正確的插入語句。請幫助查找錯誤 – Mpixify

+0

如果您使用的是命令生成器,並且您在其中一個生成的SQL語句中出現語法錯誤,則通常意味着您的一個或多個列名稱是保留字或包含空格或其他特殊字符。嘗試將命令生成器的QuotePrefix和QuoteSuffix屬性分別設置爲「[」和「]」,以便將所有列名稱包裝在括號中。 – jmcilhinney

相關問題