数据库异步数据读取

几天在网上看到了一个数据的异步读取方法  记录下来以后也许能用得上。

   public void getdata()
    
{
        SqlConnection cn 
= new SqlConnection("server=.;database=salesdb_temp;uid=***;pwd=***;Asynchronous Processing = true");
        SqlCommand cmd 
= new SqlCommand("select top 2000000  CompanyCD ,BranchCD,JAN from mst_Product_20080327", cn);
        Response.Write(
"start .\n");
        
try
        
{
            cn.Open();                     
            IAsyncResult ar 
= cmd.BeginExecuteReader();
            Response.Write(
"here is do other things..\n");
            
int sum = 0;
            
for (int i = 0; i < 1000; i++)
            
{
                sum 
= sum + i;
            }

            Response.Write(sum);
            Response.Write(
"do other things finish!..\n");
            SqlDataReader r 
= cmd.EndExecuteReader(ar);
            
this.GridView1.DataSource = r;
            
this.GridView1.DataBind();
            r.Close();
        }

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

        
finally
        
{
            cn.Close();
            cmd.Clone();

        }



    }

其中最主要的就是在连接字符串的时候加上
Asynchronous Processing = true
另外对于线程的操作还有很多不熟悉的地方  以后多多努力。
资料参考
http://blog.csdn.net/ljianl/archive/2007/04/10/1559410.aspx


posted @ 2008-06-13 19:59  TerryLove  阅读(449)  评论(0编辑  收藏  举报