Share via


SqlCacheDependency Costruttori

Definizione

Inizializza una nuova istanza della classe SqlCacheDependency.

Overload

SqlCacheDependency(SqlCommand)

Inizializza una nuova istanza della classe SqlCacheDependency utilizzando l'oggetto SqlCommand fornito per creare una dipendenza di chiave di cache.

SqlCacheDependency(String, String)

Inizializza una nuova istanza della classe SqlCacheDependency utilizzando i parametri forniti per creare una dipendenza di chiave di cache.

SqlCacheDependency(SqlCommand)

Inizializza una nuova istanza della classe SqlCacheDependency utilizzando l'oggetto SqlCommand fornito per creare una dipendenza di chiave di cache.

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)

Parametri

sqlCmd
SqlCommand

Classe SqlCommand utilizzata per creare un oggetto SqlCacheDependency.

Eccezioni

Il valore del parametro sqlCmd è null.

L'istanza SqlCommand ha la proprietà NotificationAutoEnlist impostata su true e nella pagina è presente una @ OutputCache direttiva con l'attributo SqlDependency impostato su CommandNotification.

Commenti

Questo costruttore viene usato per creare SqlCacheDependency oggetti che usano la funzionalità di notifica delle query dei prodotti SQL Server 2005.

Le istruzioni SQL associate al sqlCmd parametro devono includere quanto segue:

  • Nomi di tabella completi, incluso il nome del proprietario della tabella. Ad esempio, per fare riferimento a una tabella denominata Customers di proprietà del proprietario del database, l'istruzione SQL deve fare riferimento a dbo.customers.

  • Nomi di colonna espliciti nell'istruzione Select. Non è possibile utilizzare il carattere jolly asterisco (*) per selezionare tutte le colonne di una tabella. Ad esempio, anziché select * from dbo.customers, è necessario usare select name, address, city, state from dbo.customers.

Questo costruttore non può essere usato per associare un'istanza SqlCommand a un'istanza SqlCacheDependency di in una pagina utilizzando SQL Server 2005 notifiche di query con la memorizzazione nella cache dell'output a livello di pagina.

Vedi anche

Si applica a

SqlCacheDependency(String, String)

Inizializza una nuova istanza della classe SqlCacheDependency utilizzando i parametri forniti per creare una dipendenza di chiave di cache.

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)

Parametri

databaseEntryName
String

Nome di un database definito nell'elemento databases del file Web.config dell'applicazione.

tableName
String

Nome della tabella di database a cui è associata la classe SqlCacheDependency.

Eccezioni

La verifica interna dell'oggetto SqlClientPermission non è riuscita.

-oppure-

Il parametro databaseEntryName non è stato trovato nell'elenco di database configurato per le notifiche basate su tabella.

-oppure-

Non è stato possibile connettere l'oggetto SqlCacheDependency al database durante l'inizializzazione.

-oppure-

L'oggetto SqlCacheDependency ha rilevato un errore di autorizzazione negata nel database o nelle stored procedure di database che supportano l'oggetto SqlCacheDependency.

Il valore del parametro tableName è Empty.

Il polling non è attivato per la classe SqlCacheDependency.

-oppure-

L'intervallo di polling non è stato configurato correttamente.

-oppure-

Non è stata specificata alcuna stringa di connessione nel file di configurazione dell'applicazione.

-oppure-

Non è stato possibile trovare la stringa di connessione specificata nel file di configurazione dell'applicazione.

-oppure-

La stringa di connessione specificata nel file di configurazione dell'applicazione è una stringa vuota.

Il database specificato nel parametro databaseEntryName non è attivato per le notifiche di modifica.

La tabella di database specificata nel parametro tableName non è attivata per le notifiche di modifica.

databaseEntryName è null.

-oppure-

tableName è null.

Esempio

Nell'esempio di codice seguente viene utilizzato questo costruttore per creare un'istanza della SqlCacheDependency classe associata a una tabella di database denominata Categories in un database SQL Server denominato Northwind.

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

Commenti

Questo costruttore viene usato per creare SqlCacheDependency oggetti per SQL Server 7.0 e SQL Server 2000 prodotti.

Il nome del database passato al database parametro deve essere definito nel file di Web.config dell'applicazione. Ad esempio, il file di Web.config seguente definisce un database denominato pubs per SqlCacheDependency le notifiche di modifica.

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

Quando viene usato questo costruttore, vengono comunemente generate due eccezioni: DatabaseNotEnabledForNotificationException e TableNotEnabledForNotificationException. Se viene generata un'eccezione DatabaseNotEnabledForNotificationException , è possibile chiamare il SqlCacheDependencyAdmin.EnableNotifications metodo nel codice di gestione delle eccezioni oppure usare lo aspnet_regsql.exe strumento da riga di comando per configurare il database per le notifiche. Se viene generata un'eccezione TableNotEnabledForNotificationException , è possibile chiamare il SqlCacheDependencyAdmin.EnableTableForNotifications metodo o usare aspnet_regsql.exe per configurare la tabella per le notifiche.

Vedi anche

Si applica a