INSERT上的“查询值和目标字段数不一样”错误

时间:2016-01-05 17:52:11

标签: ms-access-2010 vb.net-2010

Imports System.Data
Imports System.Data.OleDb

Public Class form7
    Dim inc As Integer
    Dim con As New OleDb.OleDbConnection
    Dim cmd As OleDbCommand
    Dim dbprovider As String
    Dim dbsource As String
    Dim str As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim TotalRows As Integer
    Dim ID_Number As Integer

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Bank\db1.accdb ")
        con.Open()
        Dim cmd As New OleDbCommand("SELECT COUNT(*) FROM accountsTable", con)
        Dim totalRows As Long = CInt(cmd.ExecuteScalar())
        cmd.ExecuteNonQuery()
        TextBox1.Text = totalRows + 1000
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Form5.Show()

        dbprovider = "Provider=Microsoft.ACE.OLEDB.12.0; dbsource=Data Source=D:\Bank\db1.accdb"
        MsgBox("Database is now open")

        str = "Insert into accountsTable (Account_Number, First_Name, Midle_Name, Last_Name, Date_of_birth, Occupation, Age, Marital_Status, Address, City, Country, State, Account_Type, Pin_Code, Mobile_Number, Email_ID, Opening_Balance) values ('" &
           TextBox1.Text & "','" &
           TextBox2.Text & "','" &
           TextBox3.Text & "','" &
           TextBox4.Text & "','" &
           DateTimePicker1.Value & "','" &
           TextBox6.Text & "','" &
           TextBox7.Text & "','" &
           TextBox8.Text & "','" &
           TextBox9.Text & "','" &
           ComboBox1.Text & "','" &
           ComboBox2.Text & "','" &
           ComboBox3.Text & "','" &
           ComboBox4.Text & "','" &
           TextBox10.Text & "','" &
           TextBox11.Text & "','" &
           TextBox12.Text & "','" &
           TextBox13.Text & "','" & "')"

        cmd = New OleDb.OleDbCommand(str, con)
        cmd.ExecuteNonQuery()
        MsgBox("Record is inserted")
        con.Close()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        DateTimePicker1.Value = ""
        TextBox6.Text = ""
        TextBox7.Text = ""
        TextBox8.Text = ""
        TextBox9.Text = ""
        ComboBox1.Text = ""
        ComboBox2.Text = ""
        ComboBox3.Text = ""
        ComboBox4.Text = ""
        TextBox10.Text = ""
        TextBox11.Text = ""
        TextBox12.Text = ""
        TextBox13.Text = ""

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
        Form3.Show()
    End Sub

End Class

错误发生在这里:

cmd = New OleDb.OleDbCommand(str, con)
cmd.ExecuteNonQuery()  ' <-- error here
MsgBox("Record is inserted")
con.Close()

1 个答案:

答案 0 :(得分:0)

列列表中有17列,但是您要在VALUES列表的末尾添加额外的(第18个)零长度字符串值:

TextBox13.Text & "','" & "')"
                   ^^^^^^^^

你真的应该使用参数化查询,更像是这样:

str = 
        "Insert into accountsTable (Account_Number, First_Name, Midle_Name, Last_Name, Date_of_birth, Occupation, Age, Marital_Status, Address, City, Country, State, Account_Type, Pin_Code, Mobile_Number, Email_ID, Opening_Balance) " &
        "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
cmd = New OleDb.OleDbCommand(str, con)
cmd.Parameters.AddWithValue("?", TextBox1.Text)
cmd.Parameters.AddWithValue("?", TextBox2.Text)
cmd.Parameters.AddWithValue("?", TextBox3.Text)
cmd.Parameters.AddWithValue("?", TextBox4.Text)
cmd.Parameters.AddWithValue("?", DateTimePicker1.Value)
cmd.Parameters.AddWithValue("?", TextBox6.Text)
cmd.Parameters.AddWithValue("?", TextBox7.Text)
cmd.Parameters.AddWithValue("?", TextBox8.Text)
cmd.Parameters.AddWithValue("?", TextBox9.Text)
cmd.Parameters.AddWithValue("?", ComboBox1.Text)
cmd.Parameters.AddWithValue("?", ComboBox2.Text)
cmd.Parameters.AddWithValue("?", ComboBox3.Text)
cmd.Parameters.AddWithValue("?", ComboBox4.Text)
cmd.Parameters.AddWithValue("?", TextBox10.Text)
cmd.Parameters.AddWithValue("?", TextBox11.Text)
cmd.Parameters.AddWithValue("?", TextBox12.Text)
cmd.Parameters.AddWithValue("?", TextBox13.Text)
cmd.ExecuteNonQuery()

请注意,如果其中一些TextBox控件可能为空,那么您必须检查其.Text属性的长度,并可能插入一个空值。例如,对于&#34; Opening_Balance&#34;你可能需要做一些像

这样的事情
If TextBox13.Text.Length = 0 Then
    cmd.Parameters.AddWithValue("?", DBNull.Value)
Else
    cmd.Parameters.AddWithValue("?", Convert.ToDecimal(TextBox13.Text))
End If