What you need is to enable SqlCacheDependency as @RenusRusanu mentioned.
I have several working examples on my Github site, feel free to browse
the code under my LearningProjects (3rd party edit)
You need to configure the database first in order to use SqlCacheDependency:
In order to use a SQL Dependency, we need to configure basically two things in the database:
Create the database infrastructure which consists on a table, and store procedures, to configure it we use the aspnet_regsql.exe
as follows:
/* For SQL Server authentication... */
aspnet_regsql.exe -S server -U user -P password -d database -ed
/* For Windows Authentication... */
aspnet_regsql.exe -S server -E -d database -ed
Create the polling information which consists to specify the table to monitor and a trigger to that table to populate the infrastructure table needed
/* For SQL Server authentication... */
aspnet_regsql.exe -S server
-U user -P password -d database -t tableName -et
/* For Windows Authentication... */
aspnet_regsql.exe -S server
-E -d database -t tableName -et
Code configuration
var s = new SqlCacheDependency("AdventureWorks", "Product");
HttpContext.Current.Cache.Insert(
"products",
h,
s,
Cache.NoAbsoluteExpiration,
Cache.NoSlidingExpiration);
The AdventureWorks
name is the name of the sql dependency configured in the web.config file
<caching>
<sqlCacheDependency enabled="true" pollTime="30000" >
<databases>
<add
name="AdventureWorks"
connectionStringName="AdventureWorksPolling" />
</databases>
</sqlCacheDependency>
</caching>
The pollTime
can be configured in the <add ...
element and will override the global polltime
Alternatively you can rely on the SqlDataSource control
In order to enable caching when using this control, you need to set the DataSourceMode
property to DataSet
. In this case, you do not need to configure the database as explained before
I just uploaded this example (full working example)
ASPX
<asp:SqlDataSource runat="server" ID="sds"
ConnectionString="<%$ConnectionStrings:pubsConnectionString %>"
CacheDuration="30"
EnableCaching="true"
SelectCommand="select * from jobs"
DataSourceMode="DataSet"
OnSelecting="sds_Selecting">
</asp:SqlDataSource>
<div>
<asp:Literal runat="server" ID="msg" Mode="Encode"></asp:Literal>
</div>
<asp:GridView runat="server" DataSourceID="sds"></asp:GridView>
Edit 1
In order to fire the trigger selectively when using SqlCacheDependency, you would need to update the triggers generated manually.
When you configure a table by using the aspnet_regsql -t -et...
command, a trigger is added to the specified table. This trigger is in charge to populate the AspNet_SqlCacheTablesForChangeNotification
table used by the SqlCacheDependency object to poll data from the database.
The trigger looks like:
ALTER TRIGGER [dbo].[jobs_AspNet_SqlCacheNotification_Trigger] ON [dbo].[jobs]
FOR INSERT, UPDATE, DELETE AS BEGIN
SET NOCOUNT ON
EXEC dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedure N'jobs'
END
You could update the trigger to just call the dbo.AspNet_SqlCacheUpdateChangeIdStoredProcedureN'jobs'
stored procedure when the rows you are interested in are updated
I'm not a SQL expert, but I think it would be easy to find a clean way to detect the rows that have changed, for example, googling I just found this link
Most efficient method to detect column change in MS SQL Server