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

用于创建 SqlCacheDependency 对象的 SqlCommand

例外

sqlCmd 参数为 null

实例SqlCommand的 属性设置为 true ,页面上有一个@ OutputCache指令,SqlDependency其属性设置为 CommandNotificationNotificationAutoEnlist

注解

此构造函数用于创建SqlCacheDependency使用 SQL Server 2005 产品的查询通知功能的对象。

sqlCmd 参数关联的 SQL 语句必须包括以下内容:

  • 完全限定的表名称,包括表所有者的名称。 例如,若要引用数据库所有者拥有的名为 Customers 的表,SQL 语句必须引用 dbo.customers

  • Select 语句中的显式列名。 不能使用星号 (*) 通配符来选择表中的所有列。 例如, select * from dbo.customers必须使用 select name, address, city, state 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 文件的数据库元素中定义的数据库的名称。

tableName
String

SqlCacheDependency 关联的数据库表的名称。

例外

SqlClientPermission 的内部检查失败。

- 或 -

在为基于表的通知配置的数据库的列表中未找到 databaseEntryName

- 或 -

SqlCacheDependency 对象在初始化过程中未能连接到数据库。

- 或 -

SqlCacheDependency 对象在数据库或支持 SqlCacheDependency 对象的数据库存储过程上遇到权限拒绝错误。

tableName 参数为 Empty

未对 SqlCacheDependency 启用轮询。

- 或 -

轮询间隔配置不正确。

- 或 -

在应用程序的配置文件中未指定连接字符串。

- 或 -

未能找到在应用程序的配置文件中指定的连接字符串。

- 或 -

在应用程序的配置文件中指定的连接字符串为空字符串。

没有为 databaseEntryName 参数中指定的数据库启用更改通知。

没有为 tableName 参数中指定的数据库表启用更改通知。

databaseEntryNamenull

tableName 上声明的默认值为 null

示例

下面的代码示例使用此构造函数创建 类的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

注解

此构造函数用于为 SQL Server 7.0 和 SQL Server 2000 产品创建SqlCacheDependency对象。

传递给 参数的数据库名称 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>

使用此构造函数时,通常会引发两个异常: DatabaseNotEnabledForNotificationExceptionTableNotEnabledForNotificationException。 如果引发 , DatabaseNotEnabledForNotificationException 则可以在异常处理代码中调用 SqlCacheDependencyAdmin.EnableNotifications 方法,或使用 aspnet_regsql.exe 命令行工具为通知设置数据库。 如果引发 , TableNotEnabledForNotificationException 则可以调用 SqlCacheDependencyAdmin.EnableTableForNotifications 方法或使用 aspnet_regsql.exe 设置通知表。

另请参阅

适用于