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

时间:2014-05-28 14:18:07

标签: sql database vb.net syntax-error

我正在做大学任务并且一直试图弄清楚几个小时,但我似乎无法让我的新客户保存到数据库!如果你能查看我的代码,提出任何建议,或者让我知道更有效的方法,请真的,我真的,真的很赞。 Bellow我提供了我的代码示例。

首先,在表单加载时,我确定要放入数据库的新客户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

在评论中你也可以看到我老师提供的代码,我们应该改进这些代码,我只是希望找到一种方法来完成这项工作!

非常感谢,非常感谢所有的帮助和建议。

1 个答案:

答案 0 :(得分:1)

而不是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可能是(或不是)自动增量字段,这意味着尝试将数据插入该列将导致错误。