2011-10-26 87 views
2

我有存儲過程,我將執行其結果並將其傳遞到數據集並返回它,但同時我有我的存儲過程中的輸出參數,我也想返回,怎麼可以我用函數返回數據集和整數輸出值,還是有更合適的方法來處理這種情況?請提供建議,謝謝。從函數返回2項

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 
+0

感謝所有有用的答案,如果這看起來不合適,但我剛剛閱讀有關Structs,是否他們是更好的選擇,然後使用自定義類? – k80sg

+0

可能的重複http://stackoverflow.com/questions/2434222/is-it-possible-for-a-function-to-return-two-values?lq=1 –

回答

8

你有多種選擇:

  1. 定義你自己暴露你的輸出屬性的類,並返回一個類的實例。
  2. 返回包含您的輸出值的System.Tuple的實例。
  3. 使用輸出參數返回您的輸出值。
0

您可以創建一個自定義類來封裝DataSet對象和輸出參數,並通過函數的名稱返回該參數,或者爲該函數包含out參數以返回存儲過程出參數並繼續返回DataSet當前的功能名稱。

0

另一個選擇是,除了Joviee給出的優秀答案,我們還通過了Action(Of Integer)Action(Of Exception)。通過使用操作,您可以從代碼中返回零個或多個結果,而不是隻有一個選項強制您執行該操作。

1

最簡單的事情就是如果你的數據是相同的類型(字符串,int等),然後把它們放入一個列表並傳回該列表。沒有自定義任何要求。