ITPub博客

首页 > Linux操作系统 > Linux操作系统 > SqlConnection - InfoMessage Event

SqlConnection - InfoMessage Event

原创 Linux操作系统 作者:stone0823 时间:2008-07-03 09:44:48 0 删除 编辑

 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();
  }
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/782823/viewspace-368952/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Connection Pooling
请登录后发表评论 登录
全部评论

注册时间:2008-05-19

  • 博文量
    13
  • 访问量
    50913