C# ExecuteQuery 空值 [英] C# ExecuteQuery null value

查看:94
本文介绍了C# ExecuteQuery 空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码:

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.

现在有了字符串,这很容易,我只需传递 String.Empty 就可以了.但是,如果我想传递空日期,这是一个问题.

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.

我显然尝试过:

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: 查询参数不能是 System.Object 类型.

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

经过一些阅读,我发现 ExecuteCommand 不支持空参数,而规范断言它应该支持.

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?

非常感谢

推荐答案

您是否尝试过:

DBNull.Value

这篇关于C# ExecuteQuery 空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆