应用程序数据缓存 2
只能用在SQl server 2005及其以后的数据库
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.Caching;
6 using System.Web;
7 using System.Data;
8 using System.Data.SqlClient;
9 namespace CacheDAL
10 {
11 public class Account
12 {
13 private readonly static string dbConStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ToString();
14
15
16 public DataSet GetAccount()
17 {
18 if (HttpRuntime.Cache.Get("Account") == null)
19 {
20 SqlConnection conn = new SqlConnection(dbConStr);
21 SqlCommand cmd = new SqlCommand("select AccountID,[Money] from dbo.Account", conn);
22 SqlDataAdapter sda = new SqlDataAdapter(cmd);
23 DataSet ds = new DataSet();
24 sda.Fill(ds);
25 AggregateCacheDependency acd = new AggregateCacheDependency();
26 acd.Add(new SqlCacheDependency("test", "Account"));
27 HttpRuntime.Cache.Insert("Account", ds,acd );
28 }
29 return (DataSet)HttpRuntime.Cache.Get("Account");
30 }
31 }
32 }
33
在web工程中添加文件Global.asax
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.Caching;
6 using System.Web;
7 using System.Data;
8 using System.Data.SqlClient;
9 namespace CacheDAL
10 {
11 public class Account
12 {
13 private readonly static string dbConStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnectionString"].ToString();
14
15
16 public DataSet GetAccount()
17 {
18 if (HttpRuntime.Cache.Get("Account") == null)
19 {
20 SqlConnection conn = new SqlConnection(dbConStr);
21 SqlCommand cmd = new SqlCommand("select AccountID,[Money] from dbo.Account", conn);
22 SqlDataAdapter sda = new SqlDataAdapter(cmd);
23 DataSet ds = new DataSet();
24 sda.Fill(ds);
25 AggregateCacheDependency acd = new AggregateCacheDependency();
26 acd.Add(new SqlCacheDependency("test", "Account"));
27 HttpRuntime.Cache.Insert("Account", ds,acd );
28 }
29 return (DataSet)HttpRuntime.Cache.Get("Account");
30 }
31 }
32 }
33
Code
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.SessionState;
7 using System.Data.SqlClient;
8 using System.Configuration;
9 using System.Web.Caching;
10
11 namespace WebApplication1
12 {
13 public class Global : System.Web.HttpApplication
14 {
15
16 protected void Application_Start(object sender, EventArgs e)
17 {
18 string connStr = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
19 SqlCacheDependencyAdmin.EnableNotifications(connStr);
20 SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, "Account");
21 }
22
23 protected void Session_Start(object sender, EventArgs e)
24 {
25
26 }
27
28 protected void Application_BeginRequest(object sender, EventArgs e)
29 {
30
31 }
32
33 protected void Application_AuthenticateRequest(object sender, EventArgs e)
34 {
35
36 }
37
38 protected void Application_Error(object sender, EventArgs e)
39 {
40
41 }
42
43 protected void Session_End(object sender, EventArgs e)
44 {
45
46 }
47
48 protected void Application_End(object sender, EventArgs e)
49 {
50
51 }
52 }
53 }
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.SessionState;
7 using System.Data.SqlClient;
8 using System.Configuration;
9 using System.Web.Caching;
10
11 namespace WebApplication1
12 {
13 public class Global : System.Web.HttpApplication
14 {
15
16 protected void Application_Start(object sender, EventArgs e)
17 {
18 string connStr = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
19 SqlCacheDependencyAdmin.EnableNotifications(connStr);
20 SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, "Account");
21 }
22
23 protected void Session_Start(object sender, EventArgs e)
24 {
25
26 }
27
28 protected void Application_BeginRequest(object sender, EventArgs e)
29 {
30
31 }
32
33 protected void Application_AuthenticateRequest(object sender, EventArgs e)
34 {
35
36 }
37
38 protected void Application_Error(object sender, EventArgs e)
39 {
40
41 }
42
43 protected void Session_End(object sender, EventArgs e)
44 {
45
46 }
47
48 protected void Application_End(object sender, EventArgs e)
49 {
50
51 }
52 }
53 }
Code
<connectionStrings>
<add name="dbConnectionString" connectionString="Data Source=FDM;Initial Catalog=test;UID=sa;PWD=sa;"/>
</connectionStrings>
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="test" connectionStringName="dbConnectionString" pollTime="10000"/>
</databases>
</sqlCacheDependency>
</caching>
<connectionStrings>
<add name="dbConnectionString" connectionString="Data Source=FDM;Initial Catalog=test;UID=sa;PWD=sa;"/>
</connectionStrings>
<caching>
<sqlCacheDependency enabled="true" pollTime="10000">
<databases>
<add name="test" connectionStringName="dbConnectionString" pollTime="10000"/>
</databases>
</sqlCacheDependency>
</caching>