Is it possible to coalesce string and DBNull in C#?

前端 未结 10 1333
滥情空心
滥情空心 2021-01-02 06:24

I\'m writing a C# routine to call a stored proc. In the parameter list I\'m passing in, it is possible that one of the values can legally be null. So I thought I\'d use a

10条回答
  •  攒了一身酷
    2021-01-02 06:41

    In your stored proc when you declare the incoming variable, have it set the var equal to null and then do not pass it in from your csharp code, it will then pick up the default value from sql

    @theParam as varchar(50) = null
    

    and then in your csharp

    if (theParam != null)
        cmd.Parameters.Add(new SqlParameter("@theParam", theParam));
    

    This is how I usually pass option and/or defaulted values to my stored procs

提交回复
热议问题