vb.net将数据插入访问数据库

时间:2014-07-31 06:33:59

标签: sql database vb.net

我有以下代码将数据输入我的Access数据库,但是我收到以下错误。

Query input must contain at least one table or query.

我做错了什么?以下是他们提到行的代码部分。

Private Sub EditAddButton_Click(sender As Object, e As EventArgs) Handles EditAddButton.Click

    Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\*******************;Jet OLEDB:Database Password=************;")
    Dim insertsql As String
    Try
        insertsql = "INSERT INTO RepairOrders" & _
        "(ROOtherInfo, ROJobType, ROJobTime, RODelPicDate, RONo)" & _
        "VALUES (@other, @type, @time, @delpic, @jobno) WHERE ROJobNo = @jobno"

        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(insertsql, conn)

        cmd.Parameters.AddWithValue("@other", AddOtherText.Text)
        cmd.Parameters.AddWithValue("@type", AddTypeCombo.Text)
        cmd.Parameters.AddWithValue("@time", AddTimeCombo.Text)
        cmd.Parameters.AddWithValue("@delpic", AddDatePick.Value.Date.ToString)
        cmd.Parameters.AddWithValue("@jobno", AddJobText.Text)

        conn.Open()
        cmd.ExecuteNonQuery()
        MessageBox.Show("Booking Added!")
        conn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

我用星号替换了某些信息'覆盖一些敏感信息。

编辑:

现在我收到语法错误:(

Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\*********************************;Jet OLEDB:Database Password=***********;")
    Dim insertsql As String
    Try
        insertsql = "UPDATE RepairOrders SET ROOther = @other, SET RONo = @jobno, SET ROJobType = @type, SET ROJobTime = @time, SET RODelPicDate = @delpic WHERE RONo = @jobno"

        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(insertsql, conn)

        cmd.Parameters.AddWithValue("@other", AddOtherText.Text)
        cmd.Parameters.AddWithValue("@type", AddTypeCombo.Text)
        cmd.Parameters.AddWithValue("@time", AddTimeCombo.Text)
        cmd.Parameters.AddWithValue("@delpic", AddDatePick.Value.Date.ToString)
        cmd.Parameters.AddWithValue("@jobno", AddJobText.Text)

        conn.Open()
        cmd.ExecuteNonQuery()
        MessageBox.Show("Booking Added!")
        conn.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

1 个答案:

答案 0 :(得分:0)

看起来你有太多的SET语句,OleDb需要'?'对于参数,您必须注意它们的数量以及它们的指定顺序。这有点清理,请查看OLEDB Parameterized Query以获得更全面的示例。

Try
        insertsql = "UPDATE RepairOrders 
                       SET ROOther = ?
                        , RONo = ?
                        ,  ROJobType = ?
                        , SET ROJobTime = ?
                        , SET RODelPicDate = ?
                     WHERE RONo = ?"
    Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(insertsql, conn)

    cmd.Parameters.AddWithValue("?", AddOtherText.Text)
    cmd.Parameters.AddWithValue("?", AddJobText.Text)
    cmd.Parameters.AddWithValue("?", AddTypeCombo.Text)
    cmd.Parameters.AddWithValue("?", AddTimeCombo.Text)
    cmd.Parameters.AddWithValue("?", AddDatePick.Value.Date.ToString)
    cmd.Parameters.AddWithValue("?", AddJobText.Text)

    conn.Open()
    cmd.ExecuteNonQuery()
    MessageBox.Show("Booking Added!")
    conn.Close()

Catch ex As Exception
    MsgBox(ex.Message)
End Try