知识大全 ADO.NET 2.0 数据异步处理改善用户体验

Posted

篇首语:社会的善意点燃了希望的火苗,但要让生活火起来,还是要靠自己。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 ADO.NET 2.0 数据异步处理改善用户体验相关的知识,希望对你有一定的参考价值。

ADO.NET 2.0 数据异步处理改善用户体验  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  支持异步处理的提供程序有 System Data SqlClient在针对大批量数据插入过更新时 使用异步处理方法可以不用等待多有数据更新完毕才能操作或者进行下步处理 改善了用户体验  SqlCommand对象方法如下

  异步方法 BeginExecuteNonQuery   EndExecuteNonQuery           BeginExecuteXmlReader   EndExecuteXmlReader           BeginExecuteReader   EndExecuteReader  begin前缀的方法传入参数 end前缀的方法返回输出参数和返回值  begin前缀方法返回的是IAsyncResult  用于追踪一步方法的执行状态   IAsyncResult AsnycState 用户自定义的状态对象   IAsyncResult AsnycWaitHandle 呼叫代码的等待形式 是等其中的一个异步方法完成还是全部完成   IAsyncResult CompletedSynchronously  获取所有异步方法是否同时完成   IAsyncResult Ispleted 是否执行完毕 可以根据此属性进行下部动作在连接字符串中加入 async=true    如果所有的命令都是同步的建议在连接字符串中显施加入 async=false    如果有一部分命令是异步执行 又有一部分是同步同步执行 建议分别建立两个连接对象   如果 async=true 时 也可以执行同步命令 但是会损失一些资源

  /**///// obtain connection strings from configuration files or            //// similar facility            //// NOTE: these connection strings have to include async=true for            //// example:            //// server=myserver;database=mydb;integrated security=true;async=true             //string connstrAccouting = GetConnString( accounting );            //string connstrHR = GetConnString( humanresources );            /**///// define o connection objects one for each database            //using (SqlConnection connAcc = new SqlConnection(connstrAccounting))            //using (SqlConnection connHumanRes = new SqlConnection(connstrHR))            //            //    // open the first connection            //    connAcc Open();            //    // start the execution of the first query contained in the            //    // employee_info stored procedure            //    SqlCommand cmdAcc = new SqlCommand( employee_info connAcc);            //    cmdAcc CommandType = CommandType StoredProcedure;            //    cmdAcc Parameters AddWithValue( @empl_id employee_id);            //    IAsyncResult arAcc = cmdAcc BeginExecuteReader();            //    // at this point the employee_info stored proc is executing on            //    // the server and this thread is running at the same time            //    // now open the second connection            //    connHumanRes Open();            //    // start the execution of the second stored proc against            //    // the human resources server            //    SqlCommand cmdHumanRes = new SqlCommand( employee_hrinfo             //                                            connHumanRes);            //    cmdHumanRes Parameters AddWithValue( @empl_id employee_id);            //    IAsyncResult arHumanRes = cmdHumanRes BeginExecuteReader();            //    // now both queries are running at the same time            //    // at this point; more work can be done from this thread or we            //    // can simply wait until both mands finish in our case we ll            //    // wait            //    SqlDataReader drAcc = cmdAcc EndExecuteReader(arAcc);            //    SqlDataReader drHumanRes = cmdHumanRes EndExecuteReader(arHumanRes);            //    // now we can render the results for example bind the readers to an ASP NET            //    // web control or scan the reader and draw the information in a            //    // WebForms form             //

  string custid = ALFKI ;            string orderid = ;

  // NOTE: connection strings denoted by connstring have to include            // async=true for example:            string connstring = server=(local);database=northwind;integrated security=true;async=true ;            // we ll use three connections for this            using (SqlConnection c = new SqlConnection(connstring))            using (SqlConnection c = new SqlConnection(connstring))            using (SqlConnection c = new SqlConnection(connstring))                            // get customer info                c Open();                SqlCommand cmd = new SqlCommand(                  SELECT CustomerID CompanyName ContactName FROM Customers WHERE CustomerID=@id c );                cmd Parameters Add( @id SqlDbType Char ) Value = custid;                IAsyncResult arCustomer = cmd BeginExecuteReader();                // get orders                c Open();                SqlCommand cmd = new SqlCommand( SELECT * FROM Orders WHERE CustomerID=@id c );                cmd Parameters Add( @id SqlDbType Char ) Value = custid;                IAsyncResult arOrders = cmd BeginExecuteReader();                // get order detail if user picked an order                IAsyncResult arDetails = null;                SqlCommand cmd = null;                if (null != orderid)                                    c Open();                    cmd = new SqlCommand( SELECT * FROM [Order Details] WHERE OrderID=@id c );                    cmd Parameters Add( @id SqlDbType Int) Value = int Parse(orderid);                    arDetails = cmd BeginExecuteReader();                                // build the wait handle array for WaitForMultipleObjects                WaitHandle[] handles = new WaitHandle[null == arDetails ? : ];                handles[ ] = arCustomer AsyncWaitHandle;                handles[ ] = arOrders AsyncWaitHandle;                if (null != arDetails)                    handles[ ] = arDetails AsyncWaitHandle;                // wait for mands to plete and render page controls as we                // get data back                SqlDataReader r;                DataTable dt;                for (int results = (null == arDetails) ? : ; results < ; results++)                                    // wait for any handle then process results as they e                    int index = WaitHandle WaitAny(handles false); // secs                    if (WaitHandle WaitTimeout == index)                        throw new Exception( Timeout );                    switch (index)                                            case : // customer query is ready                            r = cmd EndExecuteReader(arCustomer);                            if (!r Read())                                continue;                            lblCustomerID Text = r GetString( );                            lblCompanyName Text = r GetString( );                            lblContact Text = r GetString( );                            r Close();                            break;                        case : // orders query is ready                            r = cmd EndExecuteReader(arOrders);                            dt = new DataTable();                            dt Load(r);                            dgOrders DataSource = dt; // data bind to the orders grid                            dgOrders Refresh();                            r Close();                            break;                        case : // details query is ready                            r = cmd EndExecuteReader(arDetails);                            dt = new DataTable();                            dt Load(r);                            dgDetails DataSource = dt; // data bind to the details grid                            dgDetails Refresh();                            r Close();                            break;

cha138/Article/program/net/201311/11392

相关参考

知识大全 体验.net 2.0 的优雅 - 异步WebService

体验.net2.0的优雅-异步WebService  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 

知识大全 使用ADO.NET2.0提升数据交互性能(2)

使用ADO.NET2.0提升数据交互性能(2)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!三异步

知识大全 用ADO.NET处理层次数据

用ADO.NET处理层次数据  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  ADONET为数据在

知识大全 使用ADO.NET 和C# 处理BLOB 数据

使用ADO.NET和C#处理BLOB数据  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  使用Mi

知识大全 剖析 ADO.NET 批处理更新(深入研究数据访问)

剖析ADO.NET批处理更新(深入研究数据访问)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  

知识大全 ADO.NET 2.0中的DataSet和DataTable

ADO.NET2.0中的DataSet和DataTable  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一

知识大全 ADO.NET 2.0 动态指定 ObjectDataSource 的 SelectMethod

ADO.NET2.0动态指定ObjectDataSource的SelectMethod  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布

知识大全 经测试ADO.NET 2.0竟然比1.0要慢

经测试ADO.NET2.0竟然比1.0要慢  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  镜子里

知识大全 ADO.NET 2.0 Dataset和Datatable 新功能新

ADO.NET2.0Dataset和Datatable新功能新  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来

知识大全 ADO.NET性能改善方法集合

ADO.NET性能改善方法集合  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  总的考虑方向  )