SqlCacheDependency Costruttori

Definizione

Inizializza una nuova istanza della classe SqlCacheDependency.

Overload

Nome Descrizione
SqlCacheDependency(SqlCommand)

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

SqlCacheDependency(String, String)

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

SqlCacheDependency(SqlCommand)

Inizializza una nuova istanza della SqlCacheDependency classe utilizzando l'oggetto fornito SqlCommand per creare una dipendenza della chiave della 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

Oggetto SqlCommand utilizzato per creare un SqlCacheDependency oggetto .

Eccezioni

Il sqlCmd parametro è 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 oggetti SqlCacheDependency 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 di SqlCommand a un'istanza di SqlCacheDependency in una pagina utilizzando notifiche di query SQL Server 2005 con la memorizzazione nella cache dell'output a livello di pagina.

Vedi anche

Si applica a

SqlCacheDependency(String, String)

Inizializza una nuova istanza della SqlCacheDependency classe utilizzando i parametri forniti per creare una dipendenza della chiave della 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 di Web.config dell'applicazione.

tableName
String

Nome della tabella SqlCacheDependency di database a cui è associato .

Eccezioni

Controllo interno non SqlClientPermission riuscito.

oppure

Non databaseEntryName è stato trovato nell'elenco dei database configurati per le notifiche basate su tabelle.

oppure

Impossibile SqlCacheDependency connettersi al database durante l'inizializzazione.

oppure

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

Il tableName parametro è Empty.

Il polling non è abilitato per .SqlCacheDependency

oppure

L'intervallo di polling non è configurato correttamente.

oppure

Nessun stringa di connessione specificato nel file di configurazione dell'applicazione.

oppure

Impossibile trovare il stringa di connessione specificato nel file di configurazione dell'applicazione.

oppure

Il stringa di connessione specificato nel file di configurazione dell'applicazione è una stringa vuota.

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

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

databaseEntryName è null.

oppure

tableName è null.

Esempio

Nell'esempio di codice seguente viene usato questo costruttore per creare un'istanza della classe SqlCacheDependency 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 oggetti SqlCacheDependency per i prodotti SQL Server 7.0 e SQL Server 2000.

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