VB.Net 2008连接到MS Access

时间:2014-12-05 16:07:39

标签: vb.net visual-studio ms-access

这是我的代码。当它在cmd.ExecuteNonQuery()时有popping说(Insert Into Statement中的语法错误)请帮助我。我真的不知道该怎么做。谢谢!

Public Class Form1

Dim con As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Yang\Documents\Info.accdb"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    Using dbconnection = New OleDbConnection(con)
        Dim dt As New DataTable
        Dim da As New OleDbDataAdapter

        dbconnection.Open()
        Dim cmd As New OleDbCommand
        cmd.Connection = dbconnection
        cmd.CommandText = "INSERT INTO tbl_information(SN,Level,Course,Sur_name,First_name,Middle_name,Address,Sex,Birth_day,CN,CS,Rel,FSN,FO,MSN,MO,Guardian,Relation,GA)VALUES(@SN,@Level,@Course,@Sur_name,@First_name,@Middle_name,@Address,@Sex,@Birth_day,@CN,@CS,@Rel,@FSN,@FO,@MSN,@MO,@Guardian,@Relation,@GA)"
        cmd.Parameters.Add(New OleDbParameter("@SN", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Level", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Course", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Sur_name", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@First_name", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Middle_name", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Address", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Sex", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Birth_day", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@CN", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@CS", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Rel", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@FSN", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@FO", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@MSN", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@MO", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Guardian", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@Relation", DbType.String))
        cmd.Parameters.Add(New OleDbParameter("@GA", DbType.String))

        If RadioButton1.Checked Then
            cmd.Parameters("@Sex").Value = RadioButton1.Text
        Else
            cmd.Parameters("@Sex").Value = RadioButton2.Text
        End If


        cmd.Parameters("@SN").Value = txt_SN.Text()
        cmd.Parameters("@Level").Value = cmbLevel.Text
        cmd.Parameters("@Course").Value = cmbCourse.Text
        cmd.Parameters("@Sur_name").Value = txtSurname.Text
        cmd.Parameters("@First_name").Value = txtFirstname.Text
        cmd.Parameters("@Middle_name").Value = txt_Middlename.Text
        cmd.Parameters("@Address").Value = txt_Address.Text
        cmd.Parameters("@Birth_day").Value = DateTimePicker1.Text
        cmd.Parameters("@CN").Value = txt_CN.Text
        cmd.Parameters("@CS").Value = txt_Citizenship.Text
        cmd.Parameters("@Rel").Value = txt_Rel.Text
        cmd.Parameters("@FSN").Value = txtFsN.Text
        cmd.Parameters("@FO").Value = txtFO.Text
        cmd.Parameters("@MSN").Value = txtMN.Text
        cmd.Parameters("@MO").Value = txtMO.Text
        cmd.Parameters("@Guardian").Value = txtG.Text
        cmd.Parameters("@Relation").Value = txtRel.Text
        cmd.Parameters("@GA").Value = txtGA.Text
        cmd.ExecuteNonQuery()
        dbconnection.Close()

        MessageBox.Show("Data has been saved!")
        txt_SN.Text = ""
        cmbLevel.Text = ""
        cmbCourse.Text = ""
        txtSurname.Text = ""
        txtFirstname.Text = ""
        txt_Middlename.Text = ""
        txt_Address.Text = ""
        DateTimePicker1.Text = ""
        txt_CN.Text = ""
        txt_Citizenship.Text = ""
        txt_Rel.Text = ""
        txtFO.Text = ""
        txtMN.Text = ""
        txtMO.Text = ""
        txtG.Text = ""
        txtRel.Text = ""
        txtGA.Text = ""
    End Using
End Sub
End Class

1 个答案:

答案 0 :(得分:3)

Level看起来像reserved word,最好把它放在括号中:

, [Level],