简体   繁体   English

尝试将客户添加到数据库时出错(插入语句oledb vb net中的语法错误)

[英]Error When trying to add Customer to Database (syntax error in insert into statement oledb vb net)

I am doing a college assignment and have been trying to figure it out for hours, but I cant seem to get my new customer to save to the database! 我正在做一次大学作业,并且一直在试图解决这个问题,但是我似乎无法让我的新客户保存到数据库中! Please, I would really, REALLY apreciate it if you could have a look at my code, make any suggestions, or let me know more efficient ways of doing this. 请,如果您可以看一下我的代码,提出任何建议或让我知道更有效的方法,我将非常感谢您。 Bellow I provide a sample of my code for this. 在下面,我提供了我的代码示例。

To begin with, on form load I determine the new customer ID to be put into the database: 首先,在表单加载时,我确定要放入数据库中的新客户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

Now that being said, here is the piece of code I run when clicking on my save button for the recrod to be added through a new data row. 话虽如此,这是我单击保存按钮以将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

In comments you can also see a code provided by my teacher, which we are supposed to improve, I just wish to find a way of making this work! 在注释中,您还可以看到我的老师提供的代码,我们应该对此代码进行改进,我只想找到一种方法来完成这项工作!

Thanks a lot, all help and suggestions are very appreciated. 非常感谢,非常感谢所有帮助和建议。

Instead of ds.Tables("Customer") I used ds.Tables(0) (or whatever index your table is at inside your DataSet .) 我使用的不是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()

It's important to realize your database schema also. 同样重要的是要实现数据库模式。 If your first column is an identity auto increment column, you want to avoid trying to insert anything to that column. 如果您的第一列是身份自动递增列,则要避免尝试在该列中插入任何内容。 I prefer to use the Indexes because it's a lot easier to misspell a column name as a string, although it may not be as clear. 我更喜欢使用索引,因为将列名错误地拼写为字符串要容易得多,尽管它可能不太清楚。

So, Customer_ID may be (or not be) an auto-increment field, which means trying to insert data into that column will result in an error. 因此, Customer_ID可能是(或不是)自动递增字段,这意味着尝试将数据插入该列将导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM