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 get started quickly. NET 2.0 Sql Dependency

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how to quickly get started with. NET 2.0 Sql Dependency. The content is concise and easy to understand. It will definitely make your eyes shine. I hope you can gain something through the detailed introduction of this article.

SQL Server 2005 implementation is subject to SQL Server 2000 because of different implementation mechanisms, please refer to the relevant information.

Step 1: Execute the ALTER DATABASE SET ENABLE_BROKER; statement on SQL Server 2005 to enable listening services on the corresponding database to support the SqlDependency feature.

This statement is best executed without the database performing any transactions.

Step 2: Call the SqlDependency.Start(String strConnectionString) method to enable the dependency listener on the application side.

The parameter of this method is the connection string of a database, and the database must have performed the operation in step 1.

For the same connection string, if the statement has already been executed, no exception will occur if it is executed again, but the return value will be False.

If you are using it in a Web program, it is recommended that you execute the statement in the Application_Start event.

Listeners are database based, while dependencies can be table or query based.

Step 3: There are two different approaches to this step. Attention must be paid to the steps at this stage.

Method A: Create the connection object, create a SqlCommand instance, create a SqlCacheDependency instance, and then call the Command object to get the data (this order is important). The Cache Insert statement is then invoked to create a Cache entry that depends on a specific query dataset.

SqlConnection conn = new SqlConnection(strConnection);

SqlCommand command = new SqlCommand(strCommandText, conn);

SqlCacheDependency dependency = new SqlCacheDependency(command);

//Register method to delegate, the delegate

CacheItemRemovedCallback onRemove = new CacheItemRemovedCallback(RemovedCallback);

//Add or modify a cache record

Cache.Insert(strCacheKey, objAppCache, dependency, absoluteExpiration, slidingExpiration, CacheItemPriority.Default, onRemove);

Method B: Create a connection object, create a SqlCommand instance, and finally create a SqlDependency instance. Define OnChange delegate of SqlDependency, and make corresponding processing when data changes (such as clearing Cache).

SqlConnection conn = new SqlConnection(strConnection);

SqlCommand command = new SqlCommand(strCommandText, conn);

SqlCacheDependency dependency = new SqlCacheDependency(command);

dependency.OnChange += new OnChangeEventHandler(Dependency_OnChange);

Notes:

I don't know if there is still a BUG, I encountered some strange phenomena in the project development. The same code, running on some machines, can catch changes, some do not respond at all; sometimes there will be events that depend on changes repeatedly as soon as Cache is established. Occasionally Cache will also change data without raising an event.

But from the final project implementation situation, it seems that it is only uncertain factors caused by some machine environment. This cannot be determined. But at least whether the database side is enabled normally, you can view the monitoring through SQL Server Profiler.

The above is how to quickly get started with. NET 2.0 Sql Dependency. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to 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

Internet Technology

Wechat

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

12
Report