我试图在c#函数中获取存储过程的输出参数的值。当我执行SP时,我在sql server中得到了正确的结果,但我无法进入c#。你能告诉我如何在c#中获得输出值。下面是我试图在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;
}
}
我在这里调用函数: -
DataSet _ds = _orderPicDAC.GetOrderItemPictureByOrderId(OrderID, PageIndex);
我的商店程序: -
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
提前感谢您的帮助。
答案 0 :(得分:2)
如果要在方法之外获取输出参数的值而不更改方法的返回类型,可以在C#中使用输出参数。
更改您的方法定义,如下所示:
public DataSet GetOrderItemPictureByOrderId(int orderID, int PageIndex, out string outputValue)
{
//code here
outputValue = db.GetParameterValue(cmd, "@RecordCount");
//code here
}
然后调用此方法
string outvalue;
DataSet _ds = _orderPicDAC.GetOrderItemPictureByOrderId(OrderID, PageIndex,out outvalue;);
您将获得outvalue变量中的值。
答案 1 :(得分:0)
您可以尝试使用以下
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();
}
}