OleDbCommand.Connection Property

Definition

Gets or sets the OleDbConnection used by this instance of the OleDbCommand.

C#
public System.Data.OleDb.OleDbConnection? Connection { get; set; }
C#
[System.Data.DataSysDescription("DbCommand_Connection")]
public System.Data.OleDb.OleDbConnection Connection { get; set; }
C#
public System.Data.OleDb.OleDbConnection Connection { get; set; }

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.

C#
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;
}

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.

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also