VB-查询值和目标字段的数量不相同

时间:2015-04-02 23:31:26

标签: database vb.net

我真的不能在这里看到任何问题。但我收到了错误

Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("App_Data/Database5.mdb"))
        conn.Open()
        Dim sql As String = "INSERT INTO Users VALUES (@firstName, @lastName)"
        Dim txtFname As New TextBox
        Dim txtLname As New TextBox
    Dim cmd As New OleDbCommand(sql, conn)

    cmd.Parameters.AddWithValue("@firstName", txtFname.Text)
    cmd.Parameters.AddWithValue("@lastName", txtLname.Text)
    cmd.Connection = conn
    cmd.ExecuteNonQuery()
    cmd.Dispose()
    conn.Close()

数据库:Id;自动编号           名字;文本           姓;文本

2 个答案:

答案 0 :(得分:2)

除了我所做的评论之外,最好将块用于一次性资源,例如连接和命令。另请注意,缺少您正在创建的两个TextBox。

Using conn = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & Server.MapPath("App_Data/Database5.mdb"))
    conn.Open()
    Using cmd = New OleDbCommand("INSERT INTO Users (FirstName, LastName) VALUES (@firstName, @lastName)", conn)
        cmd.Parameters.AddWithValue("@firstName", txtFname.Text)
        cmd.Parameters.AddWithValue("@lastName", txtLname.Text)
        cmd.ExecuteNonQuery()
    End Using
End Using

答案 1 :(得分:1)

请更正您的SQL:

INSERT INTO Users (firstName,lastName) VALUES (@firstName, @lastName)