我試圖從EF使用context.Database.ExecuteSqlCommand
調用存儲過程,因爲我的一個參數是數據表。使用ExecuteSqlCommand調用存儲過程(期望沒有提供的參數)
這裏是過程的參數:
ALTER PROCEDURE [mySchema].[myProc]
@customerId INT,
@indicatorTypeId INT,
@indicators [mySchema].[IndicatorList] READONLY,
@startDate DATETIME,
@endDate DATETIME
這裏是C#代碼中調用存儲過程:
var indicatorsDt = new DataTable();
indicatorsDt.Columns.Add("Date", typeof(DateTime));
indicatorsDt.Columns.Add("Ongoing", typeof(int));
indicatorsDt.Columns.Add("Success", typeof(int));
indicatorsDt.Columns.Add("Warning", typeof(int));
indicatorsDt.Columns.Add("Error", typeof(int));
indicatorsDt.Columns.Add("Other", typeof(int));
var customerIdParam = new SqlParameter("customerId", SqlDbType.Int);
customerIdParam.Value = customerId;
var typeIdParam = new SqlParameter("indicatorTypeId", SqlDbType.Int);
typeIdParam.Value = typeId;
var startDateParam = new SqlParameter("startDate", SqlDbType.DateTime);
startDateParam.Value = startDate;
var endDateParam = new SqlParameter("endDate", SqlDbType.DateTime);
endDateParam.Value = endDate;
foreach (var indicator in indicators)
{
indicatorsDt.Rows.Add(indicator.Date, indicator.Ongoing,
indicator.Success, indicator.Warning,
indicator.Error, indicator.Other);
}
var tableParameter = new SqlParameter("indicators", SqlDbType.Structured);
tableParameter.Value = indicatorsDt;
tableParameter.TypeName = "MySchema.IndicatorList";
context.Database.ExecuteSqlCommand("exec MySchema.MyProc", customerIdParam, typeIdParam, tableParameter, startDateParam, endDateParam);
正如你所看到的,提供了所有的參數,它們中沒有一個空值,但我總是得到這個SqlException
:
過程或函數'UpdateIndicators'期望參數 '@customerId',未提供。
我找不出我缺少的東西。是否使用SqlParameter
錯誤?即使參數不重要,參數在ExecuteSqlCommand
中的提供順序也是相同的。
提前致謝。
不,它不工作要麼,我得到同樣的例外。 –