SqlDataSource.SqlCacheDependency Property
Assembly: System.Web (in system.web.dll)
'Declaration Public Overridable Property SqlCacheDependency As String 'Usage Dim instance As SqlDataSource Dim value As String value = instance.SqlCacheDependency instance.SqlCacheDependency = value
/** @property */ public String get_SqlCacheDependency () /** @property */ public void set_SqlCacheDependency (String value)
public function get SqlCacheDependency () : String public function set SqlCacheDependency (value : String)
Not applicable.
Property Value
A string that indicates which databases and tables to use for the SQL Server cache dependency.The SqlDataSource control supports an optional expiration policy based on the SqlCacheDependency object for the data cache (the service must be configured for the database server).
The SqlCacheDependency string identifies databases and tables according to the same format that is used by the @ Page directive, where the first part of the string is a connection string to a Microsoft SQL Server database, followed by a colon delimiter, and finally the name of the database table (for example, "connectionstring1:table1"). If the SqlCacheDependency property depends on more than one table, the connection string-and-table name pairs are separated by semicolons (for example, "connectionstring1:table1";connectionstring2:table2").
![]() |
---|
When you are using client impersonation under Microsoft Windows authentication, the data is cached when the first user accesses the data. If another user requests the same data, the data is retrieved from the cache. The data is not retrieved by making another call to the database to verify the user's access to the data. If you expect more than one user to access the data, and you want each retrieval to the data to be verified by the security configurations for the database, do not use caching. |
The following code example demonstrates how to create a SQL Server cache dependency and set the SqlCacheDependency property of a SqlDataSource control. In this example, the database is polled every 120 seconds. If the data in the Northwind Traders Employees table changes during that time, any data that is cached by the SqlDataSource control and displayed by the GridView control is refreshed by the SqlDataSource control the next time the database is polled.
<%@ Page language="vb" %> <!-- The page uses an example configuration that includes connection strings and a defined SqlCacheDependecy. <?xml version="1.0"?> <configuration> <connectionStrings> <add name="MyNorthwind" connectionString="Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind"" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <cache> <sqlCacheDependency enabled="true"> <databases> <add name="Northwind" connectionStringName="MyNorthwind" pollTime="120000" /> </databases> </sqlCacheDependency> </cache> </system.web> </configuration> --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:gridview id="GridView1" runat="server" datasourceid="SqlDataSource1" /> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:MyNorthwind%>" selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees" enablecaching="True" cacheduration="300" cacheexpirationpolicy="Absolute" sqlcachedependency="Northwind:Employees" /> </form> </body> </html>
<%@ Page language="VJ#" %> <!-- The page uses an example configuration that includes connection strings and a defined SqlCacheDependecy. <?xml version="1.0"?> <configuration> <connectionStrings> <add name = "NorthwindConnection" connectionString = "Data Source="localhost";Integrated Security="SSPI";Initial Catalog="Northwind"" /> </connectionStrings> <system.web> ... <cache> <sqlCacheDependency enabled="true"> <databases> <add name="Northwind_Remote" connectionStringName="NorthwindConnection" pollTime="120000" /> </databases> </sqlCacheDependency> </cache> </system.web> </configuration> --> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>ASP.NET Example</title> </head> <body> <form id="Form1" method="post" runat="server"> <asp:gridview id="GridView1" runat="server" datasourceid="SqlDataSource1" /> <asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind" selectcommand="SELECT EmployeeID,FirstName,Lastname FROM Employees" enablecaching="True" cacheduration="300" cacheexpirationpolicy="Absolute" sqlcachedependency="Northwind_Remote:Employees" /> </form> </body> </html>