asp.net.ph

SqlCommand.Connection Property

System.Data.SqlClient Namespace   SqlCommand Class


Sets or retrieves the SqlConnection used by this instance of the SqlCommand.

Syntax


Script SqlCommand.Connection [ = objConnection ]

Property Value


objConnection An object representing the connection to an SQL Server™ data source.

The property is read/write with no default value.

Exceptions


Exception Type Condition
InvalidOperationException Occurs when the Connection property is changed while a transaction is in progress.

Remarks

If you set Connection while a transaction is in progress and the Transaction property is not null, an InvalidOperationException is generated. If the Transaction property is not null and the transaction has already been committed or rolled back, Transaction is set to null.

Example

The following example illustrates how the Connection with which to execute the command can be set.

void setSqlCommand ( ) {
   string query = "SELECT * FROM Categories ORDER BY CategoryID";
   string connString = 
      "uid=sa; pwd=; database=northwind; server=myServer";
   SqlCommand myCommand = new SqlCommand ( query );
   myCommand.Connection = new SqlConnection ( connString );
}
  C# VB

See Also

SqlCommand Members   CommandText   CommandTimeout   CommandType 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