当存储过程包含输出参数时,可以进一步传入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(); }