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:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-11-19  
      1
2345678
9101112131415
16171819202122
23242526272829
30      

数据统计

  • 访问量: 1428
  • 日志数: 20
  • 建立时间: 2008-05-19
  • 更新时间: 2008-08-22

RSS订阅

Open Toolbar