Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| public class SqlDependencyExpiration : ICacheItemExpiration { private bool isExpired; public SqlDependencyExpiration(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; } #region ICacheItemExpiration Membres public bool HasExpired() { return isExpired; } #endregion } |
| private void button1_Click(object sender, EventArgs e) { DataTable dataTable = new DataTable(); if (manager.Get("1") != null) { dataTable =(DataTable) manager.Get("1").Value; label1.Text = "from cache"; } else { SqlCommand sqlCommand = new SqlCommand("Select ContactID,Contactname from dbo.[Contact]", new SqlConnection("Data Source=.;Initial Catalog=ContactDB;Integrated Security=SSPI;")); sqlCommand.Connection.Open(); dataTable.Load(sqlCommand.ExecuteReader()); label1.Text = "from sql server"; manager.Add("1", dataTable, new ICacheItemExpiration[] { new SqlDependencyExpiration(sqlCommand) }); } dataGridView1.DataSource = dataTable; } |