Sql參數緩衝技術的應用

來源:互聯網
上載者:User

1.把sql語句的參數緩衝起來應用速度快效率高 <一般是放在DBHelper通用類裡>

通常是用雜湊表來存放sql的參數達到緩衝的機制即首先要聲明一個雜湊表

private static HashTable  parmCache=HashTable.Synchronized(new HashTable());

 

 

2.把sql語句的參數緩衝起來,則要寫一個緩衝的方法

      兩個參數:一緩衝的key值, 二 緩衝的參數值

    public static void CacheParameters(string cacheKey, params SqlParameter [] cmdParameters)

{

    parmCache[cacheKey]=commandParameters;

}

   

3.取出來緩衝裡的參數,注意要取出hashTable裡的數,則要把hashTable表裡的書複製出來

   只需要通過Key值則可以取出雜湊表裡緩衝的數組 即一個參數:key

 public static SqlParameter[] GetCacheParameters(string cachekey)

{

    //第一步先把雜湊表數群組轉換sql參數數群組類型

    SqlParameter[] cacheParams=(Sqloarameter [] )parmCache[cacheKey];

 

    if(cacheParams==null)

{

  return null;

}

  //第二步轉換成功之後:聲明一個指定長度的即將要複製的sal參數數組

  // 長度為剛轉換的雜湊參數數組的長度

  SqlParameter [] clonedParam=new SqlParamerer[cacheParams.Length];

  

 

   //第三步進行複製

  for(int i=0, j=cachedParams.Length; i<j; i++)

{

  clonedParam[i]=(SqlParameter)((ICloneable)cachedParams[i]).Clone();

return cloneParam

}

}

          

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.