See Also: SqlConnection Members
A System.Data.SqlClient.SqlConnection object represents a unique session to a ssNoVersion data source. With a client/server database system, it is equivalent to a network connection to the server. System.Data.SqlClient.SqlConnection is used together with System.Data.SqlClient.SqlDataAdapter and System.Data.SqlClient.SqlCommand to increase performance when connecting to a Microsoft ssNoVersion database. For all third-party ssNoVersion products, and other OLE DB-supported data sources, use System.Data.OleDb.OleDbConnection.
When you create an instance of System.Data.SqlClient.SqlConnection, all properties are set to their initial values. For a list of these values, see the System.Data.SqlClient.SqlConnection constructor.
See SqlConnection.ConnectionString for a list of the keywords in a connection string.
If the System.Data.SqlClient.SqlConnection goes out of scope, it won't be closed. Therefore, you must explicitly close the connection by calling Close or Dispose. Close and Dispose are functionally equivalent. If the connection pooling value Pooling is set to true or yes, the underlying connection is returned back to the connection pool. On the other hand, if Pooling is set to false or no, the underlying connection to the server is actually closed.
Login and logout events will not be raised on the server when a connection is fetched from or returned to the connection pool, because the connection is not actually closed when it is returned to the connection pool. For more information, see SQL Server Connection Pooling (ADO.NET).
To ensure that connections are always closed, open the connection inside of a using block, as shown in the following code fragment. Doing so ensures that the connection is automatically closed when the code exits the block.
Example
Using connection As New SqlConnection(connectionString) connection.Open() ' Do work here; connection closed on following line. End Using
Example
using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); // Do work here; connection closed on following line. }
To deploy high-performance applications, you must use connection pooling. When you use the dnprdnshort Data Provider for ssNoVersion, you do not have to enable connection pooling because the provider manages this automatically, although you can modify some settings. For more information, see SQL Server Connection Pooling (ADO.NET).
If a System.Data.SqlClient.SqlException is generated by the method executing a System.Data.SqlClient.SqlCommand, the System.Data.SqlClient.SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server ordinarily closes the System.Data.SqlClient.SqlConnection. However, the user can reopen the connection and continue.
An application that creates an instance of the System.Data.SqlClient.SqlConnection object can require all direct and indirect callers to have sufficient permission to the code by setting declarative or imperative security demands. System.Data.SqlClient.SqlConnection makes security demands using the System.Data.SqlClient.SqlClientPermission object. Users can verify that their code has sufficient permissions by using the System.Data.SqlClient.SqlClientPermissionAttribute object. Users and administrators can also use the Code Access Security Policy Tool (Caspol.exe) to modify security policy at the machine, user, and enterprise levels. For more information, see Security in the .NET Framework. For an example demonstrating how to use security demands, see Code Access Security and ADO.NET.
For more information about handling warning and informational messages from the server, see Working with Connection Events. ssNoVersion engine errors and error messages are documented in ssNoVersion Books Online.
You can force TCP instead of shared memory. You can do that by prefixing tcp: to the server name in the connection string or you can use localhost.