Asp.Net Signal R - Detect Changes in Database ? As

2019-04-02 07:38发布

问题:

So I have a List View inside an Update Panel

Update Panel
    List View
        Email 1
        Email 2
        Email 3
           ...

I'm trying to do an inbox similar to GMAIL in ASP.NET, the only I'm struggling with is how to detect DataBase Changes (ie when a new message is sent) and Push that message into the ListView to simulate that the user has received a new message (just like gmail does)

How do I use SignalR to detect database changes and push them into the List View using SignalR? Is it possible?

回答1:

If you are using Sql Server follow this link. http://techbrij.com/database-change-notifications-asp-net-signalr-sqldependency

It basically uses SqlDependency to subscribe changes in Sql Server.

If you aren't using Sql Server you have to do this manually. And for view side you can use KnockoutJS or anngular for easy list modification.

//Set up dependency
protected void Application_Start()
{
        //...
   SqlDependency.Start(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 
}


protected void Application_End()
{
    SqlDependency.Stop(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 
}


SqlDependency dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);


回答2:

I dont think you can use SignalR to detect changes in the database, but to push the changes to the web-site. Use something like SqlDependency or SQL Notification Services to detect the changes in the database and then SignalR to push them to the web-page.