共用方式為


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

備註

如果目前的連接正在執行或擷取作業,則無法設定 ConnectionCommandTypeCommandText 屬性。

如果在交易進行時設定 Connection,且 Transaction 屬性不是 null,則會產生 InvalidOperationException。 如果 Transaction 屬性不是 null,且異動已經認可或回復,則 Transaction 會設定為 null。

適用於

另請參閱