當存儲過程包含輸出參數時,可以進一步傳入out形式的SqlParameter對象以獲取輸出參數,參考以下的存儲過程:
CREATE PROCEDURE [dbo].[SetSPriceById]
@P1 int = 0 ,
@P2 int = 0 ,
@P3 int = 0 ,
@P4 int OUT
AS
UPDATE Product SET SPrice = @P1 WHERE Id > @P2 AND Id < @P3
SET @P4 = @@ROWCOUNT
//using StoredProcedureOut.Model;
//using System.Data.SqlClient;
using (var context = new KTStoreContext())
{
string sp = "SetSPriceById @P1,@P2, @P3,@P4 OUT";
SqlParameter[] parameters = {
new SqlParameter("P1", 100),
new SqlParameter("P2", 10),
new SqlParameter("P3", 15),
new SqlParameter("P4", System.Data.SqlDbType.Int) {
Direction = System.Data.ParameterDirection.Output
}
};
context.Database.ExecuteSqlCommand(sp, parameters);
Console.WriteLine("變更數據項數:{0}",parameters[3].Value.ToString());
Console.ReadLine();
}