将DEFAULT值传递给新的SqlParameter()
本文关键字:SqlParameter DEFAULT 值传 | 更新日期: 2023-09-27 18:02:56
如何将DEFAULT值传递到MVC4 .NET中的存储过程中?我有这个代码
return Database.SqlQuery<Program>(
"spGetProgram @p1,@p2,@p3,@p4",
new SqlParameter("p1", (object)ID ?? "DEFAULT"),
new SqlParameter("p2", (object)displayStart ?? "DEFAULT"),
new SqlParameter("p3", (object)displayLength ?? "DEFAULT"),
new SqlParameter("p4", (object)searchString ?? "DEFAULT")
).ToList();
我试着
-
"DEFAULT"
-> Error: Conversion Failed -
null
-->错误:参数未传递 -
DBNull.Value
->它传递N "值,但这个值不是默认值
我找到了一个解决方案,但它只适用于一个默认参数。
return Database.SqlQuery<int>(
"spGetProgramsCount " + (searchString != null ? "@p1" : ""),
(searchString != null ? new SqlParameter("p1", searchString) : null)
).First();
但是当我有更多的参数时例如
return Database.SqlQuery<int>(
"spGetProgramsCount" + (searchString != null ? " @p1," : "")
+ (searchString2 != null ? " @p2," : "")
+ (searchString3 != null ? " @p3," : ""),
(searchString != null ? new SqlParameter("p1", searchString) : null), (searchString2 != null ? new SqlParameter("p2", searchString2) : null), (searchString3 != null ? new SqlParameter("p3", searchString3) : null)
).First();
I am getting
When executing a command, parameters must be exclusively database parameters or values.
和我认为这是因为逗号在过程中,我不知道如何处理它:/