Asynchronous call of a SQL Server stored procedure in C#

后端 未结 3 925
感动是毒
感动是毒 2020-12-10 03:42

Is it possible to call a SQL Server stored procedure asynchronously via C#?

I have a stored procedure which writes a backup of a specific database

相关标签:
3条回答
  • 2020-12-10 04:11

    Check either the following links:

    • http://support.microsoft.com/kb/315582/en-us
    • http://social.msdn.microsoft.com/Forums/en-US/sqldataaccess/thread/66f45455-c2d1-436d-904c-f904958934a5
    0 讨论(0)
  • 2020-12-10 04:14

    I would have done it in a separate thread anyway, so I would probably go for the BackgroundWorker approach.

    0 讨论(0)
  • 2020-12-10 04:22

    In your SqlCommand you can run commands asynchronously using BeginExecuteNonQuery and EndExecuteNonQuery. The latter will block until its done. However this won't report the progress from the server about how the backup is going - I'd use a marquee progress bar for it.

    To avoid the EndExecuteNonQuery from blocking your UI (depending on how you handle it), you will need a background thread. If you use this then you may as well not use BeginXXX EndXXX methods and do it synchronously on a background thread - the BackgroundWorker is best for this.

    To avoid using a background thread in the UI, instead of blocking on EndXXX you will need to register a callback and handle the resulting event (calling EndXXX in this event handler, but it will return immediately).

    Update: as per a comment, for asynchronous calls into the SQL command/connection stuff, you need to specify as much in the connection string:

    http://www.connectionstrings.com/sql-server-2008

    Server=myServerAddress; Database=myDataBase; Integrated Security=True; Asynchronous Processing=True;
    

    Or in code using the connection string builder:

    builder.AsynchronousProcessing = true;
    
    0 讨论(0)
提交回复
热议问题