c# - sql async query problem -


So, why does not it ever call for the callback function?

  using the system; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; Using System.Data.SqlClient; Namespace sqlAsyncTesting {public partial class} form 1: form {public form 1 () {initial group (); } Private void button1_Click (object sender, EventArgs e) using {(SqlConnection Conn = new SqlConnection (@ "Data Source = bigapple; initial catalog = master; Integrated Security = SSPI; Asynchronous Processing = true")) {conn.Open (); SqlCommand cmd = new SQL Command (@ "WAITFOR DELAY '00: 03 '; up 3 SELECT * FROM sysobjects;', con); IAsyncResult result = cmd.BeginExecuteReader (new AsyncCallback (Handlcolbak), CMD, Kmmandbavik. Clojhkshn);} } private void handle callback (IAsync results) {dr SqlDataReader; SqlCommand _this = (SqlCommand) results. AsyncState; if (result. IP contained) {dr = _this.EndExecuteReader (result);} dr Else = null; Detataibl dt = New DataTable (); Dataset DS = New Dataset (); dt.Load (Dr.); ds.Tables.Add (DT); dr.Close (); Full (DS);} Private Zero Uarn (datasets DS) {string output = string.Empty; Foreign Exchange (Detacolm C Distibl [0]. Columns) {output + = C. Colmnam + "\ t";} output + = "\ r \ n"; Agrgn (Detaro DR in Distibls [0] Kro.) {foreign object (draw. item object array I) {output + = i.ToString () + "\ t";} output + = "\ r \ n"; at some point I noticed:  
  1. after the callback method I WAITFOR delay stmt was called removed.
  2. there is no need to vote for result.IsCompleted Is because the callback After the respect removed is completed async processing.
  3. Obviously because by default Dr = no need to set the null in the rest it will be zero.
  4. You must handle InvalidOperationException and ArgumentException in the HandleCallback method.
  5. Whenever, in the handle callback, the endexit reader () was called I was getting the exception "asynchronous operation has already been completed." That's why I d. Never was able to get results in

    If you are facing the problem listed in the issue number. 5, you can use the following alternative solution implemented by using the asynchronous representative instead of the built-in BeginExecuteReader () and EndExecuteReader (). Also in the solution below, the control will immediately be returned to the next line after the representative starts, as if it was in the case of BeginExecuteReader ()

    Optional solution:. Public Partial Category Form 2: Form {Public Form 2 () {Initial Group (); } Personal Representative Dataset GetDSDelegate (String Query); Private Zero Button 1_Click (Object Sender, Event AGS E) {GetDSDelegate del = new GetDSDelegate (GetDataSetAsync); Del.BeginInvoke (@ "Top 3 of Table 1 Select;", blank, blank); } Private Dataset GetDataSetAsync (string query) {DataSet ds; (Using SqlConnection Conn = New SqlConnection (@ "Data Source = mmmmm000011 \ sqlexpress; Initial List = SOExamples; Integrated Security = SSPI; Asynchronous Processing = true")) (SqlCommand cmd = New SqlCommand (Query, Connecticut }} {Try {Conn.Open (); SqlDataReader dr = cmd.ExecuteReader (); Datatable dt = new datatable (); Ds = new dataset (); Dt.load (dr); Ds.Tables.Add (DT); Dr.Close (); Full (DS); } Finally {if (conn.State! = ConnectionState.Closed) conn.Close (); }} MessageBox.Show ("Done !!!"); Return ds; } Private Zero Complete (Dataset DS) {...}}


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -