SqlCacheDependency Sınıf
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
bir ASP.NET uygulamasının Cache nesnesinde depolanan bir öğe ile belirli bir SQL Server veritabanı tablosu veya SQL Server 2005 sorgusunun sonuçları arasında bir ilişki kurar. Bu sınıf devralınamaz.
public ref class SqlCacheDependency sealed : System::Web::Caching::CacheDependency
public sealed class SqlCacheDependency : System.Web.Caching.CacheDependency
type SqlCacheDependency = class
inherit CacheDependency
Public NotInheritable Class SqlCacheDependency
Inherits CacheDependency
- Devralma
Örnekler
Aşağıdaki kod örneği, veritabanı tablosunu görüntülemek için ve SqlDataSource denetimlerini kullanırGridView. Sayfa yüklendiğinde, sayfa bir SqlCacheDependency nesne oluşturmaya çalışır.
SqlCacheDependency Nesne oluşturulduktan sonra, sayfa nesnesine Cache bir bağımlılık içeren bir SqlCacheDependency öğe ekler. Burada gösterilene benzer bir özel durum işleme kullanmanız gerekir.
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// <snippet2>
public void Page_Load(object Src, EventArgs E)
{
// Declare the SqlCacheDependency instance, SqlDep.
SqlCacheDependency SqlDep = null;
// Check the Cache for the SqlSource key.
// If it isn't there, create it with a dependency
// on a SQL Server table using the SqlCacheDependency class.
if (Cache["SqlSource"] == null) {
// Because of possible exceptions thrown when this
// code runs, use Try...Catch...Finally syntax.
try {
// Instantiate SqlDep using the SqlCacheDependency constructor.
SqlDep = new SqlCacheDependency("Northwind", "Categories");
}
// Handle the DatabaseNotEnabledForNotificationException with
// a call to the SqlCacheDependencyAdmin.EnableNotifications method.
catch (DatabaseNotEnabledForNotificationException exDBDis) {
try {
SqlCacheDependencyAdmin.EnableNotifications("Northwind");
}
// If the database does not have permissions set for creating tables,
// the UnauthorizedAccessException is thrown. Handle it by redirecting
// to an error page.
catch (UnauthorizedAccessException exPerm) {
Response.Redirect(".\\ErrorPage.htm");
}
}
// Handle the TableNotEnabledForNotificationException with
// a call to the SqlCacheDependencyAdmin.EnableTableForNotifications method.
catch (TableNotEnabledForNotificationException exTabDis) {
try {
SqlCacheDependencyAdmin.EnableTableForNotifications("Northwind", "Categories");
}
// If a SqlException is thrown, redirect to an error page.
catch (SqlException exc) {
Response.Redirect(".\\ErrorPage.htm");
}
}
// If all the other code is successful, add MySource to the Cache
// with a dependency on SqlDep. If the Categories table changes,
// MySource will be removed from the Cache. Then generate a message
// that the data is newly created and added to the cache.
finally {
Cache.Insert("SqlSource", Source1, SqlDep);
CacheMsg.Text = "The data object was created explicitly.";
}
}
else {
CacheMsg.Text = "The data was retrieved from the Cache.";
}
}
// </snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p>
</p>
<p>
<asp:SqlDataSource id="Source1" runat="server" SelectCommand="SELECT * FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName]=@CategoryName,[Description]=@Description,[Picture]=@Picture WHERE [CategoryID]=@CategoryID" ConnectionString="<%$ ConnectionStrings:Northwind %>"></asp:SqlDataSource>
<asp:GridView id="GridView1" runat="server" DataKeyNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView>
</p>
<p>
</p>
<p>
<asp:Label id="CacheMsg" runat="server" AssociatedControlID="GridView1"></asp:Label>
</p>
</form>
</body>
</html>
<%@ Page Language="VB" Debug="True" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' <snippet2>
Sub Page_Load(Src As Object, E As EventArgs)
' Declare the SqlCacheDependency instance, SqlDep.
Dim SqlDep As SqlCacheDependency
' Check the Cache for the SqlSource key.
' If it isn't there, create it with a dependency
' on a SQL Server table using the SqlCacheDependency class.
If Cache("SqlSource") Is Nothing
' Because of possible exceptions thrown when this
' code runs, use Try...Catch...Finally syntax.
Try
' Instantiate SqlDep using the SqlCacheDependency constructor.
SqlDep = New SqlCacheDependency("Northwind", "Categories")
' Handle the DatabaseNotEnabledForNotificationException with
' a call to the SqlCacheDependencyAdmin.EnableNotifications method.
Catch exDBDis As DatabaseNotEnabledForNotificationException
Try
SqlCacheDependencyAdmin.EnableNotifications("Northwind")
' If the database does not have permissions set for creating tables,
' the UnauthorizedAccessException is thrown. Handle it by redirecting
' to an error page.
Catch exPerm As UnauthorizedAccessException
Response.Redirect(".\ErrorPage.htm")
End Try
' Handle the TableNotEnabledForNotificationException with
' a call to the SqlCacheDependencyAdmin.EnableTableForNotifications method.
Catch exTabDis As TableNotEnabledForNotificationException
Try
SqlCacheDependencyAdmin.EnableTableForNotifications( _
"Northwind", "Categories")
' If a SqlException is thrown, redirect to an error page.
Catch exc As SqlException
Response.Redirect(".\ErrorPage.htm")
End Try
' If all the other code is successful, add MySource to the Cache
' with a dependency on SqlDep. If the Categories table changes,
' MySource will be removed from the Cache. Then generate a message
' that the data is newly created and added to the cache.
Finally
Cache.Insert("SqlSource", Source1, SqlDep)
CacheMsg.Text = "The data object was created explicitly."
End Try
Else
CacheMsg.Text = "The data was retrieved from the Cache."
End If
End Sub
' </snippet2>
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<p>
</p>
<p>
<asp:SqlDataSource id="Source1" runat="server" SelectCommand="SELECT * FROM [Categories]" UpdateCommand="UPDATE [Categories] SET [CategoryName]=@CategoryName,[Description]=@Description,[Picture]=@Picture WHERE [CategoryID]=@CategoryID" ConnectionString="<%$ ConnectionStrings:Northwind %>"></asp:SqlDataSource>
<asp:GridView id="GridView1" runat="server" DataKeyNames="CategoryID" AllowSorting="True" AllowPaging="True" DataSourceID="Source1"></asp:GridView>
</p>
<p>
</p>
<p>
<asp:Label id="CacheMsg" runat="server" AssociatedControlID="GridView1"></asp:Label>
</p>
</form>
</body>
</html>
Açıklamalar
SQL Server'nin desteklenen tüm sürümlerinde (Microsoft SQL Server 7.0, Microsoft SQL Server 2000 ve SQL Server 2005) SqlCacheDependency sınıfı belirli bir SQL Server veritabanı tablosunu izler. Tablo değiştiğinde, tabloyla ilişkili öğeler öğesinden Cachekaldırılır ve öğesine yeni bir öğe sürümü eklenir Cache.
SqlCacheDependency sınıfı, SQL Server 2005 veritabanı kullanırken System.Data.SqlClient.SqlDependency sınıfıyla tümleştirmeyi de destekler. SQL Server 2005'in sorgu bildirim mekanizması, bir SQL sorgusunun sonuçlarını geçersiz kılacak verilerde yapılan değişiklikleri algılar ve SQL sorgusuyla ilişkili önbelleğe alınmış öğeleri System.Web.Caching.Cache kaldırır.
SqlCacheDependency sınıfını kullanarak uygulamanızın Cache SQL Server 2005 kullanırken SQL Server veritabanı tablosuna veya SQL sorgusuna bağımlı öğeler ekleyebilirsiniz. Bu sınıfı @ OutputCache yönergesiyle kullanarak, çıktı önbelleğe alınmış bir sayfa veya SQL Server veritabanı tablosuna bağımlı bir kullanıcı denetimi de yapabilirsiniz. Son olarak, SqlCacheDependency sınıfını @ OutputCache sayfası yönergesi ile kullanarak çıktı önbelleğe alınmış bir sayfayı SQL Server 2005 kullanırken SQL sorgusunun sonuçlarına bağımlı hale getirebilirsiniz. SQL Server 2005 kullanan sorgu bildirimi, kullanıcı denetimleri için @ OutputCache yönergesinde desteklenmez.
Note
Bu sınıfın tablo tabanlı bildirimleri kullanırken düzgün çalışması için, veritabanında ve bağımlılık yapmak istediğiniz tüm tablolarda bildirimlerin etkinleştirilmiş olması gerekir. Sınıfının yöntemlerini SqlCacheDependencyAdmin çağırarak veya komut satırı aracını kullanarak aspnet_regsql.exe bildirimleri etkinleştirebilirsiniz. Ayrıca, uygun yapılandırma ayarlarının uygulamanın Web.config dosyasına eklenmesi gerekir.
SQL Server 2005 sorgu bildirimiyle SqlCacheDependency nesnesi kullanmak için herhangi bir açık yapılandırma gerekmez. Sorgu bildirimi kullanılırken izin verilen Transact-SQL sorgu türleriyle ilgili kısıtlamalar hakkında bilgi için SQL Server belgelerine bakın.
Aşağıdaki örnekte, SQL Server veritabanı tablosunda tablo tabanlı bağımlılıkları etkinleştiren bir ASP.NET Web.config dosyası gösterilmektedir.
<configuration>
<connectionStrings>
<add name="Northwind" connectionString="Data Source=(local); Initial Catalog=northwind; Integrated Security=true"; providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled = "true" pollTime = "60000" >
<databases>
<add name="northwind"
connectionStringName="Northwind"
pollTime="9000000"
/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
Oluşturucular
| Name | Description |
|---|---|
| SqlCacheDependency(SqlCommand) |
Önbellek anahtarı bağımlılığı oluşturmak için sağlanan SqlCacheDependency öğesini kullanarak sınıfının yeni bir örneğini SqlCommand başlatır. |
| SqlCacheDependency(String, String) |
Önbellek anahtarı bağımlılığı oluşturmak için sağlanan parametreleri kullanarak sınıfının yeni bir örneğini SqlCacheDependency başlatır. |
Özellikler
| Name | Description |
|---|---|
| HasChanged |
Nesnenin CacheDependency değişip değişmediğini belirten bir değer alır. (Devralındığı yer: CacheDependency) |
| UtcLastModified |
Bağımlılığın en son değiştirildiği zamanı alır. (Devralındığı yer: CacheDependency) |
Yöntemler
| Name | Description |
|---|---|
| CreateOutputCacheDependency(String) |
ASP.NET uygulamasının OutputCache nesnesinde depolanan bir öğe ile SQL Server veritabanı tablosu arasında bağımlılık ilişkisi oluşturur. |
| DependencyDispose() |
sınıfı tarafından CacheDependency kullanılan kaynakları ve öğesinden CacheDependencytüretilen tüm sınıfları serbest bırakır. (Devralındığı yer: CacheDependency) |
| Dispose() |
nesnesi tarafından CacheDependency kullanılan kaynakları serbest bırakır. (Devralındığı yer: CacheDependency) |
| Equals(Object) |
Belirtilen nesnenin geçerli nesneye eşit olup olmadığını belirler. (Devralındığı yer: Object) |
| FinishInit() |
Nesnenin başlatılmasını CacheDependency tamamlar. (Devralındığı yer: CacheDependency) |
| GetFileDependencies() |
Dosya bağımlılıklarını alır. (Devralındığı yer: CacheDependency) |
| GetHashCode() |
Varsayılan karma işlevi işlevi görür. (Devralındığı yer: Object) |
| GetType() |
Geçerli örneğin Type alır. (Devralındığı yer: Object) |
| GetUniqueID() |
Nesne için benzersiz bir SqlCacheDependency tanımlayıcı alır. |
| ItemRemoved() |
İzlenen önbellek girdisi kaldırıldığında çağrılır. (Devralındığı yer: CacheDependency) |
| KeepDependenciesAlive() |
Bu öğeye bağlı olan her önbellek öğesinin son erişim zamanını güncelleştirir. (Devralındığı yer: CacheDependency) |
| MemberwiseClone() |
Geçerli Objectbasit bir kopyasını oluşturur. (Devralındığı yer: Object) |
| NotifyDependencyChanged(Object, EventArgs) |
Türetilmiş CacheDependency bir sınıf tarafından temsil edilen bağımlılığın değiştiğini temel CacheDependency nesneye bildirir. (Devralındığı yer: CacheDependency) |
| SetCacheDependencyChanged(Action<Object,EventArgs>) |
Bu bağımlılıktaki değişiklikleri ilgili tarafa bildirmeyi işlemek için bir Eylem yöntemi ekler. (Devralındığı yer: CacheDependency) |
| SetUtcLastModified(DateTime) |
Bağımlılığın en son değiştiği zamanı işaretler. (Devralındığı yer: CacheDependency) |
| TakeOwnership() |
İlk kullanıcının bu bağımlılığın özel sahipliğini bildirmesine izin verir. (Devralındığı yer: CacheDependency) |
| ToString() |
Geçerli nesneyi temsil eden bir dize döndürür. (Devralındığı yer: Object) |