惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyberwarzone
Cyberwarzone
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
A
About on SuperTechFans
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Attack and Defense Labs
Attack and Defense Labs
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The GitHub Blog
The GitHub Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Webroot Blog
Webroot Blog
T
Tenable Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Microsoft Security Blog
Microsoft Security Blog
人人都是产品经理
人人都是产品经理
Simon Willison's Weblog
Simon Willison's Weblog
D
Docker
爱范儿
爱范儿
AI
AI
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
The Register - Security
The Register - Security
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
S
Securelist
Scott Helme
Scott Helme
B
Blog
Forbes - Security
Forbes - Security
Google DeepMind News
Google DeepMind News
T
The Blog of Author Tim Ferriss
月光博客
月光博客
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
F
Fortinet All Blogs
H
Help Net Security
Last Week in AI
Last Week in AI
N
News and Events Feed by Topic
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
I
InfoQ
P
Privacy International News Feed
V
V2EX
有赞技术团队
有赞技术团队
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Schneier on Security
T
Tailwind CSS Blog

博客园 - reagan

启动FM预算基金管理模块后,0L总账消失的解决办法 (转)找增强方法总结 ABAP开发工具概述(转) ALV简单模板1 我是怎么看friends练口语的(转贴) WCF绑定类型选择(转) powerbuilder9 连接 oracle 10g pb的网络资源【转】 powerbuider11 C/S 转换为B/S 转:将可执行文件注册成系统windows服务 转:利用reportviewer与C#生成报表 使用SerialPort类设计串口通讯程序 (VS2005)[转载] VS 2005 SP1简体中文版下载地址 C# aspx 数据绑定集中 - reagan Microsoft Expression Studio 2 简体中文正式版 Access数据类型与.net OleDbType枚举类型的对应 60个数据窗口技巧 C#常用函数和方法 【转】在ASP.NET中显示进度条
【转】ADO.NET2.0新特性-异步查询
reagan · 2007-10-17 · via 博客园 - reagan

ADO.NET2.0增加了一些新的特性,其中就包括异步查询。这个特点在需要执行多个查询的时候,或者查询过程比较常的时候就很有用。而默认情况下是不开启异步查询的,必须要在连接字符串中显示的打开,如下图,图中划线的就是需要显示制定的,后面一个是显示的打开MARS。

下面的代码包括了异步查询的三种方法,所以代码都可以测试运行,数据库是NorthWind.

其中一个运行页面如图:

首先是页面源码:

后台代码:

    public  string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindData();
            
//BindMultiData();
            
//BindMultiDataArray();
            
//BindDataWithCallBack();
        }

        
    }


    
//异步查询的poll方式,最普通的方式
    private void BindData()
    
{
        
string QueryStr = "SELECT * FROM customers";
        
using (SqlConnection Con = new SqlConnection(ConStr))
        
{
            SqlCommand Cmd 
= new SqlCommand(QueryStr, Con);
            IAsyncResult ASynResult;
            SqlDataReader Da;
            
try
            
{
                Con.Open();
                
//begin方法返回一个IAsyncResult对象,用来检查执行是否完成
                ASynResult = Cmd.BeginExecuteReader(CommandBehavior.CloseConnection);
                
while (!ASynResult.IsCompleted)
                
{
                    Response.Write(
"异步查询</br>");
                    ASynResult.AsyncWaitHandle.WaitOne(
3000true);
                    
//System.Threading.Thread.Sleep(10);
                }

                Da 
= Cmd.EndExecuteReader(ASynResult);
                GridView1.DataSource 
= Da;
                GridView1.DataBind();
            }

            
catch (Exception ex)
            
{
                Response.Write(ex.Message);
            }

        }


    }



    
//异步查询的wait方式,使用多个等待句柄来异步查询,必须等待所有进程完成处理结果集
    private void BindMultiData()
    
{
        
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
        
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
                
"Orders.OrderID, Orders.OrderDate, " +
                
"Orders.RequiredDate, Orders.ShippedDate " +
                
"FROM Orders, Customers " +
                
"WHERE Orders.CustomerID = Customers.CustomerID " +
                
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
                
"ORDER BY Customers.CompanyName, Customers.ContactName";
        
using (SqlConnection MyCon = new SqlConnection(ConStr))
        
{
            SqlCommand CusCmd 
= new SqlCommand(CusQueryStr, MyCon);
            SqlCommand SupCmd 
= new SqlCommand(SupQueryStr, MyCon);
            SqlDataReader CusDr;
            SqlDataReader SupDr;
            IAsyncResult CusIsynResult;
            IAsyncResult SupIsynResult;
            
//创建句柄数组
            System.Threading.WaitHandle[] WHandles = new System.Threading.WaitHandle[2];
            System.Threading.WaitHandle CusHandle;
            System.Threading.WaitHandle SupHandle;

            MyCon.Open();

            CusIsynResult 
= CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
            SupIsynResult 
= SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);

            CusHandle 
= CusIsynResult.AsyncWaitHandle;
            SupHandle 
= SupIsynResult.AsyncWaitHandle;

            
//将等待句柄赋给句柄数组
            WHandles[0= CusHandle;
            WHandles[
1= SupHandle;
            
//将数组传给waitall方法,等待所以的异步查询完成
            System.Threading.WaitHandle.WaitAll(WHandles);

            CusDr 
= CusCmd.EndExecuteReader(CusIsynResult);
            SupDr 
= SupCmd.EndExecuteReader(SupIsynResult);

            GridView1.DataSource 
= CusDr;
            GridView1.DataBind();

            GridView2.DataSource 
= SupDr;
            GridView2.DataBind();

            MyCon.Dispose();
            CusCmd.Dispose();
            SupCmd.Dispose();
        }




         
    }


    
//采用WaitAny方式,优点是不用等待所有进程都完成才处理结果集
    private void BindMultiDataArray()
    
{
        
string CusQueryStr = "SELECT * FROM customers WHERE CompanyName = 'Alfreds Futterkiste'";
        
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
                
"Orders.OrderID, Orders.OrderDate, " +
                
"Orders.RequiredDate, Orders.ShippedDate " +
                
"FROM Orders, Customers " +
                
"WHERE Orders.CustomerID = Customers.CustomerID " +
                
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
                
"ORDER BY Customers.CompanyName, Customers.ContactName";
       
using (SqlConnection MyCon = new SqlConnection(ConStr))
        
{
            SqlCommand CusCmd 
= new SqlCommand(CusQueryStr, MyCon);
            SqlCommand SupCmd 
= new SqlCommand(SupQueryStr, MyCon);
            SqlDataReader CusDr;
            SqlDataReader SupDr;
            IAsyncResult CusIsynResult;
            IAsyncResult SupIsynResult;
            System.Threading.WaitHandle[] WHandles 
= new System.Threading.WaitHandle[2];
            System.Threading.WaitHandle CusHandle;
            System.Threading.WaitHandle SupHandle;
            
int WHindex;

            MyCon.Open();

            CusIsynResult 
= CusCmd.BeginExecuteReader(CommandBehavior.CloseConnection);
            SupIsynResult 
= SupCmd.BeginExecuteReader(CommandBehavior.CloseConnection);

            CusHandle 
= CusIsynResult.AsyncWaitHandle;
            SupHandle 
= SupIsynResult.AsyncWaitHandle;

            WHandles[
0= CusHandle;
            WHandles[
1= SupHandle;

            
for (int i = 0; i < WHandles.Length; i++)
            
{
                
//waitany好处在于不必等待所有异步操作完成
                WHindex = System.Threading.WaitHandle.WaitAny(WHandles);
                
switch (WHindex)
                
{
                    
case 0:
                        CusDr 
= CusCmd.EndExecuteReader(CusIsynResult);
                        GridView1.DataSource 
= CusDr;
                        GridView1.DataBind();
                        
break;

                    
case 1:
                        SupDr 
= SupCmd.EndExecuteReader(SupIsynResult);
                        GridView2.DataSource 
= SupDr;
                        GridView2.DataBind();
                        
break;
                }

            }


            MyCon.Dispose();
            CusCmd.Dispose();
            SupCmd.Dispose();
        }




    }


    
//通过回调来实现异步查询
    private void BindDataWithCallBack()
    
{
        
string SupQueryStr = "SELECT Customers.CompanyName, Customers.ContactName, " +
                
"Orders.OrderID, Orders.OrderDate, " +
                
"Orders.RequiredDate, Orders.ShippedDate " +
                
"FROM Orders, Customers " +
                
"WHERE Orders.CustomerID = Customers.CustomerID " +
                
"AND Customers.CompanyName = 'Alfreds Futterkiste' " +
                
"ORDER BY Customers.CompanyName, Customers.ContactName";
        
using (SqlConnection MyCon = new SqlConnection(ConStr))
        
{
            SqlCommand SupCmd 
= new SqlCommand(SupQueryStr, MyCon);
            SqlDataReader SupDr;
            IAsyncResult SupIsynResult;
            MyCon.Open();

            AsyncCallback Callback 
= new AsyncCallback(CallBackMethod);
            SupIsynResult 
= SupCmd.BeginExecuteReader(Callback, SupCmd,CommandBehavior.CloseConnection);
            System.Threading.Thread.Sleep(
100);

            MyCon.Dispose();
            SupCmd.Dispose();
        }



    }


    
//回调方法
    public void CallBackMethod(IAsyncResult IResult)
    
{
        SqlCommand Command 
= (SqlCommand)IResult.AsyncState;
        SqlDataReader dr 
= Command.EndExecuteReader(IResult);
        GridView1.DataSource 
= dr;
        GridView1.DataBind();
    }

}

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1828642