您当前的位置:首页 > 网络编程 > Net编程 > 正文

Ado.net快马加鞭

【字体大小: 2009-03-31 16:56 来源:未知 作者:龙耀 

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

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

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

以下为引用的内容:

31041条纪录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调用,只是把System.Threading.WaitHandle.WaitAll换成,System.Threading.WaitHandle.WaitAny因为System.Threading.WaitHandle.WaitAny
    //可以在某一格进程结束后得到处理,修改try部分--注意看
    protected void Button4_Click(object sender, EventArgs e)
    {
        DateTime old = DateTime.Now;
        //实际上就是在第一个结果集是检索的源,第二个结果集实际上只要查询第一个结果集里面有的字段,不会在数据库中查寻,而是用第一个结果集
        SqlConnection DBCon = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringInfo"].ConnectionString);
        SqlCommand Table_1Command = new SqlCommand("select  * from Table_2 where Id>4000001", DBCon);//---这里执行查询后
        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];

        //封装等待对共享资源的独占访问的操作系统特定的对象。
        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++)
            {
                //--------返回完成执行等待句柄索引该数据在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();
    }

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

 

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

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

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

    顶一下
    (3)
    100%
    踩一下
    (0)
    0%
    【责任编辑:绝口不提】 标签:

    更多相关内容

    最新评论 查看所有评论
    发表评论 查看所有评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    评价:
    表情:
    用户名:密码:验证码: