Results 1 to 8 of 8

Thread: [RESOLVED] [2008] SQLcommand Async methods worth using?

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [RESOLVED] [2008] SQLcommand Async methods worth using?



    I'm working on a project that needs quite a bit of interaction with an SQL database, mainly loading and saving data to and from DGVs on my forms.
    I noticed that instead of running the standard synchronous methods of the SQLCommand class you can also run some Async ones too such as BeginExecuteNonQuery() and BeginExecuteReader(). So I did a quick bit of reading up on the MSDN site and to be honest it looks like quite a bit of work learning how exactly to implement this along with the EndExecuteReader() etc methods to actually return the results. Then once I had figured out how to use them I would obviously have to replace all the existing synchronous methods I have already written. So I was wondering if anyone here has used these methods much in the past? I'm not dragging huge amounts of data from the SQL tables so load times in my application are pretty small as it is but as I'm sure you all know even a small delay between screens (the UI blocking I mean) can give users cause to label your software as "slow" and "unresponsive"...

    So basically, my question is has anyone used these methods previously and if so is it a lot of work implementing them or should I give it a go just to cut out all possible blocks of my UI thread?

    Ta
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2008] SQLcommand Async methods worth using?

    Did you consider useing background worker tasks instead?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] SQLcommand Async methods worth using?

    Yes but I thought if there are methods specifically for this on the SQLcommand class then why use a backgroundworker (which I assume uses more resources and is a bit overkill for running a few simple SQL commands)
    Last edited by chris128; Jul 21st, 2008 at 04:02 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] SQLcommand Async methods worth using?

    Anyone else?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] SQLcommand Async methods worth using?

    Well, for one thing, a background worker is a much simpler option for people who aren't too familiar/good with the concepts of multithreading.

    One thing about synchronous sql commands is, even if the actual request isn't large, if for any reason you have a connection problem and you haven't manually adjusted your timeout property to a lower value, your UI thread could freeze for up to 20 seconds while it attempts to open the connection.

  6. #6

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] SQLcommand Async methods worth using?

    Sorry i didnt realise there had been another reply on this.

    OK so is the general consensus that I should just use a background worker then instead of these methods? If so, is it a good/bad idea to just have one backgroundworker and write out a load of functions & methods in there, then just pass a different argument to RunWorkerAsync depending on which SQL method I want to run? Or would it be a better idea to have seperate background workers for different SQL queries?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] SQLcommand Async methods worth using?

    I'd use the SqlClient methods. It's very little work at all. I've never done it before and I just wrote the following code in a few minutes. I simply applied my existing knowledge of the .NET asynchronous programming model (which you may not have) and consulted the MSDN documentation for the BeginExecuteReader method for the specifics.
    vb.net Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     Dim connection As New SqlConnection("connection string here")
    3.     Dim command As New SqlCommand("SQL query here", connection)
    4.  
    5.     connection.Open()
    6.     command.BeginExecuteReader(AddressOf LoadData, _
    7.                                command, _
    8.                                CommandBehavior.CloseConnection)
    9. End Sub
    10.  
    11. Private Sub LoadData(ByVal ar As IAsyncResult)
    12.     Dim command As SqlCommand = DirectCast(ar.AsyncState, SqlCommand)
    13.     Dim reader As SqlDataReader = command.EndExecuteReader(ar)
    14.     Dim table As New DataTable
    15.  
    16.     table.Load(reader)
    17.     reader.Close()
    18.     command.Connection.Dispose()
    19.     command.Dispose()
    20.  
    21.     'Bind table here.
    22. End Sub
    There's no asynchronous methods for a SqlDataAdapter, so if you need to use one of those then you'd need to look elsewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] SQLcommand Async methods worth using?

    Nah I dont use the SQLDataAdapter class at all so that shouldnt be a problem. I thought the SQL async methods would probably be the way to go because MS must have made them for a reason... I mean they dont make an async method for everything. Thanks yet again jmc, I'll try convert all my existing methods to the async ones and see how it goes

    Cheers
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width