Ado.net快马加鞭~~(提速)

当然首先是使用链接池了~~
连接池
Connection Timeout--尝试连接数据存储区时的等待时间默认是15秒
Min Pool Size-连接池的最小容量
Max Pool Size-连接池最大容量默认是100
Pooling 默认是true则请求从连接池返回一个新的连接,没有泽创建

Connection Reset表示在从连接池中删除数据库连接时,将会重置该连接,默认是true,如果设置成false则在创建连接时往返服务器的次数会更少但是不更新连接状态

如果出了毛病就~~SqlConnection.ClearAllPools();//清除连接池
------然后是重头戏~~自然是使用异步咯
1首先在连接字符串中设置一个 async=true
-------理论就这么多了~~看段代码爽爽把

None.gif
None.gif31041条纪录4秒
None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Collections;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
public  partial  class  Default5 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        DateTime old 
= DateTime.Now;
InBlock.gif
InBlock.gif        SqlConnection DbCon;
InBlock.gif        SqlCommand Command 
= new SqlCommand();
InBlock.gif        SqlDataReader OrdersReader;
InBlock.gif        IAsyncResult AsyncResult;
//异步
InBlock.gif

InBlock.gif
InBlock.gif        DbCon 
= new SqlConnection();
InBlock.gif        DbCon.ConnectionString 
= System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringInfo"].ConnectionString;
InBlock.gif        Command.Connection 
= DbCon;
InBlock.gif
InBlock.gif
InBlock.gif        Command.CommandText 
= "Select";
InBlock.gif        Command.CommandType 
= CommandType.StoredProcedure;
InBlock.gif        Command.Connection 
= DbCon;
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DbCon.Open();
InBlock.gif            AsyncResult 
= Command.BeginExecuteReader();
InBlock.gif            
while (!AsyncResult.IsCompleted)//获取异步操作是否已完成的指示。
ExpandedSubBlockStart.gifContractedSubBlock.gif
            dot.gif{
InBlock.gif                
//由于异步操作必须阻止线程秒钟
InBlock.gif
                System.Threading.Thread.Sleep(10);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            OrdersReader 
= Command.EndExecuteReader(AsyncResult);
InBlock.gif            GridView1.DataSource 
= OrdersReader;
InBlock.gif            GridView1.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        TimeSpan not
=DateTime.Now-old;
InBlock.gif        Label1.Text 
= not.Seconds.ToString();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

- -上面的只是小事伸手~~来个速度更快的

None.gif      // 最强大的wait调用,只是把System.Threading.WaitHandle.WaitAll换成,System.Threading.WaitHandle.WaitAny因为System.Threading.WaitHandle.WaitAny
None.gif    
// 可以在某一格进程结束后得到处理,修改try部分--注意看
None.gif
     protected   void  Button4_Click( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        DateTime old 
= DateTime.Now;
InBlock.gif        
//实际上就是在第一个结果集是检索的源,第二个结果集实际上只要查询第一个结果集里面有的字段,不会在数据库中查寻,而是用第一个结果集
InBlock.gif
        SqlConnection DBCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringInfo"].ConnectionString);
InBlock.gif        SqlCommand Table_1Command 
= new SqlCommand("select  * from Table_2 where Id>4000001", DBCon);//---这里执行查询后
InBlock.gif
        SqlCommand MMCommand = new SqlCommand("select Title ,Content from MM,Table_2 where MM.ID=Table_2.Id", DBCon);//Table_2.Id其实是上面的Table_2地一列
InBlock.gif

InBlock.gif        Table_1Command.CommandType 
= CommandType.Text;
InBlock.gif        MMCommand.CommandType 
= CommandType.Text;
InBlock.gif
InBlock.gif        SqlDataReader Table_1DataReader;
InBlock.gif        SqlDataReader MMDataReader;
InBlock.gif
InBlock.gif        IAsyncResult Table_1AsyncResult;
InBlock.gif        IAsyncResult MMAsyncResult;
InBlock.gif
InBlock.gif        System.Threading.WaitHandle[] WHandles 
= new System.Threading.WaitHandle[2];
InBlock.gif
InBlock.gif        
//封装等待对共享资源的独占访问的操作系统特定的对象。
InBlock.gif
        System.Threading.WaitHandle Table_1Whandle;
InBlock.gif        System.Threading.WaitHandle MMWhandle;
InBlock.gif
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            DBCon.Open();
InBlock.gif            Table_1AsyncResult 
= Table_1Command.BeginExecuteReader();
InBlock.gif            MMAsyncResult 
= MMCommand.BeginExecuteReader();
InBlock.gif
InBlock.gif            Table_1Whandle 
= Table_1AsyncResult.AsyncWaitHandle;
InBlock.gif            MMWhandle 
= MMAsyncResult.AsyncWaitHandle;
InBlock.gif
InBlock.gif            WHandles[
0= Table_1Whandle;
InBlock.gif            WHandles[
1= MMWhandle;
InBlock.gif
InBlock.gif            System.Threading.WaitHandle.WaitAny(WHandles);
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
for (int index = 0; index < 2; index++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//--------返回完成执行等待句柄索引该数据在WHandles索引里面的某个
InBlock.gif
                int whindex = System.Threading.WaitHandle.WaitAny(WHandles);
InBlock.gif                
switch (whindex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//注意这里必须和上面装入WHandles集合的索引一样
InBlock.gif
                    case 0:
InBlock.gif                        Table_1DataReader 
= Table_1Command.EndExecuteReader(Table_1AsyncResult);
InBlock.gif                        GridView1.DataSource 
= Table_1DataReader;
InBlock.gif                        GridView1.DataBind();
InBlock.gif                        
break;
InBlock.gif                    
case 1:
InBlock.gif
InBlock.gif                        MMDataReader 
= MMCommand.EndExecuteReader(MMAsyncResult);
InBlock.gif                        GridView2.DataSource 
= MMDataReader;
InBlock.gif                        GridView2.DataBind();
InBlock.gif
InBlock.gif                        
break;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif        
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DBCon.Close();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif        TimeSpan not 
= DateTime.Now - old;
InBlock.gif        Label1.Text 
= not.Seconds.ToString();
ExpandedBlockEnd.gif    }

~~上面的可是高级应用--不过在怎么提速安全第一
首先要设置三台服务器~~或者是三个sqlserver实例咯

fffffffffffffffff.jpg

主要服务器为.
景象服务器为.\Partner
观察者服务器为.\Witness

然后再连接字符串中设置 FailOver Parter=".\Partner"即可

--当往主服务器中插入数据的时候竟象服务器也会插入数据,如果主服务器停止工作则景象服务器被观察者服务器设置为主服务器14.gif

转载于:https://www.cnblogs.com/ajaxren/archive/2007/05/21/753609.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值