这是我的存储过程,查询返回一个包含三列的表,每列中的总量。
UnitPrice,Tax,OrderTotal
ALTER PROCEDURE [dbo].[sp_TaxOrderReport]
-- Add the parameters for the stored procedure here
@StartDate date,
@EndDate date,
@TaxReturnValue int,
@TotalReturnValue int,
@UnitReturnValue int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT Sum(oi.UnitPrice) as UnitPrice, Sum(oi.Tax) as Tax,
Sum(oi.TotalAmount) as OrderTotal
FROM OrderItems oi, Orders o
where @StartDate <= o.OrderDate and O.OrderDate <= @EndDate
and o.OrderId = oi.OrderId
select @TaxReturnValue as Tax
select @UnitReturnValue as UnitPrice
select @TotalReturnValue as OrderTotal
-- How can I return several values over??
return @TaxReturnValue
END
我的目标是拥有一个管理员输入日期,并获得税前总销售税和资金的报告。这就是我的方法在我的控制器中的样子。
public ActionResult GetTaxReport(string StartDate, string EndDate)
{
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("sp_TaxOrderReport", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = StartDate;
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = EndDate;
cmd.Parameters.Add("@TaxReturnValue", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@UnitReturnValue", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@TotalReturnValue", SqlDbType.Int).Value = 0;
cmd.ExecuteNonQuery();
var returnvalue = cmd.Parameters["@TaxReturnValue"].Value.ToString();
}
connection.Close();
}
return View("FindUser");
}
@TaxReturnValue没有设置为列中的值,我似乎无法找到设置所有这三个变量的方法,然后将它们全部返回,以便我可以在我的视图中渲染它们。如果有人有任何提示我会很感激。
-----UPDATE--------------
我想我会更新问题以包含答案,以防万一需要有关如何返回和设置传入存储过程的变量的非常明确的答案。
存储过程:
ALTER PROCEDURE [dbo].[sp_TaxOrderReport]
-- Add the parameters for the stored procedure here
@StartDate date,
@EndDate date,
@TaxReturnValue int OUTPUT,
@TotalRevenue int OUTPUT,
@UnitReturnValue int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
--Insert statements for procedure here
SELECT
@TaxReturnValue = Sum(oi.Tax),
@UnitReturnValue = Sum(oi.UnitPrice),
@TotalRevenue = Sum(oi.TotalAmount)
FROM OrderItems oi, Orders o
where @StartDate <= o.OrderDate and O.OrderDate <= @EndDate
and o.OrderId = oi.OrderId
END
C#代码:
string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlCommand cmd = new SqlCommand("sp_TaxOrderReport", connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = StartDate;
cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = EndDate;
cmd.Parameters.Add("@TaxReturnValue", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@UnitReturnValue", SqlDbType.Int).Value = 0;
cmd.Parameters.Add("@TotalRevenue", SqlDbType.Int).Value = 0;
cmd.Parameters["@TaxReturnValue"].Direction = ParameterDirection.Output;
cmd.Parameters["@TotalRevenue"].Direction = ParameterDirection.Output;
cmd.Parameters["@UnitReturnValue"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
var taxTotal = cmd.Parameters["@TaxReturnValue"].Value;
var unitPriceTotal = cmd.Parameters["@UnitReturnValue"].Value;
var totalRenuve = cmd.Parameters["@TotalRevenue"].Value;
}
connection.Close();
}
感谢所有帮助人员!
答案 0 :(得分:2)
对于初学者,您需要设置ParameterDirection
否则@TaxReturnValue
不会被返回:
cmd.Parameters.Add("@TotalReturnValue", SqlDbType.Int).Value = 0;
cmd.Parameters["@TaxReturnValue"].Direction = ParameterDirection.ReturnValue;
但是,由于您没有正确填充它们,因此仍然无法返回您的值。你应该这样做:
SELECT @TaxReturnValue = Sum(oi.UnitPrice) as UnitPrice,
@UnitReturnValue=Sum(oi.Tax),
TotalReturnValue =Sum(oi.TotalAmount) as OrderTotal
FROM OrderItems oi, Orders o
where @StartDate <= o.OrderDate and O.OrderDate <= @EndDate
and o.OrderId = oi.OrderId
--select @TaxReturnValue as Tax
--select @UnitReturnValue as UnitPrice
--select @TotalReturnValue as OrderTotal
然后,您应该将这些变量指定为OUTPUT
参数:
ALTER PROCEDURE [dbo].[sp_TaxOrderReport]
-- Add the parameters for the stored procedure here
@StartDate date,
@EndDate date,
@TaxReturnValue int OUTPUT,
@TotalReturnValue int OUTPUT,
@UnitReturnValue int OUTPUT
AS
你需要在C#中使用类似的东西:
cmd.Parameters["@TaxReturnValue"].Direction = ParameterDirection.Output;
cmd.Parameters["@TotalReturnValue "].Direction = ParameterDirection.Output;
cmd.Parameters["@UnitReturnValue "].Direction = ParameterDirection.Output;
或者,您可以使用select语句返回结果集(不使用变量),有关详细信息,请参阅documentation。
答案 1 :(得分:0)
您应该在过程
中指定要返回的参数的输出变量...
@EndDate date,
@TaxReturnValue int output,
@TotalReturnValue int,
...
您也可以查看此post