C# ExecuteQuery 空值
我有一些代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有没有尝试过:
Have you tried:
您是否尝试过发送
DBNull.Value
或新的Nullable()
?Have you tried sending
DBNull.Value
or newNullable<DateTime>()
?