1、SqlDependency What is it? :

  SqlDependency  Objects represent applications and SQL Server Query notification dependencies between instances . Applications can create a  SqlDependency  Object and register to pass OnChangeEventHandler  Event handler receives notification .

   It provides such a capability : When the data in the monitored database changes ,SqlDependency Will automatically trigger OnChange Event to inform the application , So that the system can update the data automatically ( Or cache ) Purpose .

   application : Scenes with high real-time data 、 Journalism 、 Monitoring data, etc .

   Can combine SignalR To achieve web Real time data update for .

2、 Usage method :

  1)sql server Set up :ALTER DATABASE <DatabaseName> SET ENABLE_BROKER; Statement to enable the corresponding database listening service , To support SqlDependency characteristic .

  2) Code : Console project ,web Project recommendation start Put it in Application_start In the method .

  3) Be careful : Cannot use... In a query statement *, The table name should be added [dbo].[xx].sql Statement to execute .

conn =ConfigurationManager.ConnectionStrings["default"].ToString();
//Start and Stop Method
SqlDependency.Start(conn);
Update(conn); private static void Update(string conn)
{
using (
SqlConnection connection =
new SqlConnection(conn))
{
// here it is to be noted that Out of commission * The table name should be added [dbo] Otherwise, there will be always call execution OnChange
string sql = "select agent from [dbo].[info]"; using (SqlCommand command = new SqlCommand(sql, connection))
{
connection.Open();
command.CommandType=CommandType.Text;
dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// It has to be carried out command
command.ExecuteNonQuery();
Console.WriteLine(dependency.HasChanges);
}
}
} //update insert delete Will enter
private static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
Console.WriteLine("onchange In the method :"+dependency.HasChanges);
Console.WriteLine(" Database data changes "+DateTime.Now);
// Here we call again
Update(conn);
}

SqlDependency More articles about the use of

  1. OutputCache Properties, ( Four )— SqlDependency

    Catalog OutputCache Concept learning OutputCache Properties, ( One ) OutputCache Properties, ( Two ) OutputCache Properties, ( 3、 ... and ) OutputCache Properties, ( Four )— SqlD ...

  2. When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.

    In the debug SignalR The program , Prompt for an exception :When using SqlDependency without providing an options value, SqlDependency.Star ...

  3. SqlDependency Database synchronization +signalr Push message

    sqlDependency Provides such a capability : When the data in the monitored database changes ,SqlDependency Will automatically trigger OnChange Event to inform the application , So that the system can update the data automatically ( Or cache ) Purpose . ...

  4. Enterprise Library Deep analysis and flexible application (2): adopt SqlDependency Realization Cache and Database Synchronization of

    For a real enterprise application ,Caching It must be a factor that has to be considered , reasonable . Make effective use of Caching For enhanced applications Performance( Reduce the impact on Persistent storage Of IO operation ) ...

  5. SqlDependency Cache database table case

    SqlDependency An introduction to the : SqlDependency yes outputcache A parameter of Web caching , Its role is to specify the database dependencies that are cache retired , It can be specific to databases and tables . SqlDependency Can solve ...

  6. WPF Update database changes in non polling mode SqlDependency( Database modification, foreground automatic update )

    In the last chapter we talked about wpf The histogram component of , It includes updating database changes without polling SqlDependency The content of , But there was no detailed explanation , Now let's give you a simple example to illustrate this part . Previous chapter : WPF Histogram ( Support number ...

  7. SqlDependency It doesn't work

    Use today SqlDependency, The results didn't work , invalid , No matter how the database is modified , There's no response here ,OnChange Events are never executed , It's strange . I opened the msdn The example in , Copy the code , It works , Can execute , The rest asked ...

  8. sqlDependency Monitoring database data changes , Automatic notification

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. c# Real time monitoring database SqlDependency

    http://blog.csdn.net/idays021/article/details/49661855 class Program { private static string _connSt ...

Random recommendation

  1. AppDomain Detailed explanation ( turn )

    AppDomain yes CLR The running unit of , It can load Assembly. Creating objects and executing programs . AppDomain yes CLR The basic mechanism of code isolation . every last AppDomain It can run alone . stop it : Every AppDo ...

  2. flex Layout notes

    1, There is a problem today , It's when the element layout is set to flex after , It's just words , But yes. text-align Invalid property setting , Think about it carefully , It's because of putting display Set up in order to flex after ,flex Think of the words in it as ...

  3. oracle System package —-dbms_output usage

    dbms_output Packages are mainly used for debugging pl/sql Program , Or in sql*plus The information in the command (displaying message) And reports , For example, we can write a simple anonymous pl/sql Block , And the block is out of some kind of ...

  4. UML Tool selection

    Today I'm thinking about UML Choice of tools , Personal requirements are relatively simple : Be able to draw use case diagrams , Sequence diagram , Activity diagram is enough . There are three main tools to choose : 1.Enterprise Architect 2.Power Designer 15 3. ...

  5. Chinese software development project Teacher's pain

    At a recent meeting , Some senior officials said that they had done a great job in China before , However, after contacting with their American counterparts, they found that there was a very big gap between them , I had the same experience when I worked in a foreign company . Only when we really get in touch with foreign counterparts can we know what the gap is , In this article, I ...

  6. JS in replace() Usage example

    grammar : string.replace(regexp,replacement) Parameters : regexp: Declaring the pattern to be replaced RegExp object . If the parameter is a string , Then use it as the direct text pattern to retrieve , Instead of ...

  7. Linux Lower installation mysql(yum And source compilation two ways )

    Here are Linux The next two installations mysql The way :yum Installation and source code compilation installation . 1. yum install (1) First of all to see centos Self contained mysql Is it installed : # yum list installed |grep ...

  8. Android Dynamic modification ToolBar Of Menu menu

    Android Dynamic modification ToolBar Of Menu menu design sketch Realization It's easy to implement , It's a person with 3 individual Action Of Menu, As we slide into different states , Put the corresponding Action Hide the . Start loading Menu Me ...

  9. Synchronous and asynchronous usage of process pool Pool

    Synchronization of process pools , The following procedure : from multiprocessing import Pool import time import os def func(n): print('start process %s' ...

  10. golang String interception

    java There is SubString, that golang How to intercept strings ? str := "hello world" fmt.Print(str[:]) Above code output hello Array ,sli ...