OleDbCommand.Connection 属性

定义

获取或设置 OleDbCommand 的此实例使用的 OleDbConnection

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

属性值

到数据源的连接。 默认值是 null

属性

例外

Connection 属性在事务执行过程中发生了更改。

示例

以下示例创建 并 OleDbCommand 设置其一些属性。

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

注解

如果当前连接正在执行执行或提取操作,CommandType则无法设置 Connection、 和 CommandText 属性。

如果在事务执行过程中设置了 Connection,并且 Transaction 属性不为 null,则会生成 InvalidOperationException。 如果 Transaction 属性不为 null,而事务已提交或回滚,则会将 Transaction 设置为 null。

适用于

另请参阅