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 the StateChange event of ADO.NET

2025-02-23 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 's StateChange event, the article is very detailed, has a certain reference value, interested friends must read it!

ADO.NET is still quite common, so I studied the use of ADO.NET StateChange events, and here I share with you that ADO.NET StateChange events occur when the state of Connection changes. The ADO.NET StateChange event receives the StateChangeEventArgs, which enables you to use the OriginalState and CurrentState properties to determine a change in the Connection state.

The ◆ OriginalState property is an ConnectionState enumeration that indicates the Connection state before the change.

◆ CurrentState is an ConnectionState enumeration that indicates the changed Connection state.

The following code example uses the ADO.NET StateChange event to write a message to the console when the state of the Connection changes.

Visual Basic

'Assumes connection represents a SqlConnection object. AddHandler connection.StateChange, _ New StateChangeEventHandler (AddressOf OnStateChange) Protected Shared Sub OnStateChange (_ sender As Object, args As StateChangeEventArgs) Console.WriteLine (_ "The current Connection state has changed from {0} to {1}.", _ args.OriginalState, args.CurrentState) End Sub

C#

/ / Assumes connection represents a SqlConnection object. Connection.StateChange+= new StateChangeEventHandler (OnStateChange); protected static void OnStateChange (object sender, StateChangeEventArgs args) {Console.WriteLine ("The current Connection state has changed from {0} to {1}.", args.OriginalState, args.CurrentState);}}

Analyze the case ADO.NET connection test

Easily handle ADO.NET and connect to MySQL

Parsing ADO.NET connection string injection attacks

How to configure ADO.NET connection string

Analyze and summarize the security of ADO.NET connection information

Typically, only informational and warning messages from the server trigger the InfoMessage event. However, when a real error occurs, the ExecuteNonQuery or ExecuteReader method that starts the server operation pauses execution and throws an exception. If you want to continue working on the rest of the statement in the command regardless of any errors generated by the server, set the FireInfoMessageEventOnUserErrors property of SqlConnection to true. Doing so causes the connection to fire InfoMessage events for errors, rather than throwing an exception and interrupting processing. The client application can handle this event and respond to the error condition.

An error with a severity equal to or greater than 17 will cause the server to stop processing commands, which must be handled as an exception. In this case, an exception is thrown no matter how the error is handled in the InfoMessage event.

The above is all the content of the article "how to use ADO.NET 's StateChange event". 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