有没有更好的方法在VB中使用SqlParameters调用存储过程?

时间:2012-02-29 21:58:58

标签: vb.net sqlparameters

任何人都知道在VB.NET中创建和初始化SqlParameter的更好方法吗?每个变量使用3行似乎相当过分。不幸的是,这个类的constructors非常荒谬,所以我只想编写自己的sub来初始化每个参数。这就是我一直在做的事情。

Dim ID As New SqlParameter("@ID", SqlDbType.Int)
ID.Value = val
query.Parameters.Add(ID)

这是我用于完整代码的完整代码,即使是只需要一个参数的简单存储过程。我有一些需要15-20,这是使用这种笨重方法的大量代码。

Dim query As New SqlCommand("spGetInitStatusEntry", connection)
query.CommandType = CommandType.StoredProcedure
Dim ID As New SqlParameter("@ID", SqlDbType.Int)
ID.Value = val
query.Parameters.Add(ID)
Dim dt As New DataTable
Dim da As New SqlDataAdapter(query)
da.Fill(dt)

最终实施

我按照建议使用两个支持Module创建了以下<Extension()>

Module TcomExtensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub AddParameter(ByRef dbCommand As SqlCommand, ByVal parameterName As String, _
            ByVal parameterValue As Object, ByVal dbType As SqlDbType)

        Dim dbParameter As SqlParameter = Nothing
        dbParameter = dbCommand.CreateParameter

        dbParameter.ParameterName = parameterName
        dbParameter.Value = parameterValue
        dbParameter.DbType = dbType

        dbCommand.Parameters.Add(dbParameter)
    End Sub

    <System.Runtime.CompilerServices.Extension()> _
    Public Function ToDataTable(ByVal value As SqlCommand) As DataTable
        Dim dt As New DataTable
        Using da As New SqlDataAdapter(value)
            da.Fill(dt)
        End Using
        Return dt
    End Function
End Module

我的实施如下:

Dim spPrefill As New SqlCommand("spGetPrefillStatusEntry", connection) With {.CommandType = CommandType.StoredProcedure}
'Only one line per added parameter.
spPrefill.AddParameter("@ID", val, SqlDbType.Int)
'Populate the table.
dt = spPrefill.ToDataTable()

3 个答案:

答案 0 :(得分:3)

您还可以合并前两行:

Dim query As New SqlCommand("spGetInitStatusEntry", connection) With {.CommandType = CommandType.StoreProcedure}

扩展方法也可以在这里成为你的朋友:

<Extension()> _
Public Function ToDataTable(ByVal value As SqlCommand) As DataTable
  Dim dt As New DataTable
  Using da As New SqlDataAdapter(value)
    da.Fill(dt)
  End Using
  Return dt
End Function

除了Mike Hofer对AddWithValue的建议外,现在可以将其简化为:

Dim query As New SqlCommand("spGetInitStatusEntry", connection) With {.CommandType = CommandType.StoreProcedure}
query.Parameters.AddWithValue("@ID", key)
Dim dt As DataTable = query.ToDataTable();

如果使用AddWithValue不舒服,您也可以将参数信息写在一行中:

query.Parameters.Add(New SqlParameter("@ID", SqlDbType.Int) With {.Value = key})

答案 1 :(得分:2)

有了这些扩展...

 <System.Runtime.CompilerServices.Extension()> _
Public Sub AddParameterValue(ByRef dbCommand As System.Data.Common.DbCommand, ByVal parameterName As String, ByVal parameterValue As Object)
    dbCommand.AddParameter(parameterName, parameterValue)
End Sub


<System.Runtime.CompilerServices.Extension()> _
Public Sub AddParameter(ByRef dbCommand As System.Data.Common.DbCommand, ByVal parameterName As String, Optional ByVal parameterValue As Object = Nothing, Optional ByVal sourceColumn As String = "")
    Dim dbParameter As System.Data.Common.DbParameter = Nothing


    dbParameter = dbCommand.CreateParameter

    dbParameter.ParameterName = parameterName
    dbParameter.Value = parameterValue
    dbParameter.SourceColumn = sourceColumn

    dbCommand.Parameters.Add(dbParameter)

End Sub

...您将能够使用1行代码添加参数

Dim query As New SqlCommand("spGetInitStatusEntry", connection)
query.CommandType = CommandType.StoredProcedure
query.AddParameterValue("@ID", key)
Dim dt As New DataTable
Dim da As New SqlDataAdapter(query)
da.Fill(dt) 

该函数非常通用,未指定参数类型,但如果确实需要,可以将其添加为参数。

答案 2 :(得分:0)

query.AddWithValue(“@ ID”,key)