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
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
type OleDbCommand = class
inherit DbCommand
interface ICloneable
interface IDbCommand
interface IDisposable
type OleDbCommand = class
inherit Component
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 才能執行新的或先前的命令。
例如,如果嚴重 OleDbException (,SQL Server 嚴重性層級為 20 或更新) 是由執行 OleDbCommand的 方法所產生,則OleDbConnection連接可能會關閉。 但是,使用者可以再次開啟連線,然後繼續進行。
建構函式
OleDbCommand() |
初始化 OleDbCommand 類別的新執行個體。 |
OleDbCommand(String) |
使用查詢的文字,初始化 OleDbCommand 類別的新執行個體。 |
OleDbCommand(String, OleDbConnection) |
使用查詢的文字和 OleDbConnection 初始化 OleDbCommand 類別的新執行個體。 |
OleDbCommand(String, OleDbConnection, OleDbTransaction) |
使用查詢的文字、OleDbConnection 和 Transaction,初始化 OleDbCommand 類別的新執行個體。 |
屬性
CanRaiseEvents |
取得值,指出元件是否能引發事件。 (繼承來源 Component) |
CommandText |
取得或設定要在資料來源執行的 SQL 陳述式或預存程序。 |
CommandTimeout |
取得或設定結束執行命令的嘗試並產生錯誤之前的等待時間 (以秒為單位)。 |
CommandType |
取得或設定值,這個值表示如何解譯 CommandText 屬性。 |
Connection |
取得或設定 OleDbCommand 的這個執行個體所使用的 OleDbConnection。 |
Container |
取得包含 IContainer 的 Component。 (繼承來源 Component) |
DbConnection |
取得或設定這個 DbConnection 所使用的 DbCommand。 (繼承來源 DbCommand) |
DbParameterCollection |
取得 DbParameter 物件的集合。 (繼承來源 DbCommand) |
DbTransaction |
取得或設定 DbTransaction,此 DbCommand 物件會在其中執行。 (繼承來源 DbCommand) |
DesignMode |
取得值,指出 Component 目前是否處於設計模式。 (繼承來源 Component) |
DesignTimeVisible |
取得或設定值,指出在命令物件是否應該顯示在自訂 Windows Form 設計工具控制項中。 |
Events |
取得附加在這個 Component 上的事件處理常式清單。 (繼承來源 Component) |
Parameters | |
Site | (繼承來源 Component) |
Transaction |
取得或設定在其中執行 OleDbTransaction 的 OleDbCommand。 |
UpdatedRowSource |
取得或設定當由 DataRow 的 |
方法
事件
Disposed |
當 Dispose() 方法的呼叫處置元件時,就會發生。 (繼承來源 Component) |
明確介面實作
ICloneable.Clone() |
如需這個成員的說明,請參閱 Clone()。 |
IDbCommand.Connection |
取得或設定 IDbCommand 的這個執行個體所使用的 IDbConnection。 (繼承來源 DbCommand) |
IDbCommand.CreateParameter() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 建立 IDbDataParameter 物件的新執行個體。 |
IDbCommand.CreateParameter() |
建立 IDbDataParameter 物件的新執行個體。 (繼承來源 DbCommand) |
IDbCommand.ExecuteReader() |
如需這個成員的說明,請參閱 ExecuteReader()。 |
IDbCommand.ExecuteReader(CommandBehavior) |
針對 Connection 執行 CommandText,並使用其中一個 CommandBehavior 值來建置 IDataReader。 |
IDbCommand.Parameters | (繼承來源 DbCommand) |
IDbCommand.Transaction |
取得或設定 DbTransaction,此 DbCommand 物件會在其中執行。 (繼承來源 DbCommand) |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應