Ado.net快馬加鞭 |
發(fā)布時間: 2012/9/11 16:56:52 |
連接池 Connection Timeout--嘗試連接數(shù)據(jù)存儲區(qū)時的等待時間默認(rèn)是15秒 Min Pool Size-連接池的最小容量 Max Pool Size-連接池最大容量默認(rèn)是100 Pooling 默認(rèn)是true則請求從連接池返回一個新的連接,沒有澤創(chuàng)建 Connection Reset表示在從連接池中刪除數(shù)據(jù)庫連接時,將會重置該連接,默認(rèn)是true,如果設(shè)置成false則在創(chuàng)建連接時往返服務(wù)器的次數(shù)會更少但是不更新連接狀態(tài) 如果出了毛病就~~SqlConnection.ClearAllPools();//清除連接池 ------然后是重頭戲~~自然是使用異步咯 1首先在連接字符串中設(shè)置一個 async=true -------理論就這么多了~~看段代碼爽爽把 31041條紀(jì)錄4秒 using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class Default5 : System.Web.UI.Page { PRotected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { DateTime old = DateTime.Now; SqlConnection DbCon; SqlCommand Command = new SqlCommand(); SqlDataReader OrdersReader; IAsyncResult AsyncResult;//異步 DbCon = new SqlConnection(); DbCon.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringInfo"].ConnectionString; Command.Connection = DbCon; Command.CommandText = "Select"; Command.CommandType = CommandType.StoredProcedure; Command.Connection = DbCon; try { DbCon.Open(); AsyncResult = Command.BeginExecuteReader(); while (!AsyncResult.IsCompleted)//獲取異步操作是否已完成的指示。 { //由于異步操作必須阻止線程秒鐘 System.Threading.Thread.Sleep(10); } OrdersReader = Command.EndExecuteReader(AsyncResult); GridView1.DataSource = OrdersReader; GridView1.DataBind(); } catch (System.Exception) { } TimeSpan not=DateTime.Now-old; Label1.Text = not.Seconds.ToString(); } } - -上面的只是小事伸手~~來個速度更快的 //最強大的wait調(diào)用,只是把System.Threading.WaitHandle.WaitAll換成,System.Threading.WaitHandle.WaitAny因為System.Threading.WaitHandle.WaitAny //可以在某一格進程結(jié)束后得到處理,修改try部分--注意看 protected void Button4_Click(object sender, EventArgs e) { DateTime old = DateTime.Now; //實際上就是在第一個結(jié)果集是檢索的源,第二個結(jié)果集實際上只要查詢第一個結(jié)果集里面有的字段,不會在數(shù)據(jù)庫中查尋,而是用第一個結(jié)果集 SqlConnection DBCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringInfo"].ConnectionString); SqlCommand Table_1Command = new SqlCommand("select * from Table_2 where Id>4000001", DBCon);//---這里執(zhí)行查詢后 SqlCommand MMCommand = new SqlCommand("select Title ,Content from MM,Table_2 where MM.ID=Table_2.Id", DBCon);//Table_2.Id其實是上面的Table_2地一列 Table_1Command.CommandType = CommandType.Text; MMCommand.CommandType = CommandType.Text; SqlDataReader Table_1DataReader; SqlDataReader MMDataReader; IAsyncResult Table_1AsyncResult; IAsyncResult MMAsyncResult; System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2]; //封裝等待對共享資源的獨占訪問的操作系統(tǒng)特定的對象。 System.Threading.WaitHandle Table_1Whandle; System.Threading.WaitHandle MMWhandle; try { DBCon.Open(); Table_1AsyncResult = Table_1Command.BeginExecuteReader(); MMAsyncResult = MMCommand.BeginExecuteReader(); Table_1Whandle = Table_1AsyncResult.AsyncWaitHandle; MMWhandle = MMAsyncResult.AsyncWaitHandle; WHandles[0] = Table_1Whandle; WHandles[1] = MMWhandle; System.Threading.WaitHandle.WaitAny(WHandles); for (int index = 0; index < 2; index++) { //--------返回完成執(zhí)行等待句柄索引該數(shù)據(jù)在WHandles索引里面的某個 int whindex = System.Threading.WaitHandle.WaitAny(WHandles); switch (whindex) { //注意這里必須和上面裝入WHandles集合的索引一樣 case 0: Table_1DataReader = Table_1Command.EndExecuteReader(Table_1AsyncResult); GridView1.DataSource = Table_1DataReader; GridView1.DataBind(); break; case 1: MMDataReader = MMCommand.EndExecuteReader(MMAsyncResult); GridView2.DataSource = MMDataReader; GridView2.DataBind(); break; } } } catch (System.Exception) { } finally { DBCon.Close(); } TimeSpan not = DateTime.Now - old; Label1.Text = not.Seconds.ToString(); } ~~上面的可是高級應(yīng)用--不過在怎么提速安全第一 首先要設(shè)置三臺服務(wù)器~~或者是三個sqlserver實例咯 主要服務(wù)器為。 景象服務(wù)器為。\Partner 觀察者服務(wù)器為。\Witness 然后再連接字符串中設(shè)置 FailOver Parter=".\Partner"即可 --當(dāng)往主服務(wù)器中插入數(shù)據(jù)的時候竟象服務(wù)器也會插入數(shù)據(jù),如果主服務(wù)器停止工作則景象服務(wù)器被觀察者服務(wù)器設(shè)置為主服務(wù)器 本文出自:億恩科技【1tcdy.com】 服務(wù)器租用/服務(wù)器托管中國五強!虛擬主機域名注冊頂級提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM] |