C# ExecuteQuery null value

拥有回忆 提交于 2019-12-08 01:32:18

问题


I have some code:

using (OAZISDBDataContext ctx = new OAZISDBDataContext())
            {                    
                IEnumerable<DBContactDetail> details = ctx.ExecuteQuery<DBContactDetail>("exec [dbo].[zna_contact] {0}, {1}",
                    "test", "19601023",
             }

However I also want to be able to pass empty values to the stored procedure so it just doesn't use them.

Now with strings this is easy, I can just pass String.Empty and it will work. However if I want to pass empty dates this is a problem.

I obviously tried:

using (OAZISDBDataContext ctx = new OAZISDBDataContext())
            {                    
                IEnumerable<DBContactDetail> details = ctx.ExecuteQuery<DBContactDetail>("exec [dbo].[zna_contact] {0}, {1}",
                    "test", null,
             }

But this doesn't' work, gives the error:

System.Exception: A queryparameter can't be of type System.Object.

After some reading I found out that the ExecuteCommand does not support null parameters while the spec asserts that it should.

Has anybody encountered this issue and found a workaround?

Thanks a bunch


回答1:


Have you tried:

DBNull.Value



回答2:


Have you tried sending DBNull.Value or new Nullable<DateTime>() ?



来源:https://stackoverflow.com/questions/2117228/c-sharp-executequery-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!