SqlCacheDependency Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy SqlCacheDependency.

Przeciążenia

Nazwa Opis
SqlCacheDependency(SqlCommand)

Inicjuje nowe wystąpienie SqlCacheDependency klasy przy użyciu podanej SqlCommand metody w celu utworzenia zależności klucza pamięci podręcznej.

SqlCacheDependency(String, String)

Inicjuje nowe wystąpienie SqlCacheDependency klasy przy użyciu podanych parametrów w celu utworzenia zależności klucza pamięci podręcznej.

SqlCacheDependency(SqlCommand)

Inicjuje nowe wystąpienie SqlCacheDependency klasy przy użyciu podanej SqlCommand metody w celu utworzenia zależności klucza pamięci podręcznej.

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)

Parametry

sqlCmd
SqlCommand

Obiekt SqlCommand używany do tworzenia SqlCacheDependency obiektu.

Wyjątki

Parametr sqlCmd jest null.

Wystąpienie SqlCommand ma ustawioną NotificationAutoEnlisttrue właściwość na @ OutputCache wartość i na stronie znajduje się dyrektywa z atrybutem ustawionym SqlDependency na CommandNotificationwartość .

Uwagi

Ten konstruktor służy do tworzenia obiektów SqlCacheDependency korzystających z funkcji powiadamiania o zapytaniach produktów SQL Server 2005.

Instrukcje SQL skojarzone z parametrem sqlCmd muszą zawierać następujące elementy:

  • W pełni kwalifikowane nazwy tabel, w tym nazwa właściciela tabeli. Aby na przykład odwołać się do tabeli o nazwie Customers, która jest własnością właściciela bazy danych, instrukcja SQL musi odwoływać się do dbo.customers.

  • Jawne nazwy kolumn w instrukcji Select. Nie można użyć gwiazdki (*) symbolu wieloznakowego, aby wybrać wszystkie kolumny z tabeli. Na przykład zamiast select * from dbo.customerspolecenia należy użyć polecenia select name, address, city, state from dbo.customers.

Tego konstruktora nie można użyć do skojarzenia wystąpienia SqlCommand z wystąpieniem SqlCacheDependency na stronie przy użyciu powiadomień zapytań SQL Server 2005 z buforowaniem danych wyjściowych na poziomie strony.

Zobacz też

Dotyczy

SqlCacheDependency(String, String)

Inicjuje nowe wystąpienie SqlCacheDependency klasy przy użyciu podanych parametrów w celu utworzenia zależności klucza pamięci podręcznej.

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)

Parametry

databaseEntryName
String

Nazwa bazy danych zdefiniowanej w elemercie baz danych pliku Web.config aplikacji.

tableName
String

Nazwa tabeli bazy danych, z którą SqlCacheDependency jest skojarzona.

Wyjątki

Wewnętrzne sprawdzanie nie SqlClientPermission powiodło się.

— lub —

Nie databaseEntryName można odnaleźć elementu na liście baz danych skonfigurowanych dla powiadomień opartych na tabelach.

— lub —

Obiekt SqlCacheDependency nie może nawiązać połączenia z bazą danych podczas inicjowania.

— lub —

Obiekt SqlCacheDependency napotkał błąd odmowy uprawnień w bazie danych lub w procedurach składowanych bazy danych, które obsługują SqlCacheDependency obiekt.

Parametr tableName jest Empty.

Sondowanie nie jest włączone dla elementu SqlCacheDependency.

— lub —

Interwał sondowania nie jest poprawnie skonfigurowany.

— lub —

W pliku konfiguracji aplikacji nie określono parametry połączenia.

— lub —

Nie można odnaleźć parametry połączenia określonego w pliku konfiguracji aplikacji.

— lub —

Connection string określony w pliku konfiguracji aplikacji jest pustym ciągiem.

Baza danych określona w parametrze databaseEntryName nie jest włączona dla powiadomień o zmianie.

Tabela bazy danych określona w parametrze tableName nie jest włączona dla powiadomień o zmianie.

Parametr databaseEntryName ma wartość null.

— lub —

Parametr tableName ma wartość null.

Przykłady

Poniższy przykład kodu używa tego konstruktora do utworzenia wystąpienia klasy SqlCacheDependency skojarzonej z tabelą bazy danych o nazwie Categories w bazie danych SQL Server o nazwie 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

Uwagi

Ten konstruktor służy do tworzenia obiektów SqlCacheDependency dla SQL Server 7.0 i SQL Server 2000 produktów.

Nazwa bazy danych przekazana do parametru database musi być zdefiniowana w pliku Web.config aplikacji. Na przykład poniższy plik Web.config definiuje bazę danych o nazwie pubs na potrzeby SqlCacheDependency powiadomień o zmianie.

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

Dwa wyjątki są często zgłaszane, gdy ten konstruktor jest używany: DatabaseNotEnabledForNotificationException i TableNotEnabledForNotificationException. Jeśli element DatabaseNotEnabledForNotificationException jest zgłaszany, możesz wywołać metodę SqlCacheDependencyAdmin.EnableNotifications w kodzie obsługi wyjątków lub użyć aspnet_regsql.exe narzędzia wiersza polecenia, aby skonfigurować bazę danych na potrzeby powiadomień. Jeśli element TableNotEnabledForNotificationException jest zgłaszany, możesz wywołać metodę SqlCacheDependencyAdmin.EnableTableForNotifications lub użyć aspnet_regsql.exe metody w celu skonfigurowania tabeli dla powiadomień.

Zobacz też

Dotyczy