[英]In C# ADO.NET, how can I tell if a sproc returned 0 rows versus the sproc had no commands to run on TSQL?
In C# ADO.NET, how can I tell if a sproc returned 0 rows versus the sproc had no commands to run on TSQL?在 C# ADO.NET 中,我如何判断 sproc 返回 0 行与 sproc 是否没有命令在 TSQL 上运行?
Let me lay it out in full.让我把它完整地列出来。 In TSQL I have (when I open this in SSMS)
在 TSQL 我有(当我在 SSMS 中打开它时)
ALTER PROC mySproc
@myvar VARCHAR(10)
AS
/* commented out on dev system - uncomment on production */
/* guess what didn't happen */
/* careful readers will note that we _normally_
throw an exception when we do this. this sproc was missing said exception. */
When run in SSMS mySproc 'value'
this says "Command(s) completed successfully."在 SSMS
mySproc 'value'
中运行时,显示“命令已成功完成”。 Under better circumstances (aka just no data returned but the body of the sproc is actually running), it would return what looked like a table but with no rows.在更好的情况下(也就是没有返回数据,但存储过程的主体实际上正在运行),它会返回看起来像一个表但没有行的东西。
and in C#:在 C# 中:
using( SqlDataReader reader = cmd.ExecuteReader() ){
//reader.HasRows == false
}
So, that's what I ran into, and without using "sql reflection" (ergo reading the actual script in the database) and without using an ORM (because then I would know that I was sending the right commands, but we have our reasons for using sprocs, of course)...所以,这就是我遇到的问题,没有使用“sql 反射”(阅读数据库中的实际脚本),也没有使用 ORM(因为那时我会知道我发送了正确的命令,但我们有理由使用存储过程,当然)...
How can I tell that I got a result of "Command(s) completed successfully."我怎么知道我得到了“命令成功完成”的结果。 instead of a potential table with just no rows?
而不是一个没有行的潜在表?
I would suggest auditing SQL Server and beating the crap out of developers that actually create SPROCs without bodies.我建议审核 SQL 服务器并击败实际上创建没有主体的 SPROC 的开发人员的废话。 I know of no way to determine stupidity in SQL Server from a reader other than actually issuing an audit when there are no rows (running something like sp_helptext and determine if there is a body?).
除了在没有行时实际发出审计(运行 sp_helptext 之类的东西并确定是否有正文?)之外,我没有办法从读者那里确定 SQL 服务器中的愚蠢性。 It is hard to protect from sloppiness.
很难防止马虎。 Sorry!
对不起!
I originally put this in a comment but it is more appropriate as an answer我最初把它放在评论中,但它更适合作为答案
You can use the FieldCount
property on the reader to determine the number of columns.您可以使用阅读器上的
FieldCount
属性来确定列数。 For example:例如:
using( SqlDataReader reader = cmd.ExecuteReader() )
{
if (reader.FieldCount > 0)
{
// there are columns
}
else
{
// there are no columns, so stored proc returning no results set
}
}
From the MSDN page on FieldCount
来自
FieldCount
上的 MSDN 页面
Executing a query that, by its nature, does not return rows (such as a DELETE query), sets FieldCount to 0. However.
执行本质上不返回行的查询(例如 DELETE 查询),会将 FieldCount 设置为 0。但是。 this should not be confused with a query that returns 0 rows (such as SELECT * FROM table WHERE 1 = 2) in which case FieldCount returns the number of columns in the table, including hidden fields.
这不应与返回 0 行的查询混淆(例如 SELECT * FROM table WHERE 1 = 2),在这种情况下 FieldCount 返回表中的列数,包括隐藏字段。 Use VisibleFieldCount to exclude hidden fields.
使用 VisibleFieldCount 排除隐藏字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.