简体   繁体   English

在 C# ADO.NET 中,我如何判断 sproc 返回 0 行与 sproc 是否没有命令在 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?

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.

相关问题 从SQL Server中的Sproc返回到ADO.NET应用程序的最大数据量是多少 - What is the maximum amount of data that can be returned from a sproc in sql server to ADO.NET application 如何使用SQLAnywhere 10从ADO.NET 2.0中的SPROC中获取LongVarchar的参数? - How do I get LongVarchar out param from SPROC in ADO.NET 2.0 with SQLAnywhere 10? 如何通过C#传递sproc的参数 - How to pass parameter for sproc via C# 加快70,000行(SProc,C#)的插入 - Speed up the insert of 70,000 rows (SProc, C#) 我必须在aspx中创建数据源吗? 我可以在C#中执行sqldatasource(sql SPROC),参数和databind吗 - Do I have to create a datasource in aspx? Can I do sqldatasource (sql SPROC), parameters and databind in C# 如何使用ADO.NET创建属性为数据库表中行的C#类? - How to create a C# class whose attributes are rows in a database table with ADO.NET? 使用SAP HANA扩展服务(XS)与ADO.NET/C#进行应用程序开发 - Using SAP HANA Extended Services (XS) versus ADO.NET/C# for application development 可以从SPROC创建/调用C#吗? - Possible to create/call C# from a SPROC? 我可以使用ADO.NET和C#在MS SQL Express 2014 LocalDB中创建表吗? - Can I create table in MS SQL Express 2014 LocalDB using ADO.NET and C#? 如何使用ADO.Net C#将null插入到sql数据库中 - how to insert null into sql database with ADO.Net C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM