OleDbCommand.Connection 属性


获取或设置 OleDbConnection 的此实例使用的 OleDbCommandGets or sets the OleDbConnection used by this instance of the OleDbCommand.

 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; }
member this.Connection : System.Data.OleDb.OleDbConnection with get, set
Public Property Connection As OleDbConnection


到数据源的连接。The connection to a data source. 默认值为 nullThe default value is null.



Connection 属性在事务执行过程中发生了更改。The Connection property was changed while a transaction was in progress.


下面的示例创建一个 OleDbCommand 并设置其一些属性。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


如果当前连接正在执行执行或提取操作,则不能设置 ConnectionCommandTypeCommandText 属性。You cannot set the Connection, CommandType and CommandText properties if the current connection is performing an execute or fetch operation.

如果在事务执行过程中设置了 Connection,并且 Transaction 属性不为 null,则会生成 InvalidOperationExceptionIf you set Connection while a transaction is in progress and the Transaction property is not null, an InvalidOperationException is generated. 如果 Transaction 属性不为 null,而事务已提交或回滚,则会将 Transaction 设置为 null。If the Transaction property is not null and the transaction has already been committed or rolled back, Transaction is set to null.