VB.Net 2008 连接到 MS Access
Posted
技术标签:
【中文标题】VB.Net 2008 连接到 MS Access【英文标题】:VB.Net 2008 connecting to MS Access 【发布时间】:2014-12-05 16:07:38 【问题描述】:这是我的代码。当它在 cmd.ExecuteNonQuery() 中有弹出说(插入语句中的语法错误)请帮助我。我真的不知道该怎么办。谢谢!
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
【问题讨论】:
所有这些字段都是字符串? 通常会有更多错误信息,例如“NEAR...” 不幸的是使用Access SQL,错误信息更简洁。在这种情况下,整个错误消息可能只是 “INSERT INTO 语句中的语法错误。” --- 没有其他有用的详细信息。 【参考方案1】:Level
看起来像 reserved word,最好把它放在括号里:
, [Level],
【讨论】:
@Yanyan:请不要忘记接受答案。谢谢。 该链接页面还显示Relation
是保留字。奇怪的是保留字Level
触发了语法错误,而保留字Relation
却没有。以上是关于VB.Net 2008 连接到 MS Access的主要内容,如果未能解决你的问题,请参考以下文章
连接到 Windows Server 2008 上的 MS Access accdb 文件
将 32 位 VB.Net 连接到 64 位 Access .ACCDB
如何在 VB.net 中连接到本地网络上的 Access 数据库?
VB.net数据库编程(01):简单的入门实例--连接到Access文件