Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ADO.NET connection events

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use ADO.NET connection events, the article is very detailed, has a certain reference value, interested friends must read it!

In ADO.NET, there is a concept of event, and the so-called event is an action that happens. Here, we will talk about the ADO.NET connection event in detail. The Connection object in all .NET Framework data providers has two events that can be used to retrieve informational messages from the data source or to determine whether the state of the Connection has been changed. The following table describes these events for the Connection object.

Occurs when the state of the Connection changes. Using the InfoMessage ADO.NET connection event, you can use the InfoMessage ADO.NET connection event of the SqlConnection object to retrieve warnings and informational messages from the SQL Server data source. Errors returned from the data source with a severity of 11 to 16 will throw an exception. However, the InfoMessage event can be used to get messages from the data source that are not associated with the error. For Microsoft SQL Server, any error with a severity equal to or less than 10 is considered an informational message and will be caught using the InfoMessage event. For more information, see the "error message severity" topic in SQL Server online Books.

The InfoMessage ADO.NET connection event receives a SqlInfoMessageEventArgs object that contains a collection of messages from the data source in its Errors property. You can query the Error object in this collection to get the error number and message text, as well as the source of the error. The SQL Server .NET Framework data provider also contains details about the database, stored procedure, and line number from which the message comes from.

Example

The following code example shows how to add an event handler for the InfoMessage event.

Visual Basic

'Assumes that connection represents a SqlConnection object. AddHandler connection.InfoMessage, _ New SqlInfoMessageEventHandler (AddressOf OnInfoMessage) Private Shared Sub OnInfoMessage (sender As Object, _ args As SqlInfoMessageEventArgs) Dim err As SqlError For Each err In args.Errors Console.WriteLine ("The {0} has received a severity {1}, _ state {2} error number {3}\ n" & _ "on line {4} of procedure {5} on server {6}:\ n {7}", _ err.Source, err.Class, err.State, err.Number, err.LineNumber _ err.Procedure, err.Server, err.Message) Next End Sub

C#

/ / Assumes that connection represents a SqlConnection object. Connection.InfoMessage + = new SqlInfoMessageEventHandler (OnInfoMessage); protected static 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}\ n" + "on line {4} of procedure {5} on server {6}:\ n {7}", err.Source, err.Class, err.State, err.Number, err.LineNumber, err.Procedure, err.Server, err.Message) } these are all the contents of the article "how to use ADO.NET connection events". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report