SqlConnection - InfoMessage Event
上一篇 /
下一篇 2008-07-03 09:44:48
/ 个人分类:ADO.Net
1. Event Handler
SqlInfoMessageEventHandler(object sender , SqlInfoMessageEventArgs e)
2. The InfoMessage event occurs when a message with a severity of 10 or less is returned by Sql Server. Message that has a severity between 11 and 20 raise an error and message that have a severity over 20 causes the connection to close.
3. SqlInfoMessageEventArgs.Errors property is a collection of the message from the data source.(type: SqlErrorCollection)
4. Handling errors as InfoMessages
If you want to continue processing the rest of the statemenents in a command regardless of any errors produced by the server, set the FireInfoMessageEventOnUserErrors property of the SqlConnection to true. Doing this causes the connection to fire InfoMessage event for errors instead of throwing an exception and interrupting processing. The client application can then handle this event and respond to error conditions.
5. Examples
using System;
using System.Data;
using System.Data.SqlClient;
class InfoMessage
{
public static void OnInfoMessage(object mySender, SqlInfoMessageEventArgs args)
{
foreach (SqlError err in args.Errors)
{
Console.WriteLine("Error Message: " + err.Message);
}
}
public static void Main()
{
SqlConnection cnt = new SqlConnection(
"server=someserver;database=Northwind;uid=sa;pwd=sa");
cnt.InfoMessage += new SqlInfoMessageEventHandler(OnInfoMessage);
cnt.Open();
SqlCommand cmd = cnt.CreateCommand();
cmd.CommandText = "PRINT'Info message from the PRINT statement'";
cmd.ExecuteNonQuery();
cmd.CommandText = "RAISERROR('Info message from the RAISERROR statement', 10, 1)";
cmd.ExecuteNonQuery();
cnt.Close();
Console.ReadLine();
}
}
导入论坛
引用链接
收藏
分享给好友
推荐到圈子
管理
举报
TAG: