I have created a TransactionScope and within the scope various items are created and updated in the database. During this process I make a lot of calls to the database. Originally I opened a SqlConnection in the beginning of the TransactionScope and passed it around to any function that made a DB call then I closed the connection after all the calls are made and before the transaction commits. Is it better to do this or to open and close a connection (using the same connection string) for each call?
I have created a TransactionScope and within the scope various items are created and
Share
If you’re going to make a lot of calls in a row, and it’s easy to pass in an open connection, then yes, reuse the open connection.
This is not incredibly important though. Not as much as it used to be. ADO.NET does a good job of managing this for you. See Connection Pooling. If your code is going to get complicated and weird to facilitate one single, open connection object, it’s not worth it.
(Now, making sure your connection objects (whether reused or not) are disposed of is of course really important, as I bet you already know.)