SET NOCOUNT ON,并使用C#和ADO.NET读取消息 [英] SET NOCOUNT ON and reading messages using C# and ADO.NET

查看:221
本文介绍了SET NOCOUNT ON,并使用C#和ADO.NET读取消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  
    

SET NOCOUNT ON停止,显示受Transact-SQL语句或从返回的结果集的一部分存储过程的行数的计数的消息。

  


a)如何可以读取这些信息使用C#和ADO.NET(我假设C#code阅读这些消息都是一样的,无论T-SQL语句是在存储过程,批处理或执行...)?


二)假设存储过程包含几个语句,如何在你的C#code确定为不特定消息指什么SQL语句?


感谢您

解决方案

信息性消息(如行受到影响计数方式)通过的 SqlConnection.InfoMessage 事件。添加一个委托时,将调用每当服务器发送通知消息(即任何错误消息严重性波纹管10)。

有没有办法关联信息像源afffected计数信息消息。你将拥有它基于逻辑的知识和理解,第一条消息是指第一个更新做,第二个消息,第二个更新等。

依托受影响的行数在客户端是generaly一个不好的做法。在许多问题ORM层像NHibernate和当SET NOCOUNT ON开启时只显示了如何有问题的这种做法是ADO.Net数据集都有。


SET NOCOUNT ON stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set.


a) How can you read these messages using C# and ADO.NET ( I assume C# code reading these messages is the same regardless of whether T-SQL statements were executed in a stored procedure, batch or… )?


b) Assuming stored procedure contains several statements, how can your C# code identify to what SQL statement does particular messages refer to?


Thank you

解决方案

Informational messages (like the rows affected count info) are reported in ADO.Net through the SqlConnection.InfoMessage event. Add a delegate to the event and will be invoked whenever the server transmits an informational message (ie. any error message with severity bellow 10).

there is no way to associate informational messages like afffected count info with the source. You're going to have to do it based on knowledge of the logic and understand that the first message refers to the first update, the second message to the second update etc.

Relying on affected rows count in the client is generaly a bad practice. The many issues ORM layers like NHibernate and ADO.Net datasets have when SET NOCOUNT ON is turned on just shows how problematic this practice is.

这篇关于SET NOCOUNT ON,并使用C#和ADO.NET读取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆