使用Access的INSERT INTO语句VB中的语法错误

时间:2018-04-19 17:00:46

标签: sql vb.net ms-access syntax-error insert-into

我现在已经挣扎了一段时间,只是不明白什么是错的。我在大学课程中这样做,几乎没有编码经验。我在这里尝试的是从我的Windows窗体中插入值到我的Access数据库。

它不断出现同样的错误

  

" INSERT INTO语句中的语法错误"

Dim sqlconn As New OleDb.OleDbConnection
    Dim sqlquery As New OleDb.OleDbCommand
    Dim connString As String
    connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Computer science\Coursework\Database.accdb"
    sqlconn.ConnectionString = connString
    sqlquery.Connection = sqlconn
    sqlconn.Open()
    Dim Query As String = "INSERT INTO Client_form (MemberNumber, Forename, Surname, Age, SkillLevel, Session) VALUES (" & NewMem.ToString & ", '" & TextBox1.Text & "', '" & TextBox2.Text & "', " & TextBox3.Text & ", '" & TextBox5.Text & "', '" & TextBox4.Text & "');"
    'sqlquery.CommandText = "INSERT INTO Client form(MemberNumber, Firstname, Surname, Age, Skill level, Session)VALUES(@MemberNumber, @Firstname, @Surname, @Age, @Skill level, @Session)"
    'sqlquery.Parameters.AddWithValue("@MemberNumber", NewMem)
    'sqlquery.Parameters.AddWithValue("@Firstname", TextBox1.Text)
    'sqlquery.Parameters.AddWithValue("@Surname", TextBox2.Text)
    'sqlquery.Parameters.AddWithValue("@Age", Convert.ToInt16(TextBox3.Text))
    'sqlquery.Parameters.AddWithValue("@Skill level", TextBox5.Text)
    'sqlquery.Parameters.AddWithValue("@Session", TextBox4.Text)
    'sqlquery.CommandText = "INSERT INTO Client_form (MemberNumber, Firstname, Surname, Age, Skill level, Session) VALUES (345, t, e, 5, 7, am);"
    sqlquery.CommandText = Query
    MsgBox(sqlquery.CommandText.ToString)
    sqlquery.ExecuteNonQuery()
    sqlconn.Close()

非常感谢任何形式的指导,谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个..

Dim sqlconn As New OleDb.OleDbConnection
Dim sqlquery As New OleDb.OleDbCommand
Dim connString As String
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Computer science\Coursework\Database.accdb"
sqlconn.ConnectionString = connString
sqlquery.Connection = sqlconn
sqlconn.Open()
Dim Query As String = "INSERT INTO Client_form([MemberNumber], [Firstname], [Surname], [Age], [Skilllevel], [Session])VALUES(@MemberNumber, @Firstname, @Surname, @Age, @Skill level, @Session)"
'sqlquery.CommandText = "INSERT INTO Client form([MemberNumber], [Firstname], [Surname], [Age], [Skilllevel], [Session])VALUES(@MemberNumber, @Firstname, @Surname, @Age, @Skill level, @Session)"
sqlquery.Parameters.AddWithValue("@MemberNumber", NewMem.Text)
sqlquery.Parameters.AddWithValue("@Firstname", TextBox1.Text)
sqlquery.Parameters.AddWithValue("@Surname", TextBox2.Text)
sqlquery.Parameters.AddWithValue("@Age",TextBox3.Text)
sqlquery.Parameters.AddWithValue("@Skilllevel", TextBox5.Text)
sqlquery.Parameters.AddWithValue("@Session", TextBox4.Text)
sqlquery.CommandText = Query
MsgBox(sqlquery.CommandText.ToString)
sqlquery.ExecuteNonQuery()
sqlconn.Close()

同时删除技能(空格)级别之间的空格。将age的数据类型也改为shortext。 我希望它会奏效。

答案 1 :(得分:0)

我删除了冗余行,还要注意用正确的类型解析值。你可能想试试这个

    Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\Computer science\Coursework\Database.accdb"
    Dim cmd As New OleDbCommand("INSERT INTO Client form(MemberNumber, Firstname, Surname, Age, SkillLevel, Session)VALUES(@MemberNumber, @Firstname, @Surname, @Age, @SkillLevel, @Session)")

    cmd.Parameters.AddWithValue("@MemberNumber", NewMem.ToString)
    cmd.Parameters.AddWithValue("@Firstname", TextBox1.Text)
    cmd.Parameters.AddWithValue("@Surname", TextBox2.Text)
    cmd.Parameters.AddWithValue("@Age", TextBox3.Text)
    cmd.Parameters.AddWithValue("@SkillLevel", TextBox5.Text)
    cmd.Parameters.AddWithValue("@Session", TextBox4.Text)

    Using con As New OleDbConnection(connString)
        cmd.Connection = con
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Using