共用方式為


SqlCommandBuilder 類別

定義

自動產生單一資料表指令,用於將對 的 DataSet 變更與相關 SQL Server 資料庫進行調和。 無法繼承這個類別。

public ref class SqlCommandBuilder sealed : System::ComponentModel::Component
public ref class SqlCommandBuilder sealed : System::Data::Common::DbCommandBuilder
public sealed class SqlCommandBuilder : System.ComponentModel.Component
public sealed class SqlCommandBuilder : System.Data.Common.DbCommandBuilder
type SqlCommandBuilder = class
    inherit Component
type SqlCommandBuilder = class
    inherit DbCommandBuilder
Public NotInheritable Class SqlCommandBuilder
Inherits Component
Public NotInheritable Class SqlCommandBuilder
Inherits DbCommandBuilder
繼承
SqlCommandBuilder
繼承

範例

以下範例使用 SqlCommand沿 SqlDataAdapterSqlConnection、 和 的參數從資料來源中選取列。 範例中會傳遞一個連接字串、一個查詢字串(Transact-SQL SELECT 語句)以及一個字串作為資料庫資料表名稱。 範例接著產生一個 SqlCommandBuilder

public static DataSet SelectSqlRows(string connectionString,
    string queryString, string tableName)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(queryString, connection);
        SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

        connection.Open();

        DataSet dataSet = new DataSet();
        adapter.Fill(dataSet, tableName);

        //code to modify data in DataSet here

        builder.GetUpdateCommand();

        //Without the SqlCommandBuilder this line would fail
        adapter.Update(dataSet, tableName);

        return dataSet;
    }
}
Private Function SelectSqlRows(ByVal connectionString As String, _
    ByVal queryString As String, ByVal tableName As String) As DataSet

    Using connection As New SqlConnection(connectionString)

        Dim adapter As New SqlDataAdapter()
        adapter.SelectCommand = New SqlCommand(queryString, connection)
        Dim builder As New SqlCommandBuilder(adapter)

        connection.Open()

        Dim dataSet As New DataSet()
        adapter.Fill(dataSet, tableName)

        ' Code to modify data in DataSet here 

        builder.GetUpdateCommand()

        ' Without the SqlCommandBuilder this line would fail.
        adapter.Update(dataSet, tableName)

        Return dataSet
    End Using
End Function

備註

SqlDataAdapter 不會自動產生必要的 Transact-SQL 語句,以將對 a DataSet 所做的變更與對應的 SQL Server 實例進行調和。 不過,如果你設定 SelectCommand 的屬性SqlDataAdapter,可以建立SqlCommandBuilder一個物件,自動產生單表更新的 Transact-SQL 語句。 接著,任何你未設定的額外 Transact-SQL 語句會由 SqlCommandBuilder產生。

當你設定DataAdapter屬性時,它SqlCommandBuilder會自動註冊為事件的監聽者RowUpdating。 你一次只能將其中一個 SqlDataAdapterSqlCommandBuilder 物件彼此關聯起來。

為了產生 INSERT、UPDATE 或 DELETE 語句,會 SqlCommandBuilder 使用 該 SelectCommand 屬性自動取得所需的元資料集合。 如果你在元資料取得後才更改 , SelectCommand 例如第一次更新後,你應該呼叫 RefreshSchema 該方法來更新元資料。

SelectCommand 還必須傳回至少一個主索引鍵或唯一的資料行。 若不存在,則會產生 InvalidOperation 例外,指令不會被產生。

SqlCommandBuilder 使用 Connection了 、 CommandTimeout以及 TransactionSelectCommand參考的屬性。 如果這些屬性中有一個或多個被修改,或SelectCommand是本身被替換,使用者應該呼叫RefreshSchema。 否則 InsertCommandUpdateCommand、 和 DeleteCommand 屬性將保留先前的值。

如果你呼叫 Dispose,則 SqlCommandBuilder 與 分離 SqlDataAdapter,產生的指令不再使用。

建構函式

名稱 Description
SqlCommandBuilder()

初始化 SqlCommandBuilder 類別的新執行個體。

SqlCommandBuilder(SqlDataAdapter)

初始化一個帶有相關SqlDataAdapter物件的新類別實例SqlCommandBuilder

屬性

名稱 Description
CanRaiseEvents

會得到一個值,表示該元件是否能引發事件。

(繼承來源 Component)
CatalogLocation

取得或設定 CatalogLocation 該類別的實例 SqlCommandBuilder

CatalogSeparator

取得或設定一個字串,作為類別實例 SqlCommandBuilder 的目錄分隔符。

ConflictOption

指定 ConflictOption 要被 使用的 DbCommandBuilder

(繼承來源 DbCommandBuilder)
Container

得到 IContainer 包含 Component的 。

(繼承來源 Component)
DataAdapter

取得或設定一個 SqlDataAdapter 物件,該物件會自動產生 Transact-SQL 語句。

DesignMode

會得到一個值,表示目前 Component 是否處於設計模式。

(繼承來源 Component)
Events

會取得與此 Component連結的事件處理程序清單。

(繼承來源 Component)
QuotePrefix

在指定 SQL Server 資料庫物件(如資料表或欄位)時,取得或設定起始字元,這些物件名稱包含空格或保留標記等字元。

QuoteSuffix

在指定 SQL Server 資料庫物件(如表格或欄位)時,取得或設定結尾字元,這些物件名稱包含空格或保留標記等字元。

SchemaSeparator

取得或設定用於結構識別碼與其他識別碼分隔符的字元。

SetAllValues

指定更新語句中所有欄位值是否包含,或僅包含變更欄位。

(繼承來源 DbCommandBuilder)
Site

取得或設定 ISiteComponent

(繼承來源 Component)

方法

名稱 Description
ApplyParameterInfo(DbParameter, DataRow, StatementType, Boolean)

允許類別的提供者實作 DbCommandBuilder 處理額外的參數屬性。

(繼承來源 DbCommandBuilder)
CreateObjRef(Type)

建立一個物件,包含產生代理伺服器所需的所有相關資訊,用於與遠端物件通訊。

(繼承來源 MarshalByRefObject)
DeriveParameters(SqlCommand)

從指定於 的 SqlCommand 儲存程序中取得參數資訊,並填充 Parameters 指定 SqlCommand 物件的集合。

Dispose()

釋放所有由 Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 未管理的資源, Component 並可選擇性地釋放受管理資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 未管理的資源, DbCommandBuilder 並可選擇性地釋放受管理資源。

(繼承來源 DbCommandBuilder)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetDeleteCommand()

取得自動產生 SqlCommand 的物件,用於對資料庫進行刪除。

GetDeleteCommand(Boolean)

取得自動產生 SqlCommand 的物件,用於對資料庫進行刪除。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetInsertCommand()

取得自動產生 SqlCommand 的物件,用於對資料庫進行插入。

GetInsertCommand(Boolean)

取得自動產生 SqlCommand 的物件,用於對資料庫進行插入。

GetLifetimeService()
已淘汰.

取得目前控制此實例生命週期政策的終身服務物件。

(繼承來源 MarshalByRefObject)
GetParameterName(Int32)

以 格式 @p#回傳指定參數名稱。 用來打造自訂指令建構器。

(繼承來源 DbCommandBuilder)
GetParameterName(String)

回傳完整的參數名稱,給定部分參數名稱。

(繼承來源 DbCommandBuilder)
GetParameterPlaceholder(Int32)

回傳相關 SQL 陳述式中參數的佔位符。

(繼承來源 DbCommandBuilder)
GetSchemaTable(DbCommand)

回傳 的結構表 DbCommandBuilder

(繼承來源 DbCommandBuilder)
GetService(Type)

回傳一個由 或Component其 所提供的Container服務的物件。

(繼承來源 Component)
GetType()

取得目前實例的 Type

(繼承來源 Object)
GetUpdateCommand()

取得自動產生 SqlCommand 的物件,用於對資料庫進行更新。

GetUpdateCommand(Boolean)

取得自動產生 SqlCommand 的物件,用於對資料庫進行更新。

InitializeCommand(DbCommand)

重置 CommandTimeoutTransactionCommandTypeUpdateRowSource 屬性。DbCommand

(繼承來源 DbCommandBuilder)
InitializeLifetimeService()
已淘汰.

取得一個終身服務物件以控制此實例的終身政策。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立一個 MarshalByRefObject 目前物件的淺層複製品。

(繼承來源 MarshalByRefObject)
QuoteIdentifier(String)

給定正確目錄中未引號的識別碼,回傳該識別碼的正確引號形式。 這包括正確跳脫識別碼中嵌入的引號。

RefreshSchema()

清除與此指令建構器相關的指令。

RefreshSchema()

清除與此 DbCommandBuilder相關指令。

(繼承來源 DbCommandBuilder)
RowUpdatingHandler(RowUpdatingEventArgs)

新增事件處理程式。RowUpdating

(繼承來源 DbCommandBuilder)
SetRowUpdatingHandler(DbDataAdapter)

註冊 以DbCommandBuilder處理RowUpdating事件。DbDataAdapter

(繼承來源 DbCommandBuilder)
ToString()

回傳 String 包含 的名稱 Component(若有的話)。 此方法不應被覆蓋。

(繼承來源 Component)
UnquoteIdentifier(String)

給定引號識別碼, 回傳該識別碼的正確無引號形式。 這包括正確解脫識別碼中嵌入的引號。

事件

名稱 Description
Disposed

當元件被呼叫方法 Dispose() 時會發生。

(繼承來源 Component)

適用於

另請參閱