System.Data.SqlClient Namespace
Represents an open connection to an SQL Server™ database. This class cannot be
inherited.
An SqlConnection object represents a unique session to an SQL Server™ data source. In the case of a client/server database system, it is equivalent to a network connection to the server. SqlConnection is used in conjunction with SqlDataAdapter and SqlCommand to increase performance when connecting to a Microsoft SQL Server™ database. For all third-party SQL Server™ products, as well as other OLE DB-supported data sources, use OleDbConnection.
When you create an instance of SqlConnection, all properties are set to their initial values. For a list of these values, see the SqlConnection constructor.
If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the the connection by calling Close or Dispose.
If an SqlException is generated by the method executing an SqlCommand, the SqlConnection remains open when the severity level is 19 or less. When the severity level is 20 or greater, the server usually closes the SqlConnection. However, the user can reopen the connection and continue.
An application that initializes an instance of the SqlConnection object can require all direct and indirect callers to have adequate permission to the code by setting declarative or imperative security demands. SqlConnection makes security demands using the SqlClientPermission object. Users can verify that their code has adequate permissions by using the SqlClientPermissionAttribute object.
The following example initializes an SqlCommand and an SqlConnection. The SqlConnection is opened and set as the Connection for the SqlCommand. The example then calls ExecuteNonQuery, and closes the connection. To accomplish this, the ExecuteNonQuery is passed a connection string and a query string that is a Transact-SQL INSERT statement.
void InsertRow ( string connString ) {
// if the connection string is null, use a default.
if ( connString == "" ) {
connString = "initial catalog=northwind; data Source=localhost;" +
"Integrated Security=SSPI;";
}
SqlConnection myConn = new SqlConnection ( connString );
string myInsertQuery = "INSERT INTO Customers ( CustomerID, CompanyName )
Values ( 'NWIND', 'Northwind Traders' ) ";
SqlCommand myCommand = new SqlCommand ( myInsertQuery );
myCommand.Connection = myConn;
myConn.Open ( );
myCommand.ExecuteNonQuery ( );
myCommand.Connection.Close ( );
}
Public Sub InsertRow ( connString As String )
' if the connection string is null, use a default.
If connString = "" Then
connString = "initial catalog=northwind; data Source=localhost;" & _
"Integrated Security=SSPI;"
End If
Dim myConn As New SqlConnection ( connString )
Dim myInsertQuery As String = "INSERT INTO Customers ( CustomerID, CompanyName ) _
Values ( 'NWIND', 'Northwind Traders' ) "
Dim myCommand As New SqlCommand ( myInsertQuery )
myCommand.Connection = myConn
myConn.Open ( )
myCommand.ExecuteNonQuery ( )
myCommand.Connection.Close ( )
End Sub 'SelectSqlClientSrvRows |
|
C# |
VB |
SqlDataAdapter SqlCommand