InfoMessage
{Sql |OleDb}InfoMessageEventHandler InfoMessage
Fires when a warning or informational messages is sent from a data
source. If the message corresponds to an error, an exception is
raised instead of this event being fired. Information about the
message is stored in a provider-specific EventArgs
object, such as OleDbInfoMessageEventArgs
or
SqlInfoMessageEventArgs
.
This event isn’t used to receive specific error messages (in SQL Server, these are messages with a severity greater than 10) that will cause an exception to be thrown.
{Sql|OleDb}InfoMessageEventArgs e
The InfoMessageEventArgs
object is
provider-specific. Typically, it includes information such as the
error number and message text, as well as the additional
provider-specific information. For example, SQL Server provides
information about the database, stored procedure, and line number
from which the message originated.
Here’s an example event handler for the
InfoMessage
event. It displays some of the
retrieved information in a console window:
private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args) { foreach (SqlError err in args.Errors) { Console.WriteLine("The {0} has received a severity {1}, " + "state {2} error number {3} on line {4} of procedure {5} " + "on server {6}", err.Source, err.Class, err.State, err.Number, err.LineNumber, err.Procedure, err.Server); } }