【问题标题】:Insert Parameterized Query ASP.NET MS SQL插入参数化查询 ASP.NET MS SQL
【发布时间】:2008-12-19 19:07:15
【问题描述】:

这是我的 SQLCommand 对象:

oCommand.CommandText = 
"INSERT INTO hits (id,client_id,client_ip,page,vars) VALUES _
                     (@@IDENTITY,@client_id,@ip,@page,@vars)"
oCommand.Parameters.Count = 4
 >>   oCommand.Parameters.Item(0).ParameterName = "@client_id"
 >>   oCommand.Parameters.Item(0).Value = "123456"
 >>   oCommand.Parameters.Item(1).ParameterName = "@ip"
 >>   oCommand.Parameters.Item(1).Value = "127.0.0.1"
 >>   oCommand.Parameters.Item(2).ParameterName = "@page"
 >>   oCommand.Parameters.Item(2).Value = "default.aspx"
 >>   oCommand.Parameters.Item(3).ParameterName = "@vars"
 >>   oCommand.Parameters.Item(3).Value = Nothing

这是我得到的错误:

The parameterized query '(@ip nvarchar(9),@client_id nvarchar(4000),@page nvarchar(12),@v' expects the parameter '@client_id', which was not supplied.

下面是函数:

Public Shared Function insertIntoHitTable(ByVal oData As gsTrack) As Boolean
    Dim oObj As New List(Of Object())
    oObj.Add(New Object() {"@client_id", cV(oData.ClientID)})
    oObj.Add(New Object() {"@ip", cV(oData.IP)})
    oObj.Add(New Object() {"@page", cV(oData.Page)})
    oObj.Add(New Object() {"@vars", oData.Vars})
    Dim oCommand As SqlCommand = InsertIntoHitTableSQL(oObj)
    oCommand.Connection.Open()
    oCommand.ExecuteNonQuery()
    oCommand.Connection.Close()
End Function

Public Shared Function createSQLCommand(ByVal oCmdTxt As String, ByVal oParams As List(Of Object())) As SqlCommand
    Dim oCommand As SqlCommand = Nothing
    Dim oBuilder As New StringBuilder
    Dim oParam As SqlParameter
    oCommand = New SqlCommand(oCmdTxt, New SqlConnection(csString))
    Try
        For i As Integer = 0 To oParams.Count - 1
            oParam = New SqlParameter
            oParam.ParameterName = oParams(i)(0)
            oParam.Value = oParams(i)(1)
            oCommand.Parameters.Add(oParam)
            oParam = Nothing
        Next
        Return oCommand
    Catch ex As Exception
        Return Nothing
    End Try
End Function

有关如何解决此参数化查询错误的任何指示?谢谢!

编辑

我应该注意到 cV() 只是一个清理函数,它检查传递的变量是否为空。

【问题讨论】:

    标签: asp.net sql-server


    【解决方案1】:

    我相信参数计数和索引会稍微偏移,因为您在插入语句中指定了@@IDENTIDY。在进行参数化查询时,我通常遵循以下语法:

    oCommand = New SqlCommand("INSERT INTO hits (id,client_id,client_ip,page,vars) VALUES (@@IDENTITY,@client_id,@ip,@page,@vars)", CONNECTION OBJECT)
    
    oCommand.CommandType = CommandType.StoredProcedure
    oCommand.Parameters.Add("@client_id", SqlDbType.Integer, 10)
    oCommand.Parameters("@client_id").Direction = ParameterDirection.Input
    oCommand.Parameters("@client_id").Value = cV(oData.ClientID)
    oCommand.Parameters.Add("@ip", SqlDbType.VarChar, 15)
    oCommand.Parameters("@ip").Direction = ParameterDirection.Input
    oCommand.Parameters("@ip").Value = cV(oData.IP)
    
    oCommand.Connection.Open()
    oCommand.ExecuteNonQuery()
    oCommand.Connection.Close()
    

    ...您可以看到其余参数将如何变化。

    【讨论】:

      【解决方案2】:

      您是否在 CV 函数中检查该值是否为空? sites 之一我看到你需要传递一个 DBNull.value 值而不是 null 的文档。

      【讨论】:

      • 我认为是您的帖子和 Dillie 的帖子共同解决了这个问题,我创建了一个新的 CV 函数,如果它什么都没有,则返回 DBNull.Value。我还添加了来自 dillie 的参数方向,现在它可以工作了。谢谢大家!
      【解决方案3】:

      你不应该使用@@identity。如果该列是一个标识,只需不要在值列表中指定它。如果您在任何地方使用它来获取刚刚插入的值,请改用 scope_identity(),除非您喜欢数据完整性问题。

      【讨论】:

        猜你喜欢
        • 2016-01-26
        • 1970-01-01
        • 2017-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        • 2014-03-21
        • 1970-01-01
        相关资源
        最近更新 更多