zoukankan      html  css  js  c++  java
  • ADO.NET 2.0 数据异步处理改善用户体验

    支持异步处理的提供程序有 System.Data.SqlClient
    在针对大批量数据插入过更新时,使用异步处理方法可以不用等待多有数据更新完毕才能操作或者进行下步处理,改善了用户体验
      SqlCommand对象方法如下:
      异步方法 BeginExecuteNonQuery   EndExecuteNonQuery
               BeginExecuteXmlReader   EndExecuteXmlReader
               BeginExecuteReader   EndExecuteReader
      begin前缀的方法传入参数,end前缀的方法返回输出参数和返回值
      begin前缀方法返回的是IAsyncResult  用于追踪一步方法的执行状态
       IAsyncResult .AsnycState 用户自定义的状态对象
       IAsyncResult .AsnycWaitHandle 呼叫代码的等待形式,是等其中的一个异步方法完成还是全部完成
       IAsyncResult .CompletedSynchronously  获取所有异步方法是否同时完成
       IAsyncResult .Iscompleted 是否执行完毕,可以根据此属性进行下部动作
    在连接字符串中加入"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 two 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 commands 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 = "10643";

                
    // 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 c1 = new SqlConnection(connstring))
                
    using (SqlConnection c2 = new SqlConnection(connstring))
                
    using (SqlConnection c3 = new SqlConnection(connstring))
                
    {
                    
    // get customer info
                    c1.Open();
                    SqlCommand cmd1 
    = new SqlCommand(
                      
    "SELECT CustomerID, CompanyName, ContactName FROM Customers WHERE CustomerID=@id", c1);
                    cmd1.Parameters.Add(
    "@id", SqlDbType.Char, 5).Value = custid;
                    IAsyncResult arCustomer 
    = cmd1.BeginExecuteReader();
                    
    // get orders
                    c2.Open();
                    SqlCommand cmd2 
    = new SqlCommand("SELECT * FROM Orders WHERE CustomerID=@id", c2);
                    cmd2.Parameters.Add(
    "@id", SqlDbType.Char, 5).Value = custid;
                    IAsyncResult arOrders 
    = cmd2.BeginExecuteReader();
                    
    // get order detail if user picked an order
                    IAsyncResult arDetails = null;
                    SqlCommand cmd3 
    = null;
                    
    if (null != orderid)
                    
    {
                        c3.Open();
                        cmd3 
    = new SqlCommand("SELECT * FROM [Order Details] WHERE OrderID=@id", c3);
                        cmd3.Parameters.Add(
    "@id", SqlDbType.Int).Value = int.Parse(orderid);
                        arDetails 
    = cmd3.BeginExecuteReader();
                    }

                    
    // build the wait handle array for WaitForMultipleObjects
                    WaitHandle[] handles = new WaitHandle[null == arDetails ? 2 : 3];
                    handles[
    0= arCustomer.AsyncWaitHandle;
                    handles[
    1= arOrders.AsyncWaitHandle;
                    
    if (null != arDetails)
                        handles[
    2= arDetails.AsyncWaitHandle;
                    
    // wait for commands to complete and render page controls as we 
                    
    // get data back
                    SqlDataReader r;
                    DataTable dt;
                    
    for (int results = (null == arDetails) ? 1 : 0; results < 3; results++)
                    
    {
                        
    // wait for any handle, then process results as they come
                        int index = WaitHandle.WaitAny(handles, 5000false); // 5 secs
                        if (WaitHandle.WaitTimeout == index)
                            
    throw new Exception("Timeout");
                        
    switch (index)
                        
    {
                            
    case 0// customer query is ready
                                r = cmd1.EndExecuteReader(arCustomer);
                                
    if (!r.Read())
                                    
    continue;
                                lblCustomerID.Text 
    = r.GetString(0);
                                lblCompanyName.Text 
    = r.GetString(1);
                                lblContact.Text 
    = r.GetString(2);
                                r.Close();
                                
    break;
                            
    case 1// orders query is ready
                                r = cmd2.EndExecuteReader(arOrders);
                                dt 
    = new DataTable();
                                dt.Load(r);
                                dgOrders.DataSource 
    = dt; // data-bind to the orders grid
                                dgOrders.Refresh();
                                r.Close();
                                
    break;
                            
    case 2// details query is ready
                                r = cmd3.EndExecuteReader(arDetails);
                                dt 
    = new DataTable();
                                dt.Load(r);
                                dgDetails.DataSource 
    = dt; // data-bind to the details grid
                                dgDetails.Refresh();
                                r.Close();
                                
    break;

                        }

                    }

                }
  • 相关阅读:
    未能加载文件或程序集“System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件。
    无法识别的属性“targetFramework”。请注意属性名称区分大小写。
    The provider is not compatible with the version of Oracle client
    新建MVC3项目时出错:错误 2 类型“System.Web.Mvc.ModelClientValidationRule”同时存在于“C:\Program Files\Microsoft ASP.NET\ASP.NET Web Pages\v2.0\Assemblies\System.Web.WebPages.dll”和“C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assembli
    MVC3 在IIS7 部署
    IIS7 IIS6 解析JSON (解决JSON格式的 Method Not Allowed )
    Administrator Note: An error message detailing the cause of this specific request failure can be found in the application event log of the web server.
    在学习js的然后写代码的过程中我老是找不到思路怎么办?
    你男朋友是程序员吧
    杨绛先生送给年轻人的9句话,值得一读再读!
  • 原文地址:https://www.cnblogs.com/ghd258/p/264839.html
Copyright © 2011-2022 走看看