Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| ///<summary> /// This class tracks a SQL Server dependency. ///</summary> [Serializable] [ComVisible(false)] public class SqlServerDependency :ICacheItemExpiration { private bool isExpired; ///<summary> /// Get expiration state ///</summary> public bool IsExpired { get { return isExpired; } } ///<summary> /// Constructor with one argument. ///</summary> ///<param name="command"></param> public SqlServerDependency(SqlCommand command) { SqlDependency.Stop(command.Connection.ConnectionString); SqlDependency.Start(command.Connection.ConnectionString); command.Notification = null; SqlDependency sqlDependency = new SqlDependency(command); sqlDependency.OnChange += new OnChangeEventHandler(OnChanged); if (command.Connection.State == ConnectionState.Closed) command.Connection.Open(); command.ExecuteReader(CommandBehavior.CloseConnection); } void OnChanged(object sender, SqlNotificationEventArgs e) { SqlDependency sqlDependency = sender as SqlDependency; sqlDependency.OnChange -= OnChanged; isExpired = true; } ///<summary> /// Specifies if the item has expired or not. ///</summary> ///<returns>Returns true if the item has expired, otherwise false.</returns> public bool HasExpired() { return isExpired; } ///<summary> /// Notifies that the item was recently used. ///</summary> public void Notify() { } ///<summary> /// Not used ///</summary> ///<param name="owningCacheItem">Not used</param> public void Initialize(CacheItem owningCacheItem) { } } |
| CacheManager cacheManager = CacheFactory.GetCacheManager(); private void button1_Click(object sender, EventArgs e) { DataSet dataSet=null; if (cacheManager.Contains("AllContacts")) { dataSet = (DataSet)cacheManager["AllContacts"]; label1.Text = "from cache"; } else { Database db = DatabaseFactory.CreateDatabase(); DbCommand command = db.GetStoredProcCommand("dbo.GetAllContacts"); dataSet = db.ExecuteDataSet(command); // SqlServerDependency dependency = new SqlServerDependency( new SqlCommand("select ContactID,ContactName,ContactFirstName,ContactAge from dbo.Contact", new SqlConnection((ConfigurationManager.GetSection("connectionStrings") as ConnectionStringsSection).ConnectionStrings[1].ConnectionString) ) ); cacheManager.Add("AllContacts", dataSet, CacheItemPriority.Normal, null, new ICacheItemExpiration[] { dependency }); label1.Text = "from database"; } dataGridView1.DataSource = dataSet.Tables[0]; } |