[英]Error When trying to add Customer to Database (syntax error in insert into statement oledb vb net)
我正在做一次大學作業,並且一直在試圖解決這個問題,但是我似乎無法讓我的新客戶保存到數據庫中! 請,如果您可以看一下我的代碼,提出任何建議或讓我知道更有效的方法,我將非常感謝您。 在下面,我提供了我的代碼示例。
首先,在表單加載時,我確定要放入數據庫中的新客戶ID:
Private Sub frmRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Me.CustomerTableAdapter.Fill(Me.DatabasePizzaPalaceDataSet.Customer)
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim Rows As Integer
Dim sql As String = "SELECT * FROM Customer"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Customer")
Rows = ds.Tables("Customer").Rows.Count
NewCustomerID.Text = Rows + 1
Customer_IDTextBox.Text = NewCustomerID.Text
conn.Close()
End Sub
話雖如此,這是我單擊保存按鈕以將Recrod通過新數據行添加時運行的代碼段。
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim conn As New System.Data.OleDb.OleDbConnection()
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=H:\Year 2\Unit 17 Project Planning\Workto do\PizzaPalce\Program\DatabasePizzaPalace.accdb"
conn.Open()
Dim inc As Integer
Dim sql As String = "SELECT * FROM Customer"
Dim da As OleDb.OleDbDataAdapter
Dim ds As New DataSet
da = New OleDb.OleDbDataAdapter(sql, conn)
da.Fill(ds, "Customer")
inc = Customer_IDTextBox.Text
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Customer").NewRow()
dsNewRow.Item("Customer_ID") = Customer_IDTextBox.Text
dsNewRow.Item("Username_Email") = Username_EmailTextBox.Text
dsNewRow.Item("Password") = PasswordTextBox.Text
dsNewRow.Item("First_Name") = First_NameTextBox.Text
dsNewRow.Item("Surname") = SurnameTextBox.Text
dsNewRow.Item("Mobile") = MobileTextBox.Text
dsNewRow.Item("House") = HouseTextBox.Text
ds.Tables("Customer").Rows.Add(dsNewRow)
da.Update(ds, "Customer")
MsgBox("New Record added to the Database")
conn.Close()
frmLogin.Show()
End If
'Dim cb As New OleDb.OleDbCommandBuilder(da)
'Me.CustomerTableAdapter.Insert(Customer_IDTextBox.Text, Username_EmailTextBox.Text, PasswordTextBox.Text, First_NameTextBox.Text, SurnameTextBox.Text, MobileTextBox.Text, HouseTextBox.Text)
'Me.CustomerTableAdapter.Fill(Me.DatabasePizzaPalaceDataSet.Customer)
'Me.Validate()
'Me.CustomerBindingSource.EndEdit()
'Me.CustomerTableAdapter.Fill(DatabasePizzaPalaceDataSet.Customer)
'Me.TableAdapterManager.UpdateAll(Me.DatabasePizzaPalaceDataSet)
'da.Update(ds, "Customer")
'MsgBox("You have been succesfully registerd with us. Thanks!")
'conn.Close()
'frmLogin.Show()
End Sub
在注釋中,您還可以看到我的老師提供的代碼,我們應該對此代碼進行改進,我只想找到一種方法來完成這項工作!
非常感謝,非常感謝所有幫助和建議。
我使用的不是ds.Tables("Customer")
而是使用ds.Tables(0)
(或表在DataSet
內部的任何索引)。
Dim con As New OleDbConnection(_myConn) ''_myConn should be your connection string
con.Open()
Dim da As OleDbDataAdapter
Dim ds As New DataSet
da = New OleDbDataAdapter("select * from customer", con)
da.Fill(ds)
Dim cb As New OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables(0).NewRow()
dsNewRow.Item(1) = "1"
dsNewRow.Item(2) = "Blah"
dsNewRow.Item(3) = "Test"
dsNewRow.Item(4) = "T"
dsNewRow.Item(5) = "T"
dsNewRow.Item(6) = "T"
dsNewRow.Item(7) = "20000101"
ds.Tables(0).Rows.Add(dsNewRow)
da.Update(ds.Tables(0))
con.Close()
同樣重要的是要實現數據庫模式。 如果您的第一列是身份自動遞增列,則要避免嘗試在該列中插入任何內容。 我更喜歡使用索引,因為將列名錯誤地拼寫為字符串要容易得多,盡管它可能不太清楚。
因此, Customer_ID
可能是(或不是)自動遞增字段,這意味着嘗試將數據插入該列將導致錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.