I would appreciate some advice on the best method of managing multiple SQL connections .net (VB)
My application connects to four instances of SQL Server (2000, 2005, 2008, 2008R2) restoring databases, modifying data and taking backups.
In addition to managing databases the app also has its own DB (on the 2008 instance) for storing various data.
I'm using two methods to connect to the instances, SMO (For backups, restoring and retrieving information about the databases, size etc) and SqlClient for querying each database for many tasks such as updating fields and executing sql scripts.I have multiple forms for performing various tasks so there are many places I’m opening\closing connections.
Would it be best to maintain a persistent global connection to each instance from the application? (Two in total, one for SMO and the other for SqlClient) or open close a connection for each task I’m performing?) I’m not using LINK as I need the tasks to be as fast as possible.
The connection string will differ on each connection (Based on Instance and Database being connected to) how would I manage this? A string in the config file for the applications database would make sense, as that would be static but the databases being worked on will vary with each connection.
Currently I'm creating a connection for each instance within the main form:
Dim 2000Connection = New SqlConnection("Data Source=" & SQLServerName & _
"\SQL2000;Initial Catalog=Master;User ID=sa;Password=P@ssw0rd;Pooling=True;")
Dim 2005Connection = New SqlConnection("Data Source=" & SQLServerName & _
"\SQL2005;Initial Catalog=Master;User ID=sa;Password=P@ssw0rd;Pooling=True;")
Dim 2008Connection = New SqlConnection("Data Source=" & SQLServerName & _
"\SQL2008;Initial Catalog=Master;User ID=sa;Password=P@ssw0rd;Pooling=True;")
etc
Then using "USE [" & DatabaseName & "]..." to change to the required database to saving creati开发者_开发百科ng a new connection string.
I'm using this Class to connect:
Public Class ClassDatabaseConnection
Private Shared connection As SqlClient.SqlConnection
Public Shared ReadOnly Property Instance As SqlClient.SqlConnection
Get
If connection Is Nothing Then
connection = New SqlConnection("Static Connection String;")
End If
Return connection
End Get
End Property
Public Shared Customconnection As SqlClient.SqlConnection
Public Shared Property CustomInstance(Optional ByVal Server As String = "", Optional ByVal Database As String = "Master") As SqlClient.SqlConnection
Get
If Customconnection Is Nothing Then
Customconnection = New SqlConnection("Data Source=" & Server & ";User ID=sa;Password=P@ssw0rd;")
End If
Return Customconnection
End Get
Set(ByVal value As SqlClient.SqlConnection)
End Set
End Property
End Class
My recommendation would be as follows.
- Setup each of the connection strings in the application configuration file, but without pointing to a specific database.
- Instantiate and open/close connections as needed, the connection pooling will allow the connections to be reused when possible
This way has a few key benefits.
- It is easy to maintain/update the connections if you need to
- You utilize connection pooling and the underlying system to manage the connections and focus on opening/closing your connection as needed to prevent resource leaks and/or code that is hard to debug
You can continue to then use your various SQL statements to switch between the individual databases being used with your SQL statements.
精彩评论