C# ExecuteQuery 空值

发布于 2024-08-19 00:05:51 字数 948 浏览 5 评论 0原文

我有一些代码:

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

但是我也希望能够将空值传递给存储过程,这样它就不会使用它们。

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

我显然尝试过:

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

但这不起作用,给出错误:

System.Exception:查询参数不能是 System.Object 类型。

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

有人遇到过这个问题并找到解决方法吗?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

裸钻 2024-08-26 00:05:51

你有没有尝试过:

DBNull.Value

Have you tried:

DBNull.Value
说好的呢 2024-08-26 00:05:51

您是否尝试过发送 DBNull.Value 或新的 Nullable()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文