在VB.Net中使用ParamArray传递对象或对象数组作为参数

时间:2019-02-01 20:29:20

标签: asp.net vb.net

我正在放屁。

我正在尝试建立一个函数,该函数可以将对象作为其参数。此函数应输出数据表。这是我到目前为止所没有的功能。

定义的输入

'Example 1
GetDataTableWithParams("..myStoredProcedure", {333, SqlDbType.Int, "@UserID"})

'Example 2
GetDataTableWithParams("..myStoredProcedure", [{333, SqlDbType.Int, "@UserID"}, {9090, SqlDbType.Int, "@ProfileID"}]

预期输出

'Example 1
Dim params(0) As IDataParameter
params(0) = Data.GetDataParameter(333, 8, "@UserID")

'Example 2
Dim params(1) As IDataParameter
params(0) = Data.GetDataParameter(333, 8, "@UserID")
params(1) = Data.GetDataParameter(9090, 8, "@ProfileID")

实际输出

'Example 1
params(0) DataAction.GetDataParameter(333
params(1) DataAction.GetDataParameter(8
params(2) DataAction.GetDataParameter(@UserID

我的功能

Public Function GetDataTableWithMultipleParams(ByVal _StoredProcedure As String, ByVal ParamArray _Params() As Object) As DataTable

        If _Params.Length <= 1 Then Exit Function

            Dim params(_Params.Length) As IDataParameter

            For i As Integer = 0 To UBound(_Params, 1)
                Console.WriteLine("params(" & i & ") " & "DataAction.GetDataParameter(" & _Params(i), ")")
            Next


        'Dim table As DataTable = DataAction.GetDataTableParametrized(_StoredProcdure, CommandType.StoredProcedure, params)
        'Return table

End Function

1 个答案:

答案 0 :(得分:0)

以下是史蒂文D的提议(无论如何,我认为)。我希望你明白就OK了。

    Option Explicit On

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ''Example2
            myStoredProcedure({New HoldItems(Value:=333, SqlType:=SqlDbType.Int, Name:="@UserID"),
                              New HoldItems(Value:=9090, SqlType:=SqlDbType.Int, Name:="@ProfileID")})
        End Sub

        Public Sub myStoredProcedure(ByVal ParamArray _Params() As Object)
            Dim _class As HoldItems
            Dim intI As Integer

            For intI = 0 To UBound(_Params)
                _class = DirectCast(_Params(intI), HoldItems)

                Debug.Print("Name=" & _class.Name & " Value=" & _class.Value & " SqlType=" & _class.SqlType)
            Next intI
        End Sub
    End Class

    Public Class HoldItems
        Public Value As Integer
        Public SqlType As Integer
        Public Name As String

        Public Sub New(Value As Integer, SqlType As SqlDbType, Name As String)
            Me.Value = Value
            Me.SqlType = SqlType
            Me.Name = Name
        End Sub
    End Class