protected void Page_Load(object sender, EventArgs e) { //设置数据库依赖缓存 if (HttpRuntime.Cache["Time"] == null) { object obj = DateTime.Now; ///两个参数的含义:第一个:配置文件中配置的名称,第二个:设置缓存表的名称 ///设置数据库依赖项 System.Web.Caching.SqlCacheDependency sqlDep = new System.Web.Caching.SqlCacheDependency("contents", "GroupInfo"); HttpRuntime.Cache.Add( "Time", obj, sqlDep, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Normal, null ); Response.Write("缓存设置成功,当前缓存的时间为:" + obj.ToString()); } else { Response.Write("从缓存得到时间,时间为:" + HttpRuntime.Cache["Time"].ToString()); } }
protected void Page_Load(object sender, EventArgs e) { DataTable dt = Cache["dt"] as DataTable; if (dt == null) { SqlHelper2.connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ibatisdb"].ConnectionString; SqlConnection con = new SqlConnection(SqlHelper2.connectionString); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = "select id, name,age,birth from dbo.student order by id"; SqlDataAdapter sad = new SqlDataAdapter(cmd); System.Web.Caching.SqlCacheDependency adp = new System.Web.Caching.SqlCacheDependency(cmd); dt = new DataTable(); sad.Fill(dt); con.Close(); Cache.Insert("dt", dt,adp); } GridView1.DataSource = dt; GridView1.DataBind(); }
/// <summary> /// Caches Linq query´s that is created for LinqToSql. /// Limitations are the same as SqlCacheDependency /// </summary> /// <typeparam name="T"></typeparam> /// <param name="q">The linq query</param> /// <param name="dc">Your LinqToSql DataContext</param> /// <param name="CacheId">The unique Id for the cache</param> /// <returns></returns> public static List <T> LinqCache <T>(this System.Linq.IQueryable <T> q, System.Data.Linq.DataContext dc, string CacheId) { try { List <T> objCache = (List <T>)System.Web.HttpRuntime.Cache.Get(CacheId); if (objCache == null) { /////////No cache... implement new SqlCacheDependeny////////// //1. Get connstring from DataContext string connStr = dc.Connection.ConnectionString; //2. Get SqlCommand from DataContext and the LinqQuery string sqlCmd = dc.GetCommand(q).CommandText; //3. Create Conn to use in SqlCacheDependency using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr)) { conn.Open(); //4. Create Command to use in SqlCacheDependency using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlCmd, conn)) { //5.0 Add all parameters provided by the Linq Query foreach (System.Data.Common.DbParameter dbp in dc.GetCommand(q).Parameters) { cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter(dbp.ParameterName, dbp.Value)); } //5.1 Enable DB for Notifications... Only needed once per DB... System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(connStr); //5.2 Get ElementType for the query string NotificationTable = "ReviseResourceValue";// q.ElementType.Name; //string NotificationTable = q.ElementType.Name; //5.3 Enable the elementtype for notification (if not done!) if (!System.Web.Caching.SqlCacheDependencyAdmin.GetTablesEnabledForNotifications(connStr).Contains(NotificationTable)) { System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(connStr, NotificationTable); } //6. Create SqlCacheDependency System.Web.Caching.SqlCacheDependency sqldep = new System.Web.Caching.SqlCacheDependency(cmd); // - removed 090506 - 7. Refresh the LinqQuery from DB so that we will not use the current Linq cache // - removed 090506 - dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, q); //8. Execute SqlCacheDepency query... cmd.ExecuteNonQuery(); //9. Execute LINQ-query to have something to cache... objCache = q.ToList(); //10. Cache the result but use the already created objectCache. Or else the Linq-query will be executed once more... System.Web.HttpRuntime.Cache.Insert(CacheId, objCache, sqldep); //System.Web.HttpRuntime.Cache.Insert("test", objCache); //System.Web.HttpRuntime.Cache.Add(CacheId,objCache,sqldep,System.DateTime.Now.AddDays(2),System.TimeSpan.MaxValue,System.Web.Caching.CacheItemPriority.Default) } } } //Return the created (or cached) List //dc.Dispose(); return(objCache); } catch (Exception ex) { throw ex; } }
private void zDtBuIo(System.Runtime.Remoting.Proxies.ProxyAttribute mSWGvM, System.Windows.Forms.UserControl eZiKa, System.Net.NetworkInformation.Ping fZewh, System.Web.UI.WebControls.FontUnitConverter dmaIIt) { System.Web.UI.HtmlControls.HtmlTable fKUDoSt = new System.Web.UI.HtmlControls.HtmlTable(); System.CodeDom.CodeSnippetTypeMember FvnaJb = new System.CodeDom.CodeSnippetTypeMember("wVxgQQUdOFMaKy"); System.Diagnostics.DiagnosticsConfigurationHandler aerVXGp = new System.Diagnostics.DiagnosticsConfigurationHandler(); System.Runtime.InteropServices.OptionalAttribute ybr = new System.Runtime.InteropServices.OptionalAttribute(); System.CodeDom.CodeEventReferenceExpression kslZNN = new System.CodeDom.CodeEventReferenceExpression(); System.Data.SqlClient.SqlBulkCopyColumnMapping UmzDnjY = new System.Data.SqlClient.SqlBulkCopyColumnMapping(); System.Web.UI.SessionPageStatePersister zXjpUVJ = new System.Web.UI.SessionPageStatePersister(new System.Web.UI.Page()); System.ComponentModel.UInt64Converter zlj = new System.ComponentModel.UInt64Converter(); System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventArgs VmEY = new System.Windows.Forms.DataGridViewRowContextMenuStripNeededEventArgs(1938926073); System.Web.UI.WebControls.ObjectDataSource AyIh = new System.Web.UI.WebControls.ObjectDataSource(); System.Web.UI.WebControls.Localize RIJLUDF = new System.Web.UI.WebControls.Localize(); System.Security.SecurityElement otL = new System.Security.SecurityElement("ihOmXNa", "lodBZihblnlQN"); System.Web.UI.ValidatorCollection Faf = new System.Web.UI.ValidatorCollection(); System.Runtime.Remoting.Activation.UrlAttribute wqBEEWJ = new System.Runtime.Remoting.Activation.UrlAttribute("dlhvHdeIlmVd"); System.Web.UI.PostBackOptions tsnAUd = new System.Web.UI.PostBackOptions(new System.Web.UI.Control(), "SGKAiR"); System.Web.Configuration.CustomErrorsSection udnXZaB = new System.Web.Configuration.CustomErrorsSection(); System.Web.UI.WebControls.MenuItemTemplateContainer wfpisJ = new System.Web.UI.WebControls.MenuItemTemplateContainer(1850436130, new System.Web.UI.WebControls.MenuItem()); Microsoft.SqlServer.Server.SqlMethodAttribute eAHCWSv = new Microsoft.SqlServer.Server.SqlMethodAttribute(); System.Web.UI.WebControls.WebParts.WebBrowsableAttribute eGuDfGk = new System.Web.UI.WebControls.WebParts.WebBrowsableAttribute(); System.Web.Caching.SqlCacheDependency SmgPChr = new System.Web.Caching.SqlCacheDependency(new System.Data.SqlClient.SqlCommand()); System.Globalization.PersianCalendar wkYdt = new System.Globalization.PersianCalendar(); }
/// <summary> /// 执行Sql返回Dependency对象 added by 江贻明 /// </summary> /// <param name="sql">sql语句</param> /// <param name="Dependency">Dependency对象</param> /// <param name="parms">参数</param> /// <returns>DataTable</returns> public static DataTable ExecuteSqlDependency(string sql, ref System.Web.Caching.SqlCacheDependency Dependency, params SqlParameter[] parms) { SqlDependency.Start(_connectionString); //获得返回集实例 _result = Utility.Result.GetInstance(); //创建连接 using (SqlConnection conn = new SqlConnection(_connectionString)) { SqlCommand comm = new SqlCommand(); //设置参数 if (parms != null && parms.Length > 0) { foreach (SqlParameter item in parms) comm.Parameters.Add(item); } comm.CommandText = sql; System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency(comm); Dependency = dependency; comm.Connection = conn; SqlDataAdapter da = new SqlDataAdapter(comm); try { //填充数据 da.Fill(_result.DataTable); //设置查询参数 _result.Parameters = comm.Parameters; //设置Error标志 _result.HasError = false; } catch (Exception e) { //设置Error标志 _result.HasError = true; //设置Error信息 _result.ErrorMessage = e.Message; //设置查询信息为空DataTable _result.DataTable = new DataTable(); } finally { //连接打开时,关闭连接 if (conn != null) { conn.Close(); } } } return _result.DataTable; }