<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Categories]"
EnableCaching="True"
CacheDuration="60"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource> |
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
DataKeyNames="CategoryID"
DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT * FROM [Categories]"
EnableCaching="True"
CacheDuration="60"
CacheExpirationPolicy="Sliding"
CacheKeyDependency="Category"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource> |
void Application_Start(object sender, EventArgs e)
{
HttpContext.Current.Cache.Insert("Category",DateTime.Now,
null,DateTime.MaxValue,
Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable,null);
} |
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<!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>添加类别信息</title>
</head>
<body>
<form id="form1" runat="server">
类别名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
类别描述:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
</body>
</html> |
protected void Button1_Click(object sender, EventArgs e)
{
string strConn =
WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
string strSql = "insert into Categories(CategoryName,Description) values(@categoryName,@description)";
SqlConnection cn = new SqlConnection(strConn);
SqlCommand cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = strSql;
cm.Parameters.AddWithValue("categoryName",TextBox1.Text);
cm.Parameters.AddWithValue("description",TextBox2.Text);
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
Cache.Insert("Category", DateTime.Now);
Response.Redirect("Default2.aspx");
} |
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = (DataTable)Cache["categories"];
if (dt == null)
{
dt = GetCategories();
Cache.Insert("categories", dt);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
private DataTable GetCategories()
{
string strConn =
WebConfigurationManager.
ConnectionStrings["NorthwindConnectionString"].
ConnectionString;
string strSql = "select categoryID,categoryName from categories";
SqlConnection cn = new SqlConnection(strConn);
SqlDataAdapter da =
new SqlDataAdapter(strSql,cn);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
} |
Cache.Insert("categories", dt,
null,
DateTime.Now.AddHours(1),
Cache.NoSlidingExpiration); |
Cache.Insert("categories",
dt,
null,
Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(10)); |
CREATE TRIGGER t
ON Categories
AFTER INSERT,UPDATE,DELETE
AS
DECLARE @v1 varchar(200)
DECLARE @v2 varchar(100)
SET @v1 = cast(datepart(year,getdate()) as varchar) +
cast(datepart(month,getdate()) as varchar) +
cast(datepart(day,getdate()) as varchar) +
cast(datepart(hour,getdate()) as varchar) +
cast(datepart(minute,getdate()) as varchar) +
cast(datepart(second,getdate()) as varchar) +
cast(datepart(ms,getdate()) as varchar)
SET @v2 = '@echo ' + @v1 + '>e:"prog"asp.net"Chapter16"Category.txt'
EXECUTE master..xp_cmdshell @v2 |
DataTable dt = (DataTable)Cache["category"];
if (dt == null)
{
string fileName = MapPath("~/Category.txt");
CacheDependency dep =
new CacheDependency(fileName);
dt = GetCategories();
Cache.Insert("category", dt,dep);
}
GridView1.DataSource = dt;
GridView1.DataBind(); |