SqlCacheDependency 建構函式

定義

初始化 SqlCacheDependency 類別的新執行個體。

多載

SqlCacheDependency(SqlCommand)

使用所提供的 SqlCacheDependency 建立快取索引鍵相依性,初始化 SqlCommand 類別的新執行個體。

SqlCacheDependency(String, String)

使用所提供的參數建立快取索引鍵相依性,初始化 SqlCacheDependency 類別的新執行個體。

SqlCacheDependency(SqlCommand)

使用所提供的 SqlCacheDependency 建立快取索引鍵相依性,初始化 SqlCommand 類別的新執行個體。

public:
 SqlCacheDependency(System::Data::SqlClient::SqlCommand ^ sqlCmd);
public SqlCacheDependency (System.Data.SqlClient.SqlCommand sqlCmd);
new System.Web.Caching.SqlCacheDependency : System.Data.SqlClient.SqlCommand -> System.Web.Caching.SqlCacheDependency
Public Sub New (sqlCmd As SqlCommand)

參數

sqlCmd
SqlCommand

SqlCommand,用來建立 SqlCacheDependency 物件。

例外狀況

sqlCmd 參數為 null

實例 SqlCommandNotificationAutoEnlist 屬性設定為 true ,且頁面上有 屬性 @ OutputCache 設定為 CommandNotificationSqlDependency 指示詞。

備註

此建構函式是用來建立 SqlCacheDependency 使用 2005 SQL Server 2005 產品的查詢通知功能的物件。

與 參數相關聯的 sqlCmd SQL 語句必須包含下列專案:

  • 完整資料表名稱,包括資料表擁有者的名稱。 例如,若要參考資料庫擁有者所擁有的 Customer 資料表,SQL 語句必須參考 dbo.customers

  • Select 語句中的明確資料行名稱。 您無法使用星號 (*) 萬用字元,從資料表中選取所有資料行。 例如,您必須使用 select name, address, city, state from dbo.customers ,而不是 select * from dbo.customers

此建構函式無法用來使用頁面層級輸出快取SQL Server 2005 查詢通知,將實例與 SqlCacheDependency 頁面上的 實例產生關聯 SqlCommand

另請參閱

適用於

SqlCacheDependency(String, String)

使用所提供的參數建立快取索引鍵相依性,初始化 SqlCacheDependency 類別的新執行個體。

public:
 SqlCacheDependency(System::String ^ databaseEntryName, System::String ^ tableName);
public SqlCacheDependency (string databaseEntryName, string tableName);
new System.Web.Caching.SqlCacheDependency : string * string -> System.Web.Caching.SqlCacheDependency
Public Sub New (databaseEntryName As String, tableName As String)

參數

databaseEntryName
String

資料庫名稱,在應用程式 Web.config 檔案中的 databases 項目內所定義。

tableName
String

資料庫資料表的名稱,SqlCacheDependency 與該資料表相關聯。

例外狀況

SqlClientPermission 的內部檢查失敗。

-或-

在設定了以資料表為基礎的告知之資料庫清單中,找不到 databaseEntryName

-或-

SqlCacheDependency 物件無法在初始化期間連接到資料庫。

-或-

SqlCacheDependency 物件在資料庫上或支援 SqlCacheDependency 物件的資料庫預存程序 (Stored Procedure) 上,遇到使用權限遭拒錯誤。

tableName 參數為 Empty

SqlCacheDependency 的輪詢未啟用。

-或-

輪詢間隔沒有正確設定。

-或-

沒有在應用程式組態檔中指定連接字串 (Connection String)。

-或-

找不到應用程式組態檔中所指定的連接字串。

-或-

應用程式組態檔中所指定的連接字串為空字串。

databaseEntryName 參數中所指定的資料庫未啟用變更告知。

tableName 參數中所指定的資料庫資料表未啟用變更告知。

databaseEntryNamenull

-或-

tableNamenull

範例

下列程式碼範例會使用此建構函式來建立類別的 SqlCacheDependency 實例,該實例與名為 Northwind 之SQL Server資料庫中名為 Categories 的資料庫資料表相關聯。

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."; 
    } 
} 
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

備註

此建構函式可用來建立 SqlCacheDependency SQL Server 7.0 和 SQL Server 2000 產品的物件。

傳遞至 database 參數的資料庫名稱必須在應用程式的 Web.config 檔案中定義。 例如,下列Web.config檔案會針對變更通知定義名為 pubs SqlCacheDependency 的資料庫。

<configuration>
  <connectionStrings>
    <add name="Pubs" connectionString="Data Source=(local); Initial Catalog=pubs; Integrated Security=true"; providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <caching>
      <sqlCacheDependency enabled = "true" pollTime = "60000" >
        <databases>
          <add name="pubs"
            connectionStringName="pubs"
            pollTime="9000000"
            />
        </databases>
      </sqlCacheDependency>
    </caching>
  </system.web>
</configuration>

使用這個建構函式時通常會擲回兩個例外狀況: DatabaseNotEnabledForNotificationExceptionTableNotEnabledForNotificationExceptionDatabaseNotEnabledForNotificationException如果擲回 ,您可以在例外狀況處理常式代碼中呼叫 SqlCacheDependencyAdmin.EnableNotifications 方法,或使用 aspnet_regsql.exe 命令列工具來設定通知的資料庫。 TableNotEnabledForNotificationException如果擲回 ,您可以呼叫 SqlCacheDependencyAdmin.EnableTableForNotifications 方法,或使用 aspnet_regsql.exe 來設定通知的資料表。

另請參閱

適用於