Nicholas Mordecai
Nicholas Mordecai

Reputation: 879

C# SQL Server Coonnectivity - Constant Connection (client - server

I have written many desktop applications and all have gone great using a mysql connection to a database and used sql to query the database. I now want to start a larger project and it feels "wrong" to make many many database connections when I could connect to the server as client - server relationship and just query without having to keep opening and closing connections.

I have done a fair bit of digging around on google but to o avail. I think it's a case of I know what want to search, but not what to search for.

Any gentle nudge in the right direction would be greatly appreciated!

Upvotes: 0

Views: 190

Answers (1)

Ravendarksky
Ravendarksky

Reputation: 633

Generally it is accepted best practice to open a database connection, perform some actions and then close the connection.

It is best not to worry about the efficiencies of using lots of connections in this fashion. SqlServer deals with this quite nicely using connection pooling http://msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx

If you decide to keep connections open throughout the use of your application you run the risk of having lots of idle connections sitting around which is more "wrong" than opening and closing.

Obviously there are exceptions to these rules (such as if you find yourself opening 100s of connections in very short succession)... but this is general advice.

Upvotes: 2

Related Questions