| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
SqlConnection ClassSystem.Data.SqlClient Namespace Represents an open connection to an SQL Server™ database. This class cannot be inherited.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Visibility | Name | Value Type | Accessibility |
|---|---|---|---|
| public | ConnectionString | String | [ Get , Set ] |
| public | ConnectionTimeout | Int32 | [ Get ] |
| public | Database | String | [ Get ] |
| public | DataSource | String | [ Get ] |
| public | FireInfoMessageEventOnUserErrors | Boolean | [ Get , Set ] |
| public | PacketSize | Int32 | [ Get ] |
| public | ServerVersion | String | [ Get ] |
| public | State | ConnectionState | [ Get ] |
| public | StatisticsEnabled | Boolean | [ Get , Set ] |
| public | WorkstationId | String | [ Get ] |
| Multicast | Name | Type |
|---|---|---|
| multicast | InfoMessage | SqlInfoMessageEventHandler |
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 ( );
}
| ||
| C# | VB | |
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme