Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Asynchronous database callback

Asynchronous database callback

Scheduled Pinned Locked Moved C#
databasehelpworkspace
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Herman T Instance
    wrote on last edited by
    #1

    I have a small problem with retrieving a DataTable object from an asynchronous call. My setup: I have a Winfom app that has a reference to a library. In the Library I have a class called Run which does:

     public static DataTable GetAsyncDataTable(String Query, CommandType sqlCommandType)
            {
                Abstract.AbstractSqlCommand mySqlCommand = new Concrete.ConcreteSqlCommand();
    
                mySqlCommand.GetAsyncDataTableBegin(Query, sqlCommandType);
                
                queryDuration = mySqlCommand.queryDuration;
                return mySqlCommand.MyTable;
    
            }
    

    In the GetAsyncDataTableBegin method the code is

     public override IAsyncResult GetAsyncDataTableBegin(String Query, CommandType sqlCommandType)
            {
                return GetAsyncDataTableBegin(Query, null, sqlCommandType, null);
            }
    
    public override IAsyncResult GetAsyncDataTableBegin(String Query, List<SqlParameter> sqlParameters, CommandType sqlCommandType, String ConnectionString)
            {
                DateTime nu = DateTime.Now;
                SqlCommand command = Get(Query, sqlParameters, sqlCommandType, ConnectionString);
               return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection);
            }
    

    In the GetAsyncDataTableEnd Method the code is

    public override void GetAsyncDataTableEnd(IAsyncResult myResult)
            {
                SqlCommand command = null;
                SqlDataReader dr = null;
                DateTime nu = DateTime.Now;
                DataTable myTable = new DataTable();
    
                try
                {
                    command = (SqlCommand)myResult.AsyncState;
                    dr = command.EndExecuteReader(myResult);
                    myTable.Load(dr);
                    MyTable = myTable; //MyTable is a public override DataTable MyTable (property)
                }
                catch (Exception err)
                {
                    String poef = err.ToString();
                }
                finally
                {
                    if (null != dr)
                        dr.Close();
                    command.Connection.Close();
                    DateTime klaar = DateTime.Now;
                    queryDuration = (klaar - nu).Duration();
                    string Time = queryDuration.ToString();
                }
            }
    

    And now my problem: When I call the Run.GetAsyncDataTable (first code block), my data is retrieved asynchronously in another thread but the lines in Run.GetAsyncDataT

    B 1 Reply Last reply
    0
    • H Herman T Instance

      I have a small problem with retrieving a DataTable object from an asynchronous call. My setup: I have a Winfom app that has a reference to a library. In the Library I have a class called Run which does:

       public static DataTable GetAsyncDataTable(String Query, CommandType sqlCommandType)
              {
                  Abstract.AbstractSqlCommand mySqlCommand = new Concrete.ConcreteSqlCommand();
      
                  mySqlCommand.GetAsyncDataTableBegin(Query, sqlCommandType);
                  
                  queryDuration = mySqlCommand.queryDuration;
                  return mySqlCommand.MyTable;
      
              }
      

      In the GetAsyncDataTableBegin method the code is

       public override IAsyncResult GetAsyncDataTableBegin(String Query, CommandType sqlCommandType)
              {
                  return GetAsyncDataTableBegin(Query, null, sqlCommandType, null);
              }
      
      public override IAsyncResult GetAsyncDataTableBegin(String Query, List<SqlParameter> sqlParameters, CommandType sqlCommandType, String ConnectionString)
              {
                  DateTime nu = DateTime.Now;
                  SqlCommand command = Get(Query, sqlParameters, sqlCommandType, ConnectionString);
                 return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection);
              }
      

      In the GetAsyncDataTableEnd Method the code is

      public override void GetAsyncDataTableEnd(IAsyncResult myResult)
              {
                  SqlCommand command = null;
                  SqlDataReader dr = null;
                  DateTime nu = DateTime.Now;
                  DataTable myTable = new DataTable();
      
                  try
                  {
                      command = (SqlCommand)myResult.AsyncState;
                      dr = command.EndExecuteReader(myResult);
                      myTable.Load(dr);
                      MyTable = myTable; //MyTable is a public override DataTable MyTable (property)
                  }
                  catch (Exception err)
                  {
                      String poef = err.ToString();
                  }
                  finally
                  {
                      if (null != dr)
                          dr.Close();
                      command.Connection.Close();
                      DateTime klaar = DateTime.Now;
                      queryDuration = (klaar - nu).Duration();
                      string Time = queryDuration.ToString();
                  }
              }
      

      And now my problem: When I call the Run.GetAsyncDataTable (first code block), my data is retrieved asynchronously in another thread but the lines in Run.GetAsyncDataT

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Um ... do you understand what 'asynchronous' means? This is exactly what is supposed to happen; to retrieve the result from an asynchronous call you should provide a callback delegate, which is executed some time later, and in the meantime, your code continues to run. If you want the result to be available 'immediately' on the next line of code, why not use a synchronous query call?

      H 1 Reply Last reply
      0
      • B BobJanova

        Um ... do you understand what 'asynchronous' means? This is exactly what is supposed to happen; to retrieve the result from an asynchronous call you should provide a callback delegate, which is executed some time later, and in the meantime, your code continues to run. If you want the result to be available 'immediately' on the next line of code, why not use a synchronous query call?

        H Offline
        H Offline
        Herman T Instance
        wrote on last edited by
        #3

        Hey BobJanova, I do not want to run an synchronous call to the database because I have sometimes a report running for over more than 1 hour. Maybe my Run Class should be omitted and my form should use the Abstract/Concrete classes directly

        In Word you can only store 2 bytes. That is why I use Writer.

        B 1 Reply Last reply
        0
        • H Herman T Instance

          Hey BobJanova, I do not want to run an synchronous call to the database because I have sometimes a report running for over more than 1 hour. Maybe my Run Class should be omitted and my form should use the Abstract/Concrete classes directly

          In Word you can only store 2 bytes. That is why I use Writer.

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          If you have a report running for an hour you definitely don't want to be waiting for it to return! Yes, using the asynchronous database classes directly, and passing a callback to be called when the report is complete, might be a better way forward.

          H 1 Reply Last reply
          0
          • B BobJanova

            If you have a report running for an hour you definitely don't want to be waiting for it to return! Yes, using the asynchronous database classes directly, and passing a callback to be called when the report is complete, might be a better way forward.

            H Offline
            H Offline
            Herman T Instance
            wrote on last edited by
            #5

            But in which class do I define the delegate?

            In Word you can only store 2 bytes. That is why I use Writer.

            B 1 Reply Last reply
            0
            • H Herman T Instance

              But in which class do I define the delegate?

              In Word you can only store 2 bytes. That is why I use Writer.

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              Wherever you're making the database calls (probably in the main form).

              H 1 Reply Last reply
              0
              • B BobJanova

                Wherever you're making the database calls (probably in the main form).

                H Offline
                H Offline
                Herman T Instance
                wrote on last edited by
                #7

                No in the Concrete classes

                In Word you can only store 2 bytes. That is why I use Writer.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups