Okay to use a single SqlConnection with multiple SqlCommands?

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi,

I have a multithreaded application where multiple threads can
potentially attempt to write to a database simultaneously. Can I
create a single SqlConnection, open it, and use it for all my
SqlCommands? Is it okay for multiple SqlCommands to execute
simultaneously when they are using the same SqlConnection?

THANKS!
John
 
John said:
Hi,

I have a multithreaded application where multiple threads can
potentially attempt to write to a database simultaneously. Can I
create a single SqlConnection, open it, and use it for all my
SqlCommands? Is it okay for multiple SqlCommands to execute
simultaneously when they are using the same SqlConnection?

No. You should create, open and close the SqlConnection each time you need
it. There is a connection pool behind the scenes so this performs fine.

David
 
U¿ytkownik "John said:
Hi,

I have a multithreaded application where multiple threads can
potentially attempt to write to a database simultaneously. Can I
create a single SqlConnection, open it, and use it for all my
SqlCommands? Is it okay for multiple SqlCommands to execute
simultaneously when they are using the same SqlConnection?

No. One connection can be used only by one Command simultaneusly.
Grzegorz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top