Monday, November 19, 2007

ASP.NET 2.0 Caching (4) Data Caching

<%@ Page Language="vb" %>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>


<script runat="server">

Sub Page_Load(ByVal Src As Object, ByVal E As EventArgs)

Dim Source As DataView

' try to retrieve item from cache

' if it's not there, add it

Source = Cache("MyDataSet")

If Source Is Nothing Then

Dim MyConnection As SqlConnection

Dim MyCommand As SqlDataAdapter

MyConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("pubsConnectionString").ConnectionString)

MyCommand = New SqlDataAdapter("select * from Authors", MyConnection)

Dim ds As New DataSet

MyCommand.Fill(ds, "Authors")

Source = New DataView(ds.Tables("Authors"))

Cache("MyDataSet") = Source

CacheMsg.Text = "Dataset created explicitly"


CacheMsg.Text = "Dataset retrieved from cache"

End If

MyGrid.DataSource = Source


End Sub



<form id="Form1" runat="server">


<font face="Verdana">Caching Data</font></h3>

<asp:GridView ID="MyGrid" runat="server">




<asp:Label ID="CacheMsg" runat="server" /></i>





You may find caching data this way is a bit faster than the output caching metioned in the previous posts.

blog comments powered by Disqus