调用标量值函数时,ExecuteScalar 始终返回 null
为什么这会返回 null?
//seedDate is set to DateTime.Now; con is initialized and open. Not a problem with that
using (SqlCommand command = new SqlCommand("fn_last_business_date", con))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
object res = command.ExecuteScalar(); //res is always null
}
但是,当我直接在数据库中调用它时,如下所示:
select dbo.fn_last_business_date('8/3/2011 3:01:21 PM')
returns '2011-08-03 15:01:21.000'
这是我从代码调用它时期望看到的结果
为什么,为什么,为什么?
Why does this return null?
//seedDate is set to DateTime.Now; con is initialized and open. Not a problem with that
using (SqlCommand command = new SqlCommand("fn_last_business_date", con))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@seed_date", seedDate);//@seed_date is the param name
object res = command.ExecuteScalar(); //res is always null
}
But when I call this directly in the DB as follows:
select dbo.fn_last_business_date('8/3/2011 3:01:21 PM')
returns '2011-08-03 15:01:21.000'
which is the result I expect to see when I call it from code
Why, why, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么每个人都坚持使用
select
语法?...Why does everyone insist on the
select
syntax?..尝试:
try:
您实际上收到了一个未被捕获的错误。您不像调用存储过程那样调用标量 udf。
将 udf 包装在存储过程中,或者更改语法。我实际上不确定那是什么,因为它并不常见......
啊哈:请参阅这些问题:
You are actually getting an error that isn't being caught. You don't call scalar udfs like you call stored procedures.
Either wrap the udf in a stored proc, or change syntax. I'm not actually sure what that is because it isn't common...
Ah ha: see these questions: