下面的代码给出了一个错误——“没有从DBnull到int的隐式转换”。
SqlParameter[] parameters = new SqlParameter[1];
SqlParameter planIndexParameter = new SqlParameter("@AgeIndex", SqlDbType.Int);
planIndexParameter.Value = (AgeItem.AgeIndex== null) ? DBNull.Value : AgeItem.AgeIndex;
parameters[0] = planIndexParameter;
这就是我所做的…
var PhoneParam = new SqlParameter("@Phone", DBNull.Value);
if (user.User_Info_Phone != null)
{
PhoneParam.SqlValue = user.User_Info_Phone;
}
return this.Database.SqlQuery<CustLogonDM>("UpdateUserInfo @UserName, @NameLast, @NameMiddle, @NameFirst, @Address, @City, @State, @PostalCode, @Phone",
UserNameParam, NameLastParam, NameMiddleParam, NameFirstParam, AddressParam, CityParam, StateParam, PostalParam, PhoneParam).Single();
你可以这样做。这里startDate和endDate是可空的datetime参数
var Statistics= db.Database.SqlQuery<ViewStatistics>("YourStoreProcedure_Or_sqlQuery @startDate,@endDate",
new SqlParameter("startDate", startDate?? (object)DBNull.Value),
new SqlParameter("endDate", endDate?? (object)DBNull.Value)
).ToList();