asp.net.ph

SqlConnection Class

System.Data.SqlClient Namespace


Represents an open connection to an SQL Server™ database. This class cannot be inherited.

SqlConnection Class Members

Collapse   Constructors

Visibility Constructor Parameters
public SqlConnection ( String connectionString )
public SqlConnection ( String connectionString , SqlCredential credential )
public SqlConnection ( )

Collapse   Properties

Visibility Name Value Type Accessibility
public AccessToken String [ Get , Set ]
public ClientConnectionId Guid [ Get ]
public static ColumnEncryptionKeyCacheTtl TimeSpan [ Get , Set ]
public static ColumnEncryptionQueryMetadataCacheEnabled Boolean [ Get , Set ]
public static ColumnEncryptionTrustedMasterKeyPaths IDictionary`2 [ Get ]
public ConnectionString String [ Get , Set ]
public ConnectionTimeout Int32 [ Get ]
public Credential SqlCredential [ Get , Set ]
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 ]

Collapse   Methods

Visibility Name Parameters Return Type
protected BeginDbTransaction ( IsolationLevel isolationLevel ) DbTransaction
public BeginTransaction ( String transactionName ) SqlTransaction
public BeginTransaction ( IsolationLevel iso ) SqlTransaction
public BeginTransaction ( IsolationLevel iso , String transactionName ) SqlTransaction
public BeginTransaction ( ) SqlTransaction
public ChangeDatabase ( String database ) Void
public static ChangePassword ( String connectionString , SqlCredential credential , SecureString newSecurePassword ) Void
public static ChangePassword ( String connectionString , String newPassword ) Void
public static ClearAllPools ( ) Void
public static ClearPool ( SqlConnection connection ) Void
public Close ( ) Void
public CreateCommand ( ) SqlCommand
protected CreateDbCommand ( ) DbCommand
protected Dispose ( Boolean disposing ) Void
public EnlistDistributedTransaction ( ITransaction transaction ) Void
public EnlistTransaction ( Transaction transaction ) Void
public GetSchema ( ) DataTable
public GetSchema ( String collectionName ) DataTable
public GetSchema ( String collectionName , String restrictionValues ) DataTable
public Open ( ) Void
public OpenAsync ( CancellationToken cancellationToken ) Task
public static RegisterColumnEncryptionKeyStoreProviders ( IDictionary`2 customProviders ) Void
public ResetStatistics ( ) Void
public RetrieveStatistics ( ) IDictionary

Collapse   Events

Multicast Name Type
multicast InfoMessage SqlInfoMessageEventHandler

Remarks

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.

Example

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

See Also

SqlDataAdapter   SqlCommand Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 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 asp.net.ph