1

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

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

3 回答 3

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})
于 2012-02-29T22:07:38.030 回答
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) 

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

于 2012-02-29T23:54:54.670 回答
0

query.AddWithValue("@ID", 键)

于 2012-02-29T22:05:20.010 回答