带参数的SQL Insert适用于MSAccess但不适用于SQL Server

时间:2016-01-27 02:50:23

标签: sql .net sql-server vb.net ms-access

我注意到,在连接到MSAccess 2010时,以下二进制数据插入工作正常,但是当连接到SQL Server Express 2012时,我收到错误。这是代码:

Public Shared Sub InsertBlobs(ByRef hash As Hashtable)
    Dim constring As String
    Select Case CONNECTION_TYPE
        Case DBConnection.MS_Access07
            constring = "Provider=Microsoft.Ace.OLEDB.12.0;" & _
                        "User ID=Admin;" & _
                        "Data Source=" & ACCESS07_CONNECTION_STRING & ";" & _
                        "Mode=Read|Write;Extended Properties="""";Jet OLEDB:System Database="""";" & _
                        "Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=5;Jet OLEDB:Database Locking Mode=0;" & _
                        "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Global Bulk Transactions=1;" & _
                        "Jet OLEDB:New Database Password="""";Jet OLEDB:Create System Database=False;" & _
                        "Jet OLEDB:Encrypt Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;" & _
                        "Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False"
        Case DBConnection.MS_SQL
            constring = "Provider=SQLOLEDB;" & getSQLConnectionString()
    End Select
    Try
        Using myconnection As New OleDbConnection(constring)
            myconnection.Open()
            Dim sqlQry As String = "INSERT INTO [UAV_Archive] ([mission_id], [vehicle_plan_gzip], [msn_header]) VALUES " & _
                "(@mission_id, @vehicle_plan_gzip, @msn_header)"
            Using cmd As New OleDbCommand(sqlQry, myconnection)
                cmd.Parameters.AddWithValue("@mission_id", CInt(hash.Item("mission_id")))
                cmd.Parameters.AddWithValue("@vehicle_plan_gzip", CType(hash.Item("vehicle_plan_gzip"), Byte()))
                cmd.Parameters.AddWithValue("@msn_header", CType(hash.Item("msn_header"), Byte()))
                cmd.ExecuteNonQuery()
            End Using
        End Using
    Catch ex As Exception
        Dim debugBreakHere As Integer = 0
    End Try

End Sub

我在SQL Server模式下遇到的错误是:

  

必须声明标量变量“@mission_id”

由于代码在我连接到MSAccess数据库时有效,我真的不确定如何为SQL Server解决这个问题。我知道我可以连接到SQL Server,因为其他查询(选择查询)可以工作。我知道插入在我的帐户下工作,因为我能够做基于字符串的插入。但我在这里步履蹒跚。

如果是数据类型问题,为什么错误与Integer字段有关?

1 个答案:

答案 0 :(得分:4)

System.Data.OleDb实际上不支持命名参数。参数是位置,必须按照其占位符在命令文本中出现的顺序声明。

现在,Access OLEDB提供程序将容忍命名参数。它将忽略这些名称并按顺序排列,但它对于具有名称的参数(和占位符)没有问题。

SQL Server的OLEDB提供程序更加繁琐,并且会在您使用它们时拒绝命名参数。所以,对于SQL Server ......

这将失败并显示您描述的错误消息:

Dim connStr As String
connStr = "Provider=SQLOLEDB;Server=(local)\SQLEXPRESS;Database=myDb;Trusted_Connection=yes"
Using conn As New OleDbConnection(connStr)
    conn.Open()
    Dim sql As String
    sql = "INSERT INTO Clients (LastName, FirstName) VALUES (@LastName, @FirstName)"
    Using cmd As New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@LastName", "Thompson")
        cmd.Parameters.AddWithValue("@FirstName", "Gord")
        cmd.ExecuteNonQuery()
    End Using
End Using

然而,这将有效:

Dim connStr As String
connStr = "Provider=SQLOLEDB;Server=(local)\SQLEXPRESS;Database=myDb;Trusted_Connection=yes"
Using conn As New OleDbConnection(connStr)
    conn.Open()
    Dim sql As String
    sql = "INSERT INTO Clients (LastName, FirstName) VALUES (?, ?)"
    Using cmd As New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("?", "Thompson")  ' LastName
        cmd.Parameters.AddWithValue("?", "Gord")  ' FirstName
        cmd.ExecuteNonQuery()
    End Using
End Using