我正在尝试将数据插入到访问数据库中,这个解决方案过去对我有用,但这次我不断收到错误:
发生了'System.Data.OleDb.OleDbException'类型的异常 System.Data.dll但未在用户代码中处理
附加信息:INSERT INTO语句中的语法错误。
这是代码&下面是访问中的数据库:
Imports System.Data.OleDb
Partial Class NewFS_Templates_Centured_Template
Inherits System.Web.UI.Page
Protected Sub sub_Click(sender As Object, e As EventArgs) Handles submit.Click
'form data requests----------------------------------------------------
Dim strFirst As String = first.Text
Dim strLast As String = last.Text
Dim strStatusg As String = statusg.Text
Dim strPhone As String = phone.Text
Dim strEmail As String = email.Text
Dim strCat As String = cat.Text
Dim strJoint As String = joint.Text
Dim strAdvise As String = advisor.Text
Dim strAuth As String = authors.Text
Dim StrAtt As String = attend.Text
Dim strTitle As String = title.Text
Dim strAbs As String = abstract.Text
'Open Db Connection (Remember to include Imports System.Data.OleDb)
Dim strSQL As String
Dim dbconn As OleDbConnection = Nothing
dbconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Server.MapPath("..\App_Data\ResearchSymposiumReg.mdb"))
dbconn.Open()
'SQL actions
'INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
strSQL = "INSERT INTO ResearchSymposium2014 (AuthorLast, AuthorFirst, RegStatus, Email, Phone, Catagory, JointSubmission, Advisor, Co-Authors, Attending, PosterTitle, Abstract) values (@strFirst,@strLast,@strStatusg,@strPhone,@strEmail,@strCat,@strJoint,@strAdvise,@strAuth,@StrAtt,@strTitle,@strAbs);"
Dim objcmd = New OleDbCommand(strSQL, dbconn)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@AuthorLast", strFirst))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@AuthorFirst", strLast))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@RegStatus", strStatusg))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Email", strEmail))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Phone", strPhone))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Catagory", strCat))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@JointSubmission", strJoint))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Advisor", strAdvise))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Co-Authors", strAuth))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Attending", StrAtt))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@PosterTitle", strTitle))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Abstract", strAbs))
'Sets each parameter to "DBNULL" or "blank" if they are null/blank
'objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@FirsNtame", If(strFirst, DBNull.Value)))
For Each param As System.Data.OleDb.OleDbParameter In objcmd.Parameters
If param.Value Is Nothing Then
param.Value = DBNull.Value
End If
Next
objcmd.ExecuteNonQuery()
'Close DB Connection
dbconn.Close()
Server.Transfer("ThankYou.aspx", True)
End Sub
End Class
答案 0 :(得分:2)
字段名Co-Authors
包含连字符,因此必须用方括号括起来,即
... Advisor, [Co-Authors], Attending, ...