错误:表插入在vb.net和msacess中返回错误

时间:2015-11-22 10:33:04

标签: vb.net ms-access insert-into

我是vb.net的新手,我正在尝试将值从vb.net插入msacess。我知道alr​​edy在这里有一个问题的答案,但那些答案并没有解决我的问题所以我再次发布

我在将数据插入数据库时​​遇到错误。

Ms-acess:

table name: reg
_________________
field |datatype
__________________
id    |autonum
fname |text
lname |text
course|text
fees  |number
amount|number
bal   |number

错误是:

  

查询值和目标字段的数量不同。

代码:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If (TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or TextBox5.Text = "" Or TextBox6.Text = "") Then
            MessageBox.Show("Field Not Empty")
        End If
        connection = New OleDbConnection(ConfigurationManager.ConnectionStrings("DBConnect").ConnectionString)
        connection.Open()
        command = New OleDbCommand("insert into reg values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "," + TextBox5.Text + "," + TextBox6.Text + ")", connection)

        command.ExecuteNonQuery()
        connection.Close()

        MessageBox.Show("Data Added")
    End Sub

1 个答案:

答案 0 :(得分:1)

您的SQL字符串必须列出字段名称,可能是:

"insert into reg (fname, lname, course, fees, amount, bal) values ('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + "," + TextBox5.Text + "," + TextBox6.Text + ")"