SqlCommandBuilder Class

Definition

自动生成用于协调通过关联的 SQL Server 数据库对 DataSet 所做更改的单表命令。Automatically generates single-table commands that are used to reconcile changes made to a DataSet with the associated SQL Server database. 无法继承此类。This class cannot be inherited.

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

Examples

下面的示例使用 SqlCommand,以及 SqlDataAdapterSqlConnection从数据源中选择行。The following example uses the SqlCommand, along SqlDataAdapter and SqlConnection, to select rows from a data source. 该示例传递了一个连接字符串、一个查询字符串(即 Transact-sql SELECT 语句)和一个作为数据库表的名称的字符串。The example is passed a connection string, a query string that is a Transact-SQL SELECT statement, and a string that is the name of the database table. 然后,该示例创建一个 SqlCommandBuilderThe example then creates a 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

Remarks

SqlDataAdapter 不会自动生成将对 DataSet 所做的更改与 SQL Server 的关联实例进行协调所需的 Transact-sql 语句。The SqlDataAdapter does not automatically generate the Transact-SQL statements required to reconcile changes made to a DataSet with the associated instance of SQL Server. 但是,如果您设置了 SqlDataAdapterSelectCommand 属性,则可以创建一个 SqlCommandBuilder 对象以自动为单表更新生成 Transact-sql 语句。However, you can create a SqlCommandBuilder object to automatically generate Transact-SQL statements for single-table updates if you set the SelectCommand property of the SqlDataAdapter. 然后,SqlCommandBuilder会生成未设置的任何其他 Transact-sql 语句。Then, any additional Transact-SQL statements that you do not set are generated by the SqlCommandBuilder.

只要设置 DataAdapter 属性,SqlCommandBuilder 就会将自身注册为 RowUpdating 事件的侦听器。The SqlCommandBuilder registers itself as a listener for RowUpdating events whenever you set the DataAdapter property. 一次只能将一个 SqlDataAdapterSqlCommandBuilder 对象彼此关联。You can only associate one SqlDataAdapter or SqlCommandBuilder object with each other at one time.

若要生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 使用 SelectCommand 属性自动检索所需的元数据集。To generate INSERT, UPDATE, or DELETE statements, the SqlCommandBuilder uses the SelectCommand property to retrieve a required set of metadata automatically. 如果在检索元数据后更改 SelectCommand (例如,在第一次更新之后),则应调用 RefreshSchema 方法来更新元数据。If you change the SelectCommand after the metadata has been retrieved, such as after the first update, you should call the RefreshSchema method to update the metadata.

SelectCommand 还必须至少返回一个主键或唯一列。The SelectCommand must also return at least one primary key or unique column. 如果没有,则生成InvalidOperation异常,且不生成命令。If none are present, an InvalidOperation exception is generated, and the commands are not generated.

SqlCommandBuilder 还使用 SelectCommand引用的 ConnectionCommandTimeoutTransaction 属性。The SqlCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by the SelectCommand. 如果修改了其中一个或多个属性,则用户应调用 RefreshSchema; 如果 SelectCommand 本身已被替换,则为。The user should call RefreshSchema if one or more of these properties are modified, or if the SelectCommand itself is replaced. 否则,InsertCommandUpdateCommandDeleteCommand 属性保留其以前的值。Otherwise the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.

如果调用 Dispose,则 SqlCommandBuilder 会与 SqlDataAdapter解除关联,不再使用生成的命令。If you call Dispose, the SqlCommandBuilder is disassociated from the SqlDataAdapter, and the generated commands are no longer used.

Constructors

SqlCommandBuilder()

初始化 SqlCommandBuilder 类的新实例。Initializes a new instance of the SqlCommandBuilder class.

SqlCommandBuilder(SqlDataAdapter)

使用关联的 SqlCommandBuilder 对象初始化 SqlDataAdapter 类的新实例。Initializes a new instance of the SqlCommandBuilder class with the associated SqlDataAdapter object.

Properties

CanRaiseEvents

获取一个指示组件是否可以引发事件的值。Gets a value indicating whether the component can raise an event.

(Inherited from Component)
CatalogLocation

获取或设置 SqlCommandBuilder 类的实例的 CatalogLocationGets or sets the CatalogLocation for an instance of the SqlCommandBuilder class.

CatalogSeparator

获取或设置一个字符串,该字符串用作 SqlCommandBuilder 类的实例的目录分隔符。Gets or sets a string used as the catalog separator for an instance of the SqlCommandBuilder class.

ConflictOption

指定哪个 ConflictOption 将由 DbCommandBuilder 使用。Specifies which ConflictOption is to be used by the DbCommandBuilder.

(Inherited from DbCommandBuilder)
Container

获取包含 IContainerComponentGets the IContainer that contains the Component.

(Inherited from Component)
DataAdapter

获取或设置自动为其生成 Transact-SQL 语句的一个 SqlDataAdapter 对象。Gets or sets a SqlDataAdapter object for which Transact-SQL statements are automatically generated.

DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(Inherited from Component)
Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(Inherited from Component)
QuotePrefix

获取或设置在指定其名称中包含空格或保留标记等字符的 SQL Server 数据库对象(如表或列)时使用的一个或多个起始字符。Gets or sets the starting character or characters to use when specifying SQL Server database objects, such as tables or columns, whose names contain characters such as spaces or reserved tokens.

QuoteSuffix

获取或设置在指定其名称中包含空格或保留标记等字符的 SQL Server 数据库对象(如表或列)时使用的一个或多个结束字符。Gets or sets the ending character or characters to use when specifying SQL Server database objects, such as tables or columns, whose names contain characters such as spaces or reserved tokens.

SchemaSeparator

获取或设置要用作架构标识符和任何其他标识符之间的分隔符的字符。Gets or sets the character to be used for the separator between the schema identifier and any other identifiers.

SetAllValues

指定 update 语句中是包含所有列值还是仅包含更改的列值。Specifies whether all column values in an update statement are included or only changed ones.

(Inherited from DbCommandBuilder)
Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(Inherited from Component)

Methods

ApplyParameterInfo(DbParameter, DataRow, StatementType, Boolean)

允许 DbCommandBuilder 类的提供程序实现处理附加参数属性。Allows the provider implementation of the DbCommandBuilder class to handle additional parameter properties.

(Inherited from DbCommandBuilder)
CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(Inherited from MarshalByRefObject)
DeriveParameters(SqlCommand)

从在 SqlCommand 中指定的存储过程中检索参数信息并填充指定的 Parameters 对象的 SqlCommand 集合。Retrieves parameter information from the stored procedure specified in the SqlCommand and populates the Parameters collection of the specified SqlCommand object.

Dispose()

释放 Component 使用的所有资源。Releases all resources used by the Component.

(Inherited from Component)
Dispose(Boolean)

释放 DbCommandBuilder 使用的非托管资源并选择性地释放托管资源。Releases the unmanaged resources used by the DbCommandBuilder and optionally releases the managed resources.

(Inherited from DbCommandBuilder)
Dispose(Boolean)

释放 Component 使用的非托管资源,并选择性地释放托管资源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(Inherited from Component)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetDeleteCommand()

获取自动生成的、对数据库执行删除操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object required to perform deletions on the database.

GetDeleteCommand(Boolean)

获取自动生成的、对数据库执行删除操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object that is required to perform deletions on the database.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetInsertCommand()

获取自动生成的、对数据库执行插入操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object required to perform insertions on the database.

GetInsertCommand(Boolean)

获取自动生成的、对数据库执行插入操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object that is required to perform insertions on the database.

GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetParameterName(Int32)

@p# 格式返回指定参数的名称。Returns the name of the specified parameter in the format of @p#. 在生成自定义命令生成器时使用。Use when building a custom command builder.

(Inherited from DbCommandBuilder)
GetParameterName(String)

给定部分参数名,返回完整参数名。Returns the full parameter name, given the partial parameter name.

(Inherited from DbCommandBuilder)
GetParameterPlaceholder(Int32)

返回该参数在关联的 SQL 语句中的占位符。Returns the placeholder for the parameter in the associated SQL statement.

(Inherited from DbCommandBuilder)
GetSchemaTable(DbCommand)

返回 DbCommandBuilder 的架构表。Returns the schema table for the DbCommandBuilder.

(Inherited from DbCommandBuilder)
GetService(Type)

返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。Returns an object that represents a service provided by the Component or by its Container.

(Inherited from Component)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
GetUpdateCommand()

获取自动生成的、对数据库执行更新操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object required to perform updates on the database.

GetUpdateCommand(Boolean)

获取自动生成的、对数据库执行更新操作所需的 SqlCommand 对象。Gets the automatically generated SqlCommand object required to perform updates on the database.

InitializeCommand(DbCommand)

重置 CommandTimeout 上的 TransactionCommandTypeUpdateRowSourceDbCommand 属性。Resets the CommandTimeout, Transaction, CommandType, and UpdateRowSource properties on the DbCommand.

(Inherited from DbCommandBuilder)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
QuoteIdentifier(String)

给定了具有正确的目录大小写设置且不带引号的标识符时,返回该标识符的正确的带引号形式。Given an unquoted identifier in the correct catalog case, returns the correct quoted form of that identifier. 这包括正确地转义该标识符中的任何嵌入引号。This includes correctly escaping any embedded quotes in the identifier.

RefreshSchema()

清除与此命令生成器关联的命令。Clears the commands that are associated with this command builder.

RefreshSchema()

清除与此 DbCommandBuilder 关联的命令。Clears the commands associated with this DbCommandBuilder.

(Inherited from DbCommandBuilder)
RowUpdatingHandler(RowUpdatingEventArgs)

添加 RowUpdating 事件的事件处理程序。Adds an event handler for the RowUpdating event.

(Inherited from DbCommandBuilder)
SetRowUpdatingHandler(DbDataAdapter)

注册 DbCommandBuilder 以处理 RowUpdatingDbDataAdapter 事件。Registers the DbCommandBuilder to handle the RowUpdating event for a DbDataAdapter.

(Inherited from DbCommandBuilder)
ToString()

返回包含 Component 的名称的 String(如果有)。Returns a String containing the name of the Component, if any. 不应重写此方法。This method should not be overridden.

(Inherited from Component)
UnquoteIdentifier(String)

给定了带引号的标识符时,返回该标识符的正确的不带引号的形式。Given a quoted identifier, returns the correct unquoted form of that identifier. 这包括正确地非转义该标识符的所有嵌入的引号。This includes correctly unescaping any embedded quotes in the identifier.

Events

Disposed

在通过调用 Dispose() 方法释放组件时发生。Occurs when the component is disposed by a call to the Dispose() method.

(Inherited from Component)

Applies to

See also