久久久久久久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 語句有什么作用?)
主站蜘蛛池模板: 国内精品一区二区三区 | 成人免费视频网站在线观看 | 91精品国产乱码久久久久久久 | 欧美a区 | 欧美极品在线观看 | 成人在线视频一区二区三区 | 国产精品久久久久久吹潮 | 在线成人 | 亚洲综合大片69999 | 日韩在线免费播放 | 超碰男人天堂 | 国产精品免费一区二区三区 | 国产免费高清 | 亚洲美女视频 | 久久精品黄色 | .国产精品成人自产拍在线观看6 | 日韩欧美国产一区二区三区 | 亚洲精品1区 | 日韩高清在线观看 | 久久久久国产一区二区三区 | 国产成人精品一区二区三区在线 | 国产精品福利在线观看 | 免费国产精品久久久久久 | 国产成人久久精品 | 一本一道久久a久久精品蜜桃 | 精品在线免费看 | 99久久久久| 精品久久久久国产 | 亚洲免费福利视频 | 久久久久久久av | 日韩成人久久 | 亚洲性视频网站 | 欧美激情精品久久久久 | 午夜精品久久久久久 | 国产99视频精品免费播放照片 | 日韩精品| 一级免费在线视频 | 热99视频| 久久伊人免费视频 | 欧美男人天堂 | 国产在线精品一区二区三区 |