#眉標=Enterprise Library #副標=設計模型套件系列(4) #大標=快取記憶體控制專家-Caching Application Block #作者=文/王寧疆 ==程式1 =========== if (!Cache.Contains("鍵值")) { //資料已經不存在於快取記憶體中 } 或 if (Cache.GetData(categoryIDTextBox.Text) == null) { //資料已經不存在於快取記憶體中 } ================ ==程式2 =========== private void categoryIDTextBox_TextChanged(object sender, EventArgs e) { byte[] buf=null; //用來儲存圖形欄位內容的陣列 CacheManager Cache = CacheFactory.GetCacheManager("ImageCache"); //取得CacheManager類別的物件 if (Cache.GetData(categoryIDTextBox.Text) == null) //判斷欲使用的資料是否存在於快取記憶體中 { string strSQL = "Select Picture from Categories where CategoryID=" +   "@CategoryID"; //查詢圖形欄位的SQL敘述 SqlCommand cmd = new SqlCommand(strSQL,   categoriesTableAdapter.Connection); //建立SqlCommand類別的物件 cmd.CommandType = CommandType.Text; //設定SqlCommand類別的物件的CommandText屬性 SqlParameter p = new SqlParameter("@CategoryID", SqlDbType.Int); //準備SQL敘述執行需要的參數 p.Direction = ParameterDirection.Input; //設定參數的傳入傳出方向 p.Value = categoryIDTextBox.Text; //設定參數的內容值 cmd.Parameters.Add(p); //將參數加到SqlCommand類別的物件的Parameters集合中 if (categoriesTableAdapter.Connection.State == ConnectionState.Open) //檢查資料庫連線的狀態 { categoriesTableAdapter.Connection.Close(); //關閉程式其他部分使用過的資料庫連線 } categoriesTableAdapter.Connection.Open(); //開啟資料庫連線 buf = (byte[])cmd.ExecuteScalar(); //執行查詢圖形欄位的SQL敘述 Cache.Add(categoryIDTextBox.Text, buf, CacheItemPriority.Normal, null,   new AbsoluteTime(DateTime.Now.AddMinutes(10))); //將查詢得到的資料存入快取記憶體並設定逾時時間為10分鐘 cmd.Dispose(); //丟棄SqlCommand類別的物件 categoriesTableAdapter.Connection.Close(); //關閉資料庫連線 } buf=(byte[])Cache.GetData(categoryIDTextBox.Text); //從快取記憶體取出圖形欄位的內容 MemoryStream ms = new   MemoryStream(buf); //將存放圖形欄位內容的陣列準備成MemoryStream類別的物件 Bitmap bmp = new Bitmap(ms); //將MemoryStream類別的物件製作Bitmap類別的物件 pictureBox1.Image = bmp; //將準備好的Bitmap類別的物件交給PictureBox控制項顯示 ms.Close(); //關閉MemoryStream類別的物件 } ================