SqlCacheDependency Sınıf

Tanım

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 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
SqlCacheDependency

Örnekler

Aşağıdaki kod örneği, veritabanı tablosunu görüntülemek için ve GridView denetimlerini kullanırSqlDataSource. Sayfa yüklendiğinde, sayfa bir SqlCacheDependency nesne oluşturmaya çalışır. SqlCacheDependency Nesne oluşturulduktan sonra, sayfa nesnesine Cache bağımlılığı SqlCacheDependency olan bir öğ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 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 öğenin yeni bir sürümü eklenir Cache.

sınıfı, SqlCacheDependency SQL Server 2005 veritabanı kullanırken sınıfıyla tümleştirmeyi System.Data.SqlClient.SqlDependency de destekler. SQL Server 2005'in sorgu bildirim mekanizması, bir SQL sorgusunun sonuçlarını geçersiz kılmaya yönelik veri değişikliklerini algılar ve SQL sorgusuyla ilişkili önbelleğe alınmış öğeleri öğesinden System.Web.Caching.Cachekaldırır.

SQL Server 2005 kullanırken uygulamanızın Cache SQL Server veritabanı tablosuna veya SQL sorgusuna bağımlı öğeler eklemek için sınıfını kullanabilirsinizSqlCacheDependency. Bu sınıfı@ OutputCache, çıktı önbelleğe alınmış bir sayfayı veya kullanıcı denetimini SQL Server veritabanı tablosuna bağımlı hale getirmek için yönergesiyle birlikte de kullanabilirsiniz. Son olarak, SQL Server 2005 kullanırken çıktı önbelleğe alınmış bir sayfayı bir SQL sorgusunun sonuçlarına bağımlı hale getirmek için page yönergesiyle sınıfını @ OutputCache kullanabilirsinizSqlCacheDependency. SQL Server 2005 kullanan sorgu bildirimi, kullanıcı denetimleri yönergesinde @ OutputCache desteklenmez.

Not

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ı uygulamanın Web.config dosyasına eklenmelidir.

SQL Server 2005 sorgu bildirimine sahip bir SqlCacheDependency nesne 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, bir 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

SqlCacheDependency(SqlCommand)

Önbellek anahtarı bağımlılığı oluşturmak için sağlanan SqlCommand öğesini kullanarak sınıfının yeni bir örneğini SqlCacheDependency 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

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

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()

Type Geçerli örneğini 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 Objectöğesinin sığ 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>)

İlgili tarafa bu bağımlılıktaki değişiklikleri 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 dizeyi döndürür.

(Devralındığı yer: Object)

Şunlara uygulanır

Ayrıca bkz.