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
 
Back
Top