if(myDataSet == null) { DataLocation.Text = "<P><B>The data came from a connection to the database.</B></P>"; SqlDataAdapter myAdapter = new SqlDataAdapter( "SELECT TOP 10 ProductID, ProductName, UnitPrice FROM Products;", "server=localhost;database=Northwind;uid=sa;pwd=sa;"); myDataSet = new DataSet();
Cache.Insert("CachedDataSet", myDataSet, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero); } else { DataLocation.Text = "<P><B>The data came from the cache. It was created at: " + myDataSet.ExtendedProperties["CreateTime"].ToString() + "</P><P>The current system time is: " + DateTime.Now.ToLongTimeString() + "</B></P>"; }
设置时间间隔
Cache.Insert("CachedDataSet", myDataSet, null,
DateTime.Now.AddMinutes(30), TimeSpan.Zero);
DataSet myDataSet = (DataSet)Cache.Get("CachedDataSet");
if(myDataSet == null)
{
DataLocation.Text = "<P><B>The data came from a connection to the database.</B></P>"; SqlDataAdapter myAdapter = new SqlDataAdapter(
"SELECT TOP 10 ProductID, ProductName, UnitPrice FROM Products;",
"server=localhost;database=Northwind;uid=sa;pwd=sa;");
myDataSet = new DataSet();
myAdapter.Fill(myDataSet, "Products");
myDataSet.ExtendedProperties.Add("CreateTime",
DateTime.Now.ToLongTimeString());
Cache.Insert("CachedDataSet", myDataSet, null,
DateTime.Now.AddMinutes(30), TimeSpan.Zero);
}
else
{
DataLocation.Text = "<P><B>The data came from the cache. It was created at: " +
myDataSet.ExtendedProperties["CreateTime"].ToString() +
"</P><P>The current system time is: " +
DateTime.Now.ToLongTimeString() +
"</B></P>";
}
myDataGrid.DataSource = myDataSet.Tables["Products"];
myDataGrid.DataBind();