OleDbCommand.Connection Property
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets or sets the OleDbConnection used by this instance of the OleDbCommand.
public:
property System::Data::OleDb::OleDbConnection ^ Connection { System::Data::OleDb::OleDbConnection ^ get(); void set(System::Data::OleDb::OleDbConnection ^ value); };
public System.Data.OleDb.OleDbConnection? Connection { get; set; }
[System.Data.DataSysDescription("DbCommand_Connection")]
public System.Data.OleDb.OleDbConnection Connection { get; set; }
public System.Data.OleDb.OleDbConnection Connection { get; set; }
member this.Connection : System.Data.OleDb.OleDbConnection with get, set
[<System.Data.DataSysDescription("DbCommand_Connection")>]
member this.Connection : System.Data.OleDb.OleDbConnection with get, set
Public Property Connection As OleDbConnection
Property Value
The connection to a data source. The default value is null
.
- Attributes
Exceptions
The Connection property was changed while a transaction was in progress.
Examples
The following example creates an OleDbCommand and sets some of its properties.
public void CreateOleDbCommand()
{
string queryString = "SELECT * FROM Categories ORDER BY CategoryID";
OleDbCommand command = new OleDbCommand(queryString);
command.Connection = new OleDbConnection
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NWIND_RW.MDB");
command.CommandTimeout = 20;
}
Public Sub CreateOleDbCommand()
Dim queryString As String = _
"SELECT * FROM Categories ORDER BY CategoryID"
Dim command As New OleDbCommand(queryString)
command.Connection = New OleDbConnection _
("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=NWIND_RW.MDB")
command.CommandTimeout = 20
End Sub
Remarks
You cannot set the Connection, CommandType and CommandText properties if the current connection is performing an execute or fetch operation.
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.