简体   繁体   English

如何在C#中获取存储过程输出参数

[英]how to get Stored Procedure output parameter in c#

I am trying to get the value of output parameter of a stored procedure in c# function. 我正在尝试获取c#函数中存储过程的输出参数的值。 when i execute the SP i am getting correct result in sql server but i can't get in c#. 当我执行SP时,我在sql server中得到正确的结果,但是我无法进入c#。 Can you please tell me how can i get the output value in c#. 您能告诉我如何在C#中获得输出值吗? Below is the function i am trying to get the output value in C# DAC. 以下是我尝试获取C#DAC中的输出值的函数。

public DataSet GetOrderItemPictureByOrderId(int orderID, int PageIndex)
    {
        DataSet dtOrder = new DataSet();

        /// Connect to database.
        Database db = DatabaseFactory.CreateDatabase(CONNECTION_NAME);
        using (DbCommand cmd = db.GetStoredProcCommand("uspGetOrderItemPicturePageWise"))
        {
            /// Set parameter values.
            db.AddInParameter(cmd, "@OrderID", DbType.Int32, orderID);
            db.AddInParameter(cmd, "@PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(cmd, "@PageSize", DbType.Int32, 6);
            db.AddOutParameter(cmd, "@RecordCount", DbType.Int32, 4);

            try
            {
                dtOrder = db.ExecuteDataSet(cmd);
                string outputValue = cmd.Parameters["@RecordCount"].Value.ToString(); // here i want to get the output value and want to return the value to main code 
            }
            catch (Exception ex)
            {
                LogErrors.WriteError(ex);
            }
        }
        return dtOrder;
    }
}

Here i am calling the function :- 在这里,我正在调用函数:-

 DataSet _ds = _orderPicDAC.GetOrderItemPictureByOrderId(OrderID, PageIndex);  

My Store Procedure :-- 我的商店流程:-

 CREATE PROCEDURE [dbo].[uspGetOrderItemPicturePageWise]    
      @OrderID int,    
      @PageIndex INT = 1    
      ,@PageSize INT = 10    
      ,@RecordCount INT OUTPUT    
AS    
BEGIN    
      SET NOCOUNT ON;    
      SELECT ROW_NUMBER() OVER    
      (    
            ORDER BY [PictureId] ASC    
      )AS RowNumber,    
      Orderid,    
      GenericFieldID,    
      ItemImage    
     INTO #Results    
      FROM [OrderPictures]    
      WHERE OrderID = @OrderID  
      SELECT @RecordCount = COUNT(*)    
      FROM #Results    

      SELECT * FROM #Results    
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1    

      DROP TABLE #Results    
END

Thanks for your help in advance. 感谢您的帮助。

If you want to get value of output parameter outside the method without changing the return type of the method, you may use out parameters in C#. 如果要在方法外部获取输出参数的值而不更改方法的返回类型,则可以在C#中使用out参数。

Change your method definition like below: 更改您的方法定义,如下所示:

public DataSet GetOrderItemPictureByOrderId(int orderID, int PageIndex, out string outputValue)
{
    //code here
    outputValue = db.GetParameterValue(cmd, "@RecordCount");
    //code here

}

and then call this method 然后调用此方法

 string outvalue;
 DataSet _ds = _orderPicDAC.GetOrderItemPictureByOrderId(OrderID, PageIndex,out outvalue;);  

You will have the value in outvalue variable. 您将在outvalue变量中拥有值。

You can try with below 您可以尝试以下

using (SqlConnection con = new SqlConnection(dc.Con))
{
using (SqlCommand cmd = new SqlCommand("SP_ADD", con))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@OrderID", DbType.Int32, orderID);
    con.Open();
    cmd.ExecuteNonQuery();
}            
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM