我的存储过程: -
DECLARE @return_value int,
@Sum int,
@Total float
EXEC @return_value = [dbo].[mysamplesp]
@sd = '2014-09-23 00:00:00.000',
@ed = '2014-09-30 00:00:00.000',
@ID = '40494',
@sp = NULL,
@Ct = NULL,
@GD = NULL,
@Sum = @Sum OUTPUT,
@Total = @Total OUTPUT
SELECT @Sum as N'@Sum',
@Total as N'@Total'
SELECT 'Return Value' = @return_value
执行结果; -
数据集1 -
col1 col2 col3 col4 col5
datase2 -
@Sum @Total
数据集3 -
return_value
我的C#实现: -
Dictionary<string, object> storage = new Dictionary<string, object>();
storage.Add("sd", "2014-09-23 00:00:00.000");
storage.Add("ed", "2014-09-30 00:00:00.000");
storage.Add("ID", "40494");
storage.Add("Sp", null);
storage.Add("Ct", null);
storage.Add("GD", null);
storage.Add("Sum", null);
storage.Add("Total", null );
var spReturn = new Data(Data.DBKEY).ExecuteSotredProcedure("mysamplesp", storage);
。 。
public int ExecuteSotredProcedure(string qryName, Dictionary<string, object> parameters)
{
using (SqlConnection con = new SqlConnection(_conStr))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(qryName, con))
{
cmd.CommandType = CommandType.StoredProcedure;
foreach (var key in parameters.Keys)
{
cmd.Parameters.AddWithValue(key, parameters[key] ?? DBNull.Value);
}
return cmd.ExecuteNonQuery();
}
}
}
Q1:如何使用C#中的字典将输出参数发送到Sp?可能?
Q2:如何从SP中获取多个数据集?
答案 0 :(得分:2)
对于Q1,您应该将sql参数direction
指定为ParameterDirection.Output
对于Q2,请使用SqlDataReader
var cmd = sqlConnection.CreateCommand();
cmd.CommandText = "[dbo].[mysamplesp]";
cmd.CommandType = CommandType.StoredProcedure;
sqlConnection.Open();
var reader = cmd.ExecuteReader();
// process first dataset
reader.NextResult();
// process second dataset
// etc