INSERT INTO语法错误vb.net,Access

时间:2018-02-18 16:47:29

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

我使用以下代码获得insert into语法错误。我的所有字段都是short text值:

Imports System.Data.OleDb
Public Class Student_Sign_up
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim myConnection As OleDbConnection = New OleDbConnection




Private Sub submit_Click(sender As Object, e As EventArgs) Handles submit.Click
    Dim myConnection As OleDbConnection = New OleDbConnection



    provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    'Change the following to your access database location
    dataFile = "D:\Student Database1.accdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString

    myConnection.Open()
    Dim str As String
    str = "insert into student details ([Student_Name], [Student_Surname], [Student_Username], [Student_Password], [Class]) values (?, ?, ?, ?, ?)"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("Student_Name", CType(Name.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Student_Surname", CType(Surname.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Student_Username", CType(UserName.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Student_Password", CType(Password.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Class", CType(Student_class.Text, String)))
    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        Student_class.Clear()
        Name.Clear()
        UserName.Clear()
        Password.Clear()
        Surname.Clear()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

End Class

1 个答案:

答案 0 :(得分:0)

您需要使用带有表名

的分隔符

而不是

str = "insert into student details 

使用

str = "insert into [student details]