自动生成用来使对 DataSet 所作的更改与相关联的数据库一致的单个表命令。
System.Object
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DbCommandBuilder
IBM.Data.DB2.DB2CommandBuilder
[Visual Basic]
NotInheritable Public Class DB2CommandBuilder
Inherits DbCommandBuilder
[C#]
public sealed class DB2CommandBuilder : DbCommandBuilder
[C++]
public __gc __sealed class DB2CommandBuilder : public DbCommandBuilder
[JScript]
public final class DB2CommandBuilder extends DbCommandBuilder
DB2DataAdapter 不会自动生成协调对与数据库相关联的 DataSet 所作的更改所需的 SQL 语句。但是,可以通过设置 DB2DataAdapter 的 SelectCommand 属性来创建生成 SQL 语句以进行单个表更新的 DB2CommandBuilder 对象。然后,DB2CommandBuilder 将生成您未设置的任何其他 SQL 语句。
DB2DataAdapter 与它相应的 DB2CommandBuilder 之间始终是一一对应关系。要建立此对应关系,应设置 DB2CommandBuilder 对象的 DataAdapter 属性。这会将 DB2CommandBuilder 注册为侦听器,该侦听器将产生会影响 DataSet 的 DB2DataAdapter.RowUpdating 事件的输出。
为了生成 INSERT、UPDATE 或 DELETE 语句,DB2CommandBuilder 使用 SelectCommand 属性,该属性检索必需的一组元数据。如果在检索元数据后(例如,在第一次更新后)更改 SelectCommand 的值,那么应调用 RefreshSchema 方法来更新该元数据。
由于您无法使用 GENERATED ALWAYS AS 列指定某个值,因此在 UPDATE 命令的 INSERT 和 SET 子句中将跳过此列。
DB2CommandBuilder 还使用由 SelectCommand 引用的 DB2Command.Connection、DB2Command.CommandTimeout 和 DB2Command.Transaction 属性。如果修改了这些属性中的任何属性,或者如果更改了 SelectCommand 属性本身的值,那么用户应调用 RefreshSchema。否则,DB2DataAdapter.InsertCommand、DB2DataAdapter.UpdateCommand 和 DB2DataAdapter.DeleteCommand 属性将保留其先前值。
如果调用 Dispose,那么 DB2CommandBuilder 与 DB2DataAdapter 就不再关联,并且不再使用生成的命令。
无法在 where 子句(blob,clob,xml)中使用的列将从 DB2DataAdapter.UpdateCommand 和 DB2DataAdapter.DeleteCommand 的 where 子句跳过。
[Visual Basic, C#] 以下示例将使用 DB2Command、DB2®DataAdapter 和 DB2Connection 从数据库中选择行。向该示例传递已初始化的 DataSet、连接字符串、作为 SQL SELECT 语句的查询字符串以及作为数据库表的名称的字符串。然后,该示例将创建 DB2CommandBuilder。
[Visual Basic]
Public Function SelectDB2SrvRows(myDataSet As DataSet, myConnection As String,
mySelectQuery As String, myTableName As String) As DataSet
Dim myConn As New DB2Connection(myConnection)
Dim myDataAdapter As New DB2DataAdapter()
myDataAdapter.SelectCommand = New DB2Command(mySelectQuery, myConn)
Dim employeeCB As DB2CommandBuilder = New DB2CommandBuilder(myDataAdapter)
myConn.Open()
Dim employeeDS As DataSet = New DataSet()
myDataAdapter.Fill(employeeDS, "EMPLOYEE")
' Code to modify data in DataSet here
' Without the DB2CommandBuilder this line would fail.
myDataAdapter.Update(employeeDS, "EMPLOYEE")
myConn.Close()
Return employeeDS
End Function 'SelectDB2SrvRows
[C#]
public DataSet SelectDB2SrvRows(DataSet
myDataSet,string myConnection,string mySelectQuery,string myTableName)
{
DB2Connection myConn = new DB2Connection(myConnection);
DB2DataAdapter myDataAdapter = new DB2DataAdapter();
myDataAdapter.SelectCommand = new DB2Command(mySelectQuery, myConn);
DB2CommandBuilder employeeCB = new DB2CommandBuilder(myDataAdapter);
myConn.Open();
DataSet employeeDS = new DataSet();
myDataAdapter.Fill(employeeDS, "EMPLOYEE");
//code to modify data in dataset here
//Without the DB2CommandBuilder this line would fail
myDataAdapter.Update(employeeDS, "EMPLOYEE");
myConn.Close();
return employeeDS;
}
此类型的任何公用静态(Visual Basic 中的 Shared)成员对于多线程操作都是安全的。 不保证任何实例成员均为线程安全。