简体   繁体   English

将DEFAULT值传递到新的SqlParameter()中

[英]Passing DEFAULT value into new SqlParameter()

How can i pass DEFAULT value into stored procedure in MVC4 .NET ? 如何将DEFAULT值传递到MVC4 .NET中的存储过程中? I have this code 我有这个代码

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();

I tried 我试过了

  • "DEFAULT" --> Error: Conversion Failed "DEFAULT" ->错误:转换失败
  • null --> Error: parameter was not passed null >错误:未传递参数
  • DBNull.Value --> it pass N'' value but this Value is not DEFAULT DBNull.Value >它传递N''值,但此值不是默认值

How can i do this ? 我怎样才能做到这一点 ?

I found a workaround but it works only for one default parameter. 我找到了一种解决方法,但是它仅适用于一个默认参数。

return Database.SqlQuery<int>(
                "spGetProgramsCount " + (searchString != null ? "@p1" : ""),
                (searchString != null ? new SqlParameter("p1", searchString) : null)
            ).First();

but when I have more parameters for example 但是当我有更多参数时

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.

and i believe it is because commas in the procedure, i don't know how to handle it :/ 而且我相信这是因为该过程中的逗号,我不知道该如何处理:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM