#副標=提升資料存取效能 #大標=ASP.NET 2.0資料存取新面貌(4) #眉標= ASP.NET #作者=文/王寧疆 ------------------box 程式1---------------------- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) //如果網頁是第一次執行,而不是PostBack { ObjectDataSource1.SelectParameters.Add("ConnectionString", SqlDataSource1.ConnectionString); //設定ObjectDataSource查詢時使用的資料庫連線資訊 } } -------------------------end----------------------------- -----------------------box 程式2------------------------ protected void btnEnable_Click(object sender, EventArgs e) { SqlCacheDependencyAdmin.EnableTableForNotifications(SqlDataSource1.ConnectionString, DropDownList1.Text); //設定使用者在DropDownList控制項上選擇的資料表可以在資 //料表中的記錄被修改時清除快取記憶體中和資料表相關的資料 lblMessage.Text = DropDownList1.Text + "資料表啟用成功"; //顯示啟用成功的訊息 lblMessage.ForeColor = Color.Blue; //設定訊息顏色 GridView1.DataBind(); //命令GridView控制項顯示所有和快取記憶體有關的資料表 } ---------------------------end--------------------------------- ------------------------box 程式3------------------------ protected void btnDisable_Click(object sender, EventArgs e) { SqlCacheDependencyAdmin.DisableTableForNotifications(SqlDataSource1.ConnectionString, DropDownList1.Text); //解除使用者在DropDownList控制項上選擇的資料表在資料表 //中的記錄被修改時清除快取記憶體中和資料表相關的資料的功能 lblMessage.Text = DropDownList1.Text + "資料表禁用成功"; //顯示禁用成功的訊息 lblMessage.ForeColor = Color.Red; //設定訊息顏色 } ------------------------------end------------------------------- -----------------------------box 程式4------------------------- CacheDependency txtDep = new CacheDependency(Server.MapPath("Storage.txt")); //為Storage.txt檔案建立//CacheDependency類別的物件 CacheDependency xmlDep = new CacheDependency(Server.MapPath("authors.xml")); //為authors.xml檔案建立 //CacheDependency類別的物件 CacheDependency[] DepArray = {txtDep, xmlDep}; //將兩個CacheDependency類別的物件放入陣列中 AggregateCacheDependency aggDep = new AggregateCacheDependency(); //建立AggregateCacheDependency類別的物件 aggDep.Add(DepArray); //將放置CacheDependency類別的物件的陣列 //加到AggregateCacheDependency類別的物件中 Cache.Insert("XMLDataSet", Source, aggDep); //將名稱為Source的物件放入快取記憶體中,指定索引鍵 //為XMLDataSet,並設定與AggregateCacheDependency類別的物件的相依關係 ---------------------------------end------------------------------------- ------------------box 程式5--------------------- public class RSSCacheDependency : CacheDependency { static Timer m_TickTock; //控制定時監督即時新聞變化的Timer物件 int m_PollingInterval; //存放定時查詢的時間間隔的變數 XmlDocument m_XmlDoc; //存放Xml文件的XmlDocument類別的物件 string m_Url; //即時新聞的來源網址 public void DetectChange(object sender) //負責查詢即時新聞是否有變化的方法 { RSSCacheDependency rssCD = (RSSCacheDependency)sender; //取得RSSCacheDependency物件 XmlDocument newDoc = GetDocument(m_Url); //依據即時新聞來源網址建立XmlDocument物件 if (newDoc.OuterXml != m_XmlDoc.OuterXml) //判斷是否與目前顯示的即時新聞內容有差異 { rssCD.NotifyDependencyChanged(rssCD, EventArgs.Empty); //命令放在快取記憶體中的資料失效 } } public XmlDocument GetDocument(string strUrl) //負責讀取即時新聞內容的方法 { XmlDocument xmlDoc = new XmlDocument(); //建立XmlDocument類別的物件 xmlDoc.Load(strUrl); //載入即時新聞內容 return xmlDoc; //傳回XmlDocument類別的物件 } public RSSCacheDependency(string strUrl, int PollingInterval) //建構函數 { m_Url=strUrl; //記下即時新聞的來源網址 m_PollingInterval=PollingInterval; //記下查詢的時間間隔 m_XmlDoc=GetDocument(m_Url); //載入即時新聞內容 if (m_TickTock == null) //如果Timer類別的物件尚未建立 { m_TickTock=new Timer(new TimerCallback(DetectChange), this, m_PollingInterval*1000, m_PollingInterval*1000); //設定每隔指定的時間要查詢即時新聞是否有變化 } } public XmlDocument RSSDocument //取得XmlDocument類別的物件的屬性 { get { return m_XmlDoc; } } protected override void DependencyDispose() //在物件被丟棄時釋放所佔用的資源 { m_TickTock = null; //釋放Timer類別的物件 base.DependencyDispose(); //呼叫父類別的DependencyDispose方法 } } --------------------------------end-------------------------- --------------------------box 程式6----------------------- protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) //判斷網頁是否為第一次顯示 { btnReal_Click(sender, e); //呼叫負責顯示即時新聞的功能 } } -------------------end------------------ -----------------box 程式7--------------- protected void btnReal_Click(object sender, EventArgs e) { XmlDocument doc = new XmlDocument(); //建立XmlDocument類別的物件 if (Cache["RealNews"] == null) //查看快取記憶體有沒有可以使用即時新聞資訊 { RSSCacheDependency rssCD = new RSSCacheDependency( "http://tw.news.yahoo.com/rss/realtime", 3600); //建立RSSCacheDependency類別的物件 doc.Load("http://tw.news.yahoo.com/rss/realtime"); //從遠端電腦載入即時新聞 Cache.Insert("RealNews", doc, rssCD); //將讀到的即時新聞放入快取記憶體,並設定與 //RSSCacheDependency類別的物件相依 Response.Write(DateTime.Now.ToString()+" ReRead!"); //顯示目前的日期時間與ReRead訊息 } else { doc = (XmlDocument)Cache["RealNews"]; //從快取記憶體取出即時新聞並放入XmlDocument物件 Response.Write(DateTime.Now.ToString() + " Read from Cache!"); //顯示目前的日期時間與Read from Cache!訊息 } XmlDataSource1.Data = doc.OuterXml; //將XmlDocument物件的內容當成XmlDataSource控制項的內容 FormView1.DataBind(); //命令FormView控制項顯示XmlDataSource控制項的內容 } --------------------------------end-------------------------------