尝试使用“插入”ms-access 数据库时出错

Posted

技术标签:

【中文标题】尝试使用“插入”ms-access 数据库时出错【英文标题】:Error when trying to use to "Insert into" a ms-access database 【发布时间】:2014-02-20 21:55:21 【问题描述】:

我正在尝试将数据插入到 access 数据库中,这个解决方案过去对我有用,但这次我不断收到错误:

“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

【问题讨论】:

OLEDB 不使用“?”对于参数占位符,而不是“@param”? @JohnSaunders 实际上两者都可以,但? 通常是首选,因为 OleDb 忽略 实际参数 names 并且只关注 order 它们出现在 CommandText 中的顺序。 【参考方案1】:

字段名称Co-Authors 包含连字符,因此必须用方括号括起来,即,

... Advisor, [Co-Authors], Attending, ...

【讨论】:

以上是关于尝试使用“插入”ms-access 数据库时出错的主要内容,如果未能解决你的问题,请参考以下文章

QT ODBC 驱动程序 .mdb ms-access 插入在 1000 行以上时不起作用(Windows)

将日期插入 Ms-Access 数据库的问题

经典 ASP 和 ms-access:插入数据的问题

在 ms-access 表中插入多行的 SQL 代码

尝试使用 RETURNING 和更新/插入从 plpgsql 函数返回时出错

使用 GenericDAO 插入第二个数据源时出错