如何通过ADO.NET获取Sql Server消息? [英] How can i get the Sql Server Message through ADO.NET?

查看:88
本文介绍了如何通过ADO.NET获取Sql Server消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我们执行插入,更新,删除查询或存储过程时,我们会在sql server中收到类似5行受影响或错误消息或sql PRINT消息的消息。我很好奇如果可能的话,我怎么能通过ADO.NET获得这个消息。谢谢。

Whenever we execute an insert, update, delete query or stored procedure we get a message in sql server something like.. "5 rows affected" or an error message or a sql PRINT message. I curious how can i get this message through ADO.NET if its at all possible. Thanks.

推荐答案

SqlCommand.ExecuteNonQuery()始终返回受命令影响的行数。 />


检查这个:SqlCommand.ExecuteNonQuery方法 [ ^ ]



从存储过程返回@@ ROWCOUNT 返回受影响的行数



检查:修改存储过程数据 [ ^ ]



RETURN @@ ERROR 从存储过程中将返回错误



检查:@@ ERROR(Transact-SQL) [ ^ ]
SqlCommand.ExecuteNonQuery() always returns the number of rows affected by the command.

check this: SqlCommand.ExecuteNonQuery Method [^]

RETURN @@ROWCOUNT from the stored procedure returns the number of rows affected

check this: Modifying Data with Stored Procedures[^]

RETURN @@ERROR from stored procedure will return the error

check this: @@ERROR (Transact-SQL)[^]


Ly试试这个:

Ly try this:
SqlConnection connection = new SqlConnection("server=.;database=Northwind;integrated Security=SSPI;");

connection .InfoMessage += new SqlInfoMessageEventHandler(InfoMessageHandler);

static void InfoMessageHandler(object sender, SqlInfoMessageEventArgs e)
{
    string myMsg = e.Message;            
}



e.Message是打印到SQL Server Management Studio中消息窗口的消息。


The "e.Message" is the message printed out to the message window in SQL Server Management Studio.


这篇关于如何通过ADO.NET获取Sql Server消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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