OdbcCommand 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示要對資料來源執行的 SQL 陳述式或預存程序。 此類別無法獲得繼承。
public ref class OdbcCommand sealed : System::Data::Common::DbCommand, ICloneable
public ref class OdbcCommand sealed : System::ComponentModel::Component, ICloneable, IDisposable, System::Data::IDbCommand
public sealed class OdbcCommand : System.Data.Common.DbCommand, ICloneable
public sealed class OdbcCommand : System.ComponentModel.Component, ICloneable, IDisposable, System.Data.IDbCommand
type OdbcCommand = class
inherit DbCommand
interface ICloneable
type OdbcCommand = class
inherit Component
interface ICloneable
interface IDbCommand
interface IDisposable
Public NotInheritable Class OdbcCommand
Inherits DbCommand
Implements ICloneable
Public NotInheritable Class OdbcCommand
Inherits Component
Implements ICloneable, IDbCommand, IDisposable
- 繼承
- 繼承
- 實作
範例
下列範例會使用 ExecuteNonQuery。
public void InsertRow(string connectionString, string insertSQL)
{
using (OdbcConnection connection =
new OdbcConnection(connectionString))
{
// The insertSQL string contains a SQL statement that
// inserts a new row in the source table.
OdbcCommand command = new OdbcCommand(insertSQL, connection);
// Open the connection and execute the insert command.
try
{
connection.Open();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
// The connection is automatically closed when the
// code exits the using block.
}
Public Sub InsertRow(ByVal connectionString As String, _
ByVal insertSQL As String)
Using connection As New OdbcConnection(connectionString)
' The insertSQL string contains a SQL statement that
' inserts a new row in the source table.
Dim command As New OdbcCommand(insertSQL, connection)
' Open the connection and execute the insert command.
Try
connection.Open()
command.ExecuteNonQuery()
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
' The connection is automatically closed when the
' code exits the Using block.
End Using
End Sub
備註
類別 OdbcCommand 提供下列方法,以針對數據源執行命令:
項目 | 描述 |
---|---|
ExecuteReader | 執行傳回數據列的命令。 |
ExecuteNonQuery | 執行 SQL INSERT、DELETE、UPDATE 和 SET 語句等命令。 |
ExecuteScalar | 從資料庫擷取單一值,例如匯總值。 |
您可以重設 CommandText 屬性並重複使用 OdbcCommand 物件。 不過,您必須先關閉 , OdbcDataReader 才能執行新的或先前的命令。
如果執行命令會造成嚴重OdbcException性,例如 SQL Server 嚴重性層級 20 或以上,OdbcConnection可能會關閉。 但是,使用者可以再次開啟連線,然後繼續進行。
建構函式
OdbcCommand() |
初始化 OdbcCommand 類別的新執行個體。 |
OdbcCommand(String) |
使用查詢的文字,初始化 OdbcCommand 類別的新執行個體。 |
OdbcCommand(String, OdbcConnection) |
使用查詢文字和 OdbcCommand 物件來初始化 OdbcConnection 類別的新執行個體。 |
OdbcCommand(String, OdbcConnection, OdbcTransaction) |
使用查詢的文字、OdbcConnection 物件和 Transaction,初始化 OdbcCommand 類別的新執行個體。 |
屬性
CanRaiseEvents |
取得值,指出元件是否能引發事件。 (繼承來源 Component) |
CommandText |
取得或設定 SQL 陳述式或預存程序以對資料來源執行。 |
CommandTimeout |
取得或設定結束執行命令的嘗試並產生錯誤之前的等待時間 (以秒為單位)。 |
CommandType |
取得或設定值,這個值表示如何解譯 CommandText 屬性。 |
Connection |
取得或設定 OdbcCommand 的這個執行個體所使用的 OdbcConnection。 |
Container |
取得包含 IContainer 的 Component。 (繼承來源 Component) |
DbConnection |
取得或設定這個 DbConnection 所使用的 DbCommand。 (繼承來源 DbCommand) |
DbParameterCollection |
取得 DbParameter 物件的集合。 (繼承來源 DbCommand) |
DbTransaction |
取得或設定 DbTransaction,此 DbCommand 物件會在其中執行。 (繼承來源 DbCommand) |
DesignMode |
取得值,指出 Component 目前是否處於設計模式。 (繼承來源 Component) |
DesignTimeVisible |
取得或設定值,指出命令物件是否應該在自訂介面控制項中顯示。 |
Events |
取得附加在這個 Component 上的事件處理常式清單。 (繼承來源 Component) |
Parameters | |
Site | (繼承來源 Component) |
Transaction |
取得或設定在其中執行 OdbcTransaction 的 OdbcCommand。 |
UpdatedRowSource |
取得或設定值,指定 Update 方法應該如何將命令結果套用至 DataRow。 |
方法
事件
Disposed |
當 Dispose() 方法的呼叫處置元件時,就會發生。 (繼承來源 Component) |
明確介面實作
ICloneable.Clone() |
如需這個成員的說明,請參閱 Clone()。 |
IDbCommand.Connection |
取得或設定 IDbCommand 的這個執行個體所使用的 IDbConnection。 (繼承來源 DbCommand) |
IDbCommand.CreateParameter() |
建立 IDbDataParameter 物件的新執行個體。 |
IDbCommand.CreateParameter() |
建立 IDbDataParameter 物件的新執行個體。 (繼承來源 DbCommand) |
IDbCommand.ExecuteReader() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 針對 Connection 執行 CommandText,並建置 IDataReader。 |
IDbCommand.ExecuteReader() |
針對 Connection 執行 CommandText,並建置 IDataReader。 (繼承來源 DbCommand) |
IDbCommand.ExecuteReader(CommandBehavior) |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 針對 Connection 執行 CommandText,並使用指定行為來建置 IDataReader。 |
IDbCommand.ExecuteReader(CommandBehavior) |
針對 Connection 執行 CommandText,並使用其中一個 CommandBehavior 值來建置 IDataReader。 (繼承來源 DbCommand) |
IDbCommand.Parameters | (繼承來源 DbCommand) |
IDbCommand.Transaction |
取得或設定 DbTransaction,此 DbCommand 物件會在其中執行。 (繼承來源 DbCommand) |
擴充方法
ConfigureAwait(IAsyncDisposable, Boolean) |
設定如何執行從非同步可處置項目傳回的工作 await。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應