简体   繁体   中英

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. when i execute the SP i am getting correct result in sql server but i can't get in c#. Can you please tell me how can i get the output value in c#. Below is the function i am trying to get the output value in 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#.

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.

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();
}            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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