【问题标题】:Is there a better way to call a Stored Procedure with SqlParameters in VB?有没有更好的方法在 VB 中使用 SqlParameters 调用存储过程?
【发布时间】:2012-02-29 21:58:57
【问题描述】:

有人知道在 VB.NET 中创建和初始化SqlParameters 的更好方法吗?每个变量使用 3 行似乎非常过分。不幸的是,这个类的constructors 相当荒谬,所以我正在考虑编写自己的子程序来初始化每个参数。这就是我一直以来的做法。

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()>s。

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()

【问题讨论】:

  • 我做了一个扩展方法,以 SqlCommand 类为目标来处理添加参数。
  • 天哪,你们让我很难选择。 The_Black_Smurf 和 LarsTech 都给出了出色的答案。我选择了一种混合动力车。社区指南建议在出现平局时将答案提供给代表较少的人作为礼貌。对于任何寻找答案的人来说,这两个都很棒。 :)

标签: vb.net sqlparameter


【解决方案1】:

您也可以合并前两行:

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})

【讨论】:

  • 我不太了解&lt;Extension()&gt; 方法,即使在玩过它并阅读MSDN 之后也是如此。如果我接受 Mike 的建议,那么我该如何定义 DataType?如果它可以是隐含的,那真的是个好主意吗?谢谢:)
  • @Chiramisu 扩展方法很有用。可能很难解释:您必须将扩展代码放在模块中并用&lt;Extension()&gt; _ 属性(Import System.Runtime.CompilerServices)装饰它,它基本上说“接受我的第一个参数并用它做点什么”。至于AddWithValue(),它会根据您传递的数据为您处理类型。我见过争论说它是好是坏,但我从来没有觉得它不适合我。我将发布一个更新,将参数显示为单行。
【解决方案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) 

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

【讨论】:

  • 我给你分数只是因为你的代表较少,而且你和 LarsTech 的答案都一样好,对我有很大帮助。谢谢你:)
【解决方案3】:

query.AddWithValue("@ID", key)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2022-06-10
    • 2012-05-01
    相关资源
    最近更新 更多