SqlDependency (VB) : SQL Cache Dependencies « Cache « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" Height="400px" Width="400px">
        </asp:GridView>
    
    </div>
    </form>
</body>
</html>

File: Default.aspx.vb

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Write("Page created: " + DateTime.Now.ToLongTimeString())
        Dim connStr As String = ConfigurationManager.ConnectionStrings("AppConnectionString1").ConnectionString
        SqlDependency.Start(connStr)
        Dim connection As New SqlConnection(connStr)
        Dim command As New SqlCommand("Select * FROM Customers", connection)
        Dim depends As New SqlCacheDependency(command)

        connection.Open()
        GridView1.DataSource = command.ExecuteReader()
        GridView1.DataBind()

        connection.Close()

        Response.AddCacheDependency(depends)
    End Sub
End Class



File: Web.config
<?xml version="1.0"?>

<configuration>
  <appSettings/>
  <connectionStrings>
    <add name="AppConnectionString1" connectionString="Data Source=.\SQLEXPRESS;User ID=wrox;Password=wrox1*c;Database=Northwind;Persist Security Info=False" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <caching>
      <sqlCacheDependency enabled="true">
        <databases>
          <add name="Northwind" connectionStringName="AppConnectionString1" pollTime="500"/>
        </databases>
      </sqlCacheDependency>
    </caching>

    <compilation debug="true" strict="false" explicit="true"/>
    <pages>
      <namespaces>
        <clear/>
        <add namespace="System"/>
        <add namespace="System.Collections"/>
        <add namespace="System.Collections.Specialized"/>
        <add namespace="System.Configuration"/>
        <add namespace="System.Text"/>
        <add namespace="System.Text.RegularExpressions"/>
        <add namespace="System.Web"/>
        <add namespace="System.Web.Caching"/>
        <add namespace="System.Web.SessionState"/>
        <add namespace="System.Web.Security"/>
        <add namespace="System.Web.Profile"/>
        <add namespace="System.Web.UI"/>
        <add namespace="System.Web.UI.WebControls"/>
        <add namespace="System.Web.UI.WebControls.WebParts"/>
        <add namespace="System.Web.UI.HtmlControls"/>
      </namespaces>
    </pages>

    <authentication mode="Windows"/>

  </system.web>
</configuration>








13.12.SQL Cache Dependencies
13.12.1.SqlDependency (C#)
13.12.2.SqlDependency (VB)
13.12.3.Using SQL Cache Dependencies
13.12.4.Using Polling SQL Cache Dependencies with Page Output Caching
13.12.5.Using Polling SQL Cache Dependencies with DataSource Caching
13.12.6.Using Polling SQL Cache Dependencies with Data Caching
13.12.7.SqlDependency="Northwind:Customers"
13.12.8.Using the Cache object with the SqlDependency object (C#)
13.12.9.Using the Cache object with the SqlDependency object (VB)