久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

當數據庫更新時如何通知我的程序?

How can I notify my program when the database has been updated?(當數據庫更新時如何通知我的程序?)
本文介紹了當數據庫更新時如何通知我的程序?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 C# 程序可以查詢 SQL Server 數據庫的某些值.

I have a C# program that queries the SQL Server database for some values.

目前,應用程序每分鐘查詢一次數據庫,以確保表是最新的.

Currently the application queries the database every minutes to make sure that the table is up to date.

我希望能夠做的是查詢僅在數據庫已更改/更新時完成.當數據庫中的某些內容更新時,我如何通知我的程序?

What I would like to be able to do is that the query is only done when the database has been changed / updated. How do I notify my program when something has been updated in the database?

謝謝

推薦答案

輪詢數據庫不是很優雅的解決方案.

Polling database is not very elegant solution.

SqlDependency 對您的情況很有用.它不使用輪詢而是使用通知機制.通知由 Service Broker 在您的數據庫中提供,因此需要在您的數據庫中啟用此服務.OnChange 事件將在指定的表更改(更新、刪除、插入...)時引發

SqlDependency from ADO.NET will be useful in your case. It does not use polling but notification mechanism. The notifications are provided by Service Broker in your database, so will need to enable this service in your databse. The OnChange event will raise when specified table changes(update, delete, insert..)

這是一個如何使用SqlDependency的例子:

Here is an example how to use SqlDependency:

void Initialization()
{
    // Create a dependency connection.
    SqlDependency.Start(connectionString, queueName);
}

void SomeMethod()
{
    // Assume connection is an open SqlConnection.

    // Create a new SqlCommand object.
    using (SqlCommand command=new SqlCommand(
        "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers", 
        connection))
    {

        // Create a dependency and associate it with the SqlCommand.
        SqlDependency dependency=new SqlDependency(command);
        // Maintain the refence in a class member.

        // Subscribe to the SqlDependency event.
        dependency.OnChange+=new
           OnChangeEventHandler(OnDependencyChange);

        // Execute the command.
        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Process the DataReader.
        }
    }
}

// Handler method
void OnDependencyChange(object sender, 
   SqlNotificationEventArgs e )
{
  // Handle the event (for example, invalidate this cache entry).
}

void Termination()
{
    // Release the dependency.
    SqlDependency.Stop(connectionString, queueName);
}

來自 http://msdn.microsoft.com/en-us/圖書館/62xk7953.aspx

以下是如何啟用 Service Broker(請注意,您將擁有數據庫的獨占性來執行此操作 - 最好在重新啟動 sql server 后執行此操作):http://blogs.sftsrc.com/stuart/archive/2007/06/13/42.aspx(鏈接失效)

Here is how to enable Service Broker(note that you will have exclusiveness on the database to do that - best do it after restart of the sql server): http://blogs.sftsrc.com/stuart/archive/2007/06/13/42.aspx(Broken link)

可能的替代鏈接:http://technet.microsoft.com/en-us/library/ms166086(v=sql.105).aspx

這篇關于當數據庫更新時如何通知我的程序?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發問題)
SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
what does a using statement without variable do when disposing?(處理時不帶變量的 using 語句有什么作用?)
主站蜘蛛池模板: 天天看片天天干 | 欧美成人一区二区 | 久久国产精彩视频 | 97国产精品 | 美女久久久 | 欧美激情久久久 | 日韩欧美一二三区 | 亚洲视频在线一区 | 亚洲免费一区二区 | 国产精品美女www爽爽爽视频 | 黄免费观看 | 国产精品视频不卡 | 欧美综合自拍 | 婷婷丁香在线视频 | 日韩在线一区二区三区 | 国产免费一级片 | 综合久久久 | 国产成人av在线 | 成人国产精品久久 | 美女久久久久久久久 | 日韩一区二区三区四区五区六区 | 久久精品欧美视频 | 99精品观看 | 日本中文字幕在线视频 | 欧美精品一区二区免费 | www.中文字幕.com| 久久久免费少妇高潮毛片 | 国产精品久久久久久亚洲调教 | 青青草av| 欧洲高清转码区一二区 | 欧美一级欧美三级在线观看 | 亚洲精品国产第一综合99久久 | 久久久久久久97 | 精品国产高清一区二区三区 | 精品美女久久久 | 日韩三极 | 黄色免费在线观看网站 | 一级在线免费观看 | 韩日视频在线观看 | 亚洲一区不卡 | 成人免费视频网站在线看 |