我有一个使用 Entity Framework 的 MVC 应用程序。我想将具有空值的参数传递给存储过程。在某些情况下,我想将 MerchantID 作为 null
传递。
GetValues(int[] TicketID,int? MerchantID,bool IsOpen)
{
//TicketID has values 1123,1122 etc
//MerchantID sometimes null
//IsOpen true/false
DataTable tbldepartmentid = new DataTable("Departmentid");
tbldepartmentid.Columns.Add("VALUE", typeof(int));
foreach (var id in TicketID)
tbldepartmentid.Rows.Add(id);
List<GetTroubleTicketDetails_Result> GetTroubleTicketDetails = _getTroubleTicketDetails_Result.ExecuteCustomStoredProc("Tickets.GetDetails", " @GroupID,@MerchantID,@Open",
new SqlParameter("GroupID", SqlDbType.Structured) { Value = tbldepartmentid, TypeName = "dbo.tblTVPForCSVINT" }
, new SqlParameter("MerchantID", MerchantID)
, new SqlParameter("Open", IsOpen)).ToList();
return GetTroubleTicketDetails;
}
我的问题是,当我传递 MerchantID=null 时,出现以下错误:
"The parameterized query '(@GroupID [dbo].[tblTVPForCSVINT] READONLY,@MerchantID nvarchar(' expects the parameter '@MerchantID', which was not supplied."
如何为 MerchantID 传递空值?
最佳答案
您需要传递 SqlInt32.Null
而不是 null
,如下所示:
new SqlParameter("MerchantID", MerchantID ?? SqlInt32.Null)
关于c# - 如何使用 Entity Framework 将空值传递给存储过程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201548/