I am using multiple queries to pull data from the same server in my application. The issue is that I have to open a new connection every time I have a new query.
Is it even possible to:
- Open the connection
- Run query
- Pull results
- Run another query
- Pull another result
- Run final query
- Pull another result
- Close connection.
Although you may not yet know it, you are doing it correctly.
Open the connection, do your query, close it. Preferably using a
usingblock ortry/finally.This may sound like a lot of overhead, but the connection pool in the .NET Framework Data Provider for SQL Server will actually optimize this for you.
In fact closing the connection is recommended. Here is a quote from the documentation:
Here is an example of some code that does this:
For reference:
http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.71).aspx