【发布时间】:2021-05-03 17:52:00
【问题描述】:
我想在 ASP.NET Core MVC 中执行具有 ADO.NET 输出参数的存储过程。但是我收到一条错误消息,说我的存储过程需要一个我已经设置的参数。
我的存储过程是这样的:
ALTER PROCEDURE [dbo].[spSearch]
@Notes nvarchar(max) = NULL,
@StartDate datetime = NULL,
@EndDate datetime = NULL,
@UserId nvarchar(450),
@Result money OUTPUT
AS
BEGIN
IF (@StartDate > @EndDate)
BEGIN
RAISERROR (N'Start date cannot be greater than end date',16,1)
END
ELSE
BEGIN
IF (@Notes IS NULL OR @Notes = '')
BEGIN
SELECT *
FROM Expenses
WHERE UserId = @UserId
AND [date] BETWEEN @StartDate AND @EndDate
ORDER BY [date] DESC
IF (@@ROWCOUNT = 0)
BEGIN
RAISERROR (N'No data found according to date match', 16, 1)
END
SELECT @Result = SUM(amount)
FROM Expenses
WHERE UserId = @UserId
AND [date] BETWEEN @StartDate AND @EndDate
END
ELSE
BEGIN
IF (@StartDate IS NULL OR @StartDate = '' OR @EndDate IS NULL OR @EndDate = '')
BEGIN
SELECT *
FROM expenses
WHERE UserId = @UserId
AND Notes LIKE '%' + @notes + '%'
ORDER BY [date] DESC
SELECT @Result = SUM(amount)
FROM expenses
WHERE UserId = @UserId
AND notes IN (SELECT notes
FROM expenses
WHERE notes LIKE '%' + @Notes + '%')
END
ELSE
BEGIN
SELECT *
FROM Expenses
WHERE UserId=@UserId and notes LIKE '%' + @notes + '%' and [date] BETWEEN @StartDate AND @EndDate
ORDER BY [date] DESC
IF( @@ROWCOUNT = 0 )
BEGIN
RAISERROR(N'No matching record found',16,1)
END
SELECT @Result = Sum(amount)
FROM Expenses
WHERE Notes IN(SELECT Notes
FROM Expenses
WHERE UserId=@UserId and notes LIKE '%' + @Notes + '%')
AND [date] BETWEEN @StartDate AND @EndDate
END
END
END
END
我的方法是这样的:
public IQueryable<Expenses> SearchExpenses(string UserId, string Notes, DateTime startFrom, DateTime endTo)
{
List<Expenses> expenses = new List<Expenses>();
using (DALC.GetConnection())
{
DALC.Command("[spSearch]");
DALC.cmd.Parameters.Add("@Notes", SqlDbType.NVarChar).Value = Notes;
DALC.cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = startFrom;
DALC.cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = endTo;
DALC.cmd.Parameters.Add("@UserId", SqlDbType.NVarChar).Value = UserId;//I set my sp paremeter here but why error?
//Is This the correct way to set an output parameter?
SqlParameter howMuch = new SqlParameter("@Result", SqlDbType.Money);
howMuch.Direction = ParameterDirection.Output;
DALC.cmd.Parameters.Add(howMuch);
DALC.cmd.ExecuteNonQuery();
var result = Convert.ToDecimal(DALC.cmd.Parameters["@Result"]);
using (SqlDataReader reader = DALC.cmd.ExecuteReader())
{
reader.Read();
Expenses expense = new Expenses
{
Id = Convert.ToInt32(reader["Id"]),
Desription = reader["Notes"].ToString(),
Date = Convert.ToDateTime(reader["Date"]),
IsCash = Convert.ToBoolean(reader["IsCash"]),
IsCard = Convert.ToBoolean(reader["IsCash"]),
SearchResultOut = result
};
expenses.Add(expense);
}
}
return expenses.AsQueryable();
}
错误:
System.Data.SqlClient.SqlException: '过程或函数'spSearch' 需要未提供的参数“@UserId”。
还有我的控制器:
[HttpPost]
public IActionResult Search(string UserId, string notes, DateTime startFrom, DateTime endTo)
{
UserId = GetUserId();
context.SearchExpenses(UserId,notes,startFrom,endTo);
return View();
}
【问题讨论】:
-
你能出示你的 DALC 吗?
-
您是否将CommandType 设置为
StoredProcedure? -
@Sergey 是的,但是这不是我第一次使用这个class.. 到目前为止还没有问题..
-
@Zer0 肯定接受a look..