OleDbCommand 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示要針對數據源執行的 SQL 語句或預存程式。
public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable, IDisposable
public ref class OleDbCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public ref class OleDbCommand sealed : System::Data::Common::DbCommand, ICloneable
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("OleDbConnection is not AOT-compatible.")]
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
public sealed class OleDbCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable, IDisposable
public sealed class OleDbCommand : System.Data.Common.DbCommand, ICloneable
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("OleDbConnection is not AOT-compatible.")>]
type OleDbCommand = class
inherit DbCommand
interface IDbCommand
interface IDisposable
interface ICloneable
type OleDbCommand = class
inherit Component
interface ICloneable
interface IDbCommand
interface IDisposable
type OleDbCommand = class
inherit DbCommand
interface ICloneable
interface IDbCommand
interface IDisposable
type OleDbCommand = class
inherit DbCommand
interface IDbCommand
interface IDisposable
interface ICloneable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable, IDisposable
Public NotInheritable Class OleDbCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
Public NotInheritable Class OleDbCommand
Inherits DbCommand
Implements ICloneable
- 繼承
- 繼承
- 繼承
- 屬性
- 實作
範例
以下範例使用 OleDbCommand沿著 OleDbDataAdapterOleDbConnection和 的 ,從 Access 資料庫中選取列。 填充 DataSet 物隨後會被歸還。 範例會傳遞一個初始化的 DataSet、一個連接字串、一個查詢字串(SQL SELECT 語句)以及一個字串(來源資料庫資料表的名稱)。
public void ReadMyData(string connectionString)
{
string queryString = "SELECT OrderID, CustomerID FROM Orders";
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
}
// always call Close when done reading.
reader.Close();
}
}
Public Sub ReadMyData(ByVal connectionString As String)
Dim queryString As String = "SELECT OrderID, CustomerID FROM Orders"
Using connection As New OleDbConnection(connectionString)
Dim command As New OleDbCommand(queryString, connection)
connection.Open()
Dim reader As OleDbDataReader = command.ExecuteReader()
While reader.Read()
Console.WriteLine(reader.GetInt32(0).ToString() + ", " _
+ reader.GetString(1))
End While
' always call Close when done reading.
reader.Close()
End Using
End Sub
備註
當建立 的 OleDbCommand 實例時,讀寫屬性會設定為初始值。 如需這些值的清單,請參閱建構函式 OleDbCommand 。
OleDbCommand 具備以下方法在資料來源執行指令:
| 專案 | 描述 |
|---|---|
| ExecuteReader | 執行傳回數據列的命令。 ExecuteReader 若用於執行像是 SQL SET 語句這類指令,可能無法達到你想要的效果。 |
| ExecuteNonQuery | 執行 SQL INSERT、DELETE、UPDATE 和 SET 語句等命令。 |
| ExecuteScalar | 擷取單一值,例如,從資料庫擷取匯總值。 |
你可以重置屬性 CommandText 並重複使用該 OleDbCommand 物件。 不過,你必須先關閉 , OleDbDataReader 才能執行新的或之前的指令。
若執行 的方法OleDbCommandOleDbConnection產生致命事件OleDbException(例如 SQL Server 嚴重度等級達 20 或以上),則該連線可能會被關閉。 不過,用戶可以重新開啟連線並繼續。
建構函式
| 名稱 | Description |
|---|---|
| OleDbCommand() |
初始化 OleDbCommand 類別的新執行個體。 |
| OleDbCommand(String, OleDbConnection, OleDbTransaction) |
初始化一個新的類別實例 OleDbCommand ,包含查詢文本、一個 OleDbConnection、 和 Transaction。 |
| OleDbCommand(String, OleDbConnection) |
初始化一個新的類別實例 OleDbCommand ,並以查詢文字和一個 OleDbConnection。 |
| OleDbCommand(String) |
初始化一個新的類別實例 OleDbCommand ,並使用查詢的文字。 |
屬性
| 名稱 | Description |
|---|---|
| CanRaiseEvents |
取得值,指出元件是否可以引發事件。 (繼承來源 Component) |
| CommandText |
取得或設定要於數據源執行的 SQL 語句或預存程式。 |
| CommandTimeout |
取得或設定終止嘗試執行命令併產生錯誤之前的等候時間(以秒為單位)。 |
| CommandType |
取得或設定一個值,指示屬性的 CommandText 解讀方式。 |
| Connection |
取得或設定 OleDbConnection 此實例 OleDbCommand所使用的。 |
| Container |
得到 IContainer 包含 Component的 。 (繼承來源 Component) |
| DbConnection |
得到或設定 由 DbConnection 所使用 DbCommand。 (繼承來源 DbCommand) |
| DbParameterCollection |
取得物品集合 DbParameter 。 (繼承來源 DbCommand) |
| DbTransaction |
取得或設定該DbCommand物件執行的內部。DbTransaction (繼承來源 DbCommand) |
| DesignMode |
會得到一個值,表示目前 Component 是否處於設計模式。 (繼承來源 Component) |
| DesignTimeVisible |
取得或設定值,這個值表示命令物件是否應該顯示在自定義的 Windows Forms 設計工具控制件中。 |
| Events |
會取得與此 Component連結的事件處理程序清單。 (繼承來源 Component) |
| Parameters | |
| Site | (繼承來源 Component) |
| Transaction |
取得或設定 OleDbTransaction 執行的內部 OleDbCommand 。 |
| UpdatedRowSource |
取得或設定指令結果在 使用 該方法OleDbDataAdapter時如何應用於 。DataRow |
方法
事件
| 名稱 | Description |
|---|---|
| Disposed |
當元件被呼叫方法 Dispose() 時會發生。 (繼承來源 Component) |
明確介面實作
| 名稱 | Description |
|---|---|
| ICloneable.Clone() |
關於此成員的描述,請參見 Clone()。 |
| IDbCommand.Connection |
取得或設定 IDbConnection 此實例 IDbCommand所使用的。 (繼承來源 DbCommand) |
| IDbCommand.CreateParameter() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 建立一個新的物件實例 IDbDataParameter 。 |
| IDbCommand.CreateParameter() |
建立一個新的物件實例 IDbDataParameter 。 (繼承來源 DbCommand) |
| IDbCommand.ExecuteReader() |
關於此成員的描述,請參見 ExecuteReader()。 |
| IDbCommand.ExecuteReader(CommandBehavior) |
對 執行 ,CommandTextConnection並利用其中一個CommandBehavior值建立 IDataReader 。 |
| IDbCommand.Parameters | (繼承來源 DbCommand) |
| IDbCommand.Transaction |
取得或設定該DbCommand物件執行的內部。DbTransaction (繼承來源 DbCommand) |