OleDbCommand.Connection OleDbCommand.Connection OleDbCommand.Connection OleDbCommand.Connection Property

定义

获取或设置 OleDbCommand 的此实例使用的 OleDbConnectionGets 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); };
[System.Data.DataSysDescription("DbCommand_Connection")]
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

注解

如果当前连接正在Connection执行CommandType执行CommandText或提取操作,则不能设置、和属性。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.

适用于

另请参阅