从函数返回2项

本文关键字:2项 返回 函数 | 更新日期: 2023-09-27 18:12:38

我有存储过程,我将执行其结果并将其传递到一个数据集并返回它,但同时我在我的存储过程中有一个输出参数,我也想返回,我怎么能返回数据集和整数输出值与我的函数,或者有一个更"合适的方式"来处理这种情况?请多多指教,谢谢。

Public Function SelectCampaignManagementTableListing(ByVal objCampaignManagmentClassBLL As CampaignManagementBLL, ByVal dbConnection As DbConnection, ByVal SortExpression As String, ByVal SortFilterText As String, ByVal SortFilterField As String, ByVal SortDirection As String, ByVal PageNumber As Integer, ByVal PageCount As Integer, ByVal TotalCount As Integer) As DataSet
            Dim dbCommand As DbCommand = Nothing
            Dim retDS As DataSet = Nothing
            Try
                If dbConnection.State <> ConnectionState.Open Then
                    dbConnection.Open()
                End If
                dbCommand = GetStoredProcedureCommand("Campaign_LoadListing")
                dbCommand.Connection = dbConnection
                With objCampaignManagmentClassBLL
                    Select Case SortFilterField
                        Case "Code"
                            AddInParameter(dbCommand, "@Code", DbType.String, 50, DBNull.Value)
                            dbCommand.Parameters("@Code").Value = SortFilterText
                        Case "Name"
                            AddInParameter(dbCommand, "@Name", DbType.String, 150, DBNull.Value)
                            dbCommand.Parameters("@Name").Value = SortFilterText
                    End Select

                    Select Case SortDirection
                        Case "True"
                            AddInParameter(dbCommand, "@SortOrder", DbType.Boolean, 1, DBNull.Value)
                            dbCommand.Parameters("@SortOrder").Value = True
                        Case "False"
                            AddInParameter(dbCommand, "@SortOrder", DbType.Boolean, 1, DBNull.Value)
                            dbCommand.Parameters("@SortOrder").Value = False
                    End Select
                    AddInParameter(dbCommand, "@SortOrder", DbType.String, 50, DBNull.Value)
                    If Not String.IsNullOrEmpty(SortDirection) Then
                        dbCommand.Parameters("@SortOrder").Value = SortDirection
                    End If
                    Select Case SortExpression
                        Case "Code"
                            'sortType = "@SortByCampaignCode"
                            AddInParameter(dbCommand, "@SortByCampaignCode", DbType.Boolean, 1, DBNull.Value)
                            dbCommand.Parameters("@SortByCampaignCode").Value = True
                        Case "Name"
                            'sortType = "@SortByCampaignName"
                            AddInParameter(dbCommand, "@SortByCampaignName", DbType.Boolean, 1, DBNull.Value)
                            dbCommand.Parameters("@SortByCampaignName").Value = True
                        Case "Priority"
                            AddInParameter(dbCommand, "@SortByPriority", DbType.Boolean, 1, DBNull.Value)
                            dbCommand.Parameters("@SortByPriority").Value = True
                    End Select
                    AddInParameter(dbCommand, "@PageNumber", DbType.Int32, 4, DBNull.Value)
                    If Not IsNothing(PageNumber) Then
                        dbCommand.Parameters("@PageNumber").Value = PageNumber
                    End If
                    AddInParameter(dbCommand, "@PageCount", DbType.Int32, 4, DBNull.Value)
                    If Not IsNothing(PageCount) Then
                        dbCommand.Parameters("@PageCount").Value = PageCount
                    End If
' Heres the output parameter
                    Dim objOutputParameter As New SqlParameter("@Return_TotalCount", SqlDbType.Int)
                    dbCommand.Parameters.Add(objOutputParameter)
                    objOutputParameter.Direction = ParameterDirection.Output
                End With
' These are the 2 items I need to return.
                retDS = ExecuteDataSet(dbCommand)
                Dim tCount As Integer = CInt(dbCommand.Parameters("@Return_TotalCount").Value)
            Catch ex As Exception
                Throw New DALException(ex, dbCommand, Caching.GetCustomerCodeCookie(), "SelectCampaignManagementTableListing")
            Finally
                If Not dbCommand Is Nothing Then
                    dbCommand.Dispose()
                End If
            End Try
            Return retDS
        End Function

从函数返回2项

您有许多选择:

  1. 定义你自己的自定义类来公开你的输出属性,并返回该类的一个实例。
  2. 返回System的实例。包含输出值的元组。
  3. 使用输出参数返回你的输出值

可以使用具有DataSet属性和integer属性的类或结构体。通过在函数

中设置值,可以从函数中返回该类或结构。

为许多其他选择提供很少的链接:

  • 我如何从c#函数返回多个值?

  • 方法应该返回多个值

  • 一个函数可以返回两个值吗?

最简单的做法是,如果你的数据是相同的类型(字符串,int等),然后把它们放入一个列表,并传回该列表。

您可以创建一个封装DataSet对象和输出参数并通过函数名返回的自定义类,或者在函数中包含一个out参数以返回存储过程out参数并继续通过当前的函数名返回DataSet

除了Joviee给出的优秀答案外,您还有另一个选择,我们可以通过Action(Of Integer)Action(Of Exception)。使用操作使您能够从代码中返回零个或多个结果,而不是选项强制您只返回一个或一个结果。