OdbcCommandBuilder Class
Assembly: System.Data (in system.data.dll)
The OdbcDataAdapter does not automatically generate the SQL statements required to reconcile changes made to a DataSet associated with the data source. However, you can create an OdbcCommandBuilder object that generates SQL statements for single-table updates by setting the SelectCommand property of the OdbcDataAdapter. The OdbcCommandBuilder then generates any additional SQL statements that you do not set.
The relationship between an OdbcDataAdapter and its corresponding OdbcCommandBuilder is always one-to-one. To create this correspondence, you set the OdbcDataAdapter property of the OdbcCommandBuilder object. This causes the OdbcCommandBuilder to register itself as a listener, which produces the output of RowUpdating events that affect the DataSet.
To generate INSERT, UPDATE, or DELETE statements, the OdbcCommandBuilder uses the SelectCommand property to retrieve a required set of metadata. If you change the value of SelectCommand after the metadata has been retrieved, such as after the first update, you should then call the RefreshSchema method to update the metadata.
Note |
|---|
| If the SELECT statement assigned to the SelectCommand property uses aliased column names, the resulting INSERT, UPDATE, and DELETE statements may be inaccurate or fail. If the underlying ODBC driver cannot provide the appropriate base column name for the alias column name (using the SQL_DESC_BASE_COLUMN_NAME value of SQLColAttribute), the alias name could be used in the generated INSERT, UPDATE, and DELETE statements. For example, the Microsoft ODBC Driver for Oracle returns the alias name as the base column name. Therefore, the generated INSERT, UPDATE, and DELETE statements would cause errors. |
The OdbcCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by the SelectCommand. The user should call RefreshSchema if one or more of these properties are modified, or if the value of the SelectCommand property itself is changed. Otherwise the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.
If you call Dispose, the OdbcCommandBuilder is disassociated from the OdbcDataAdapter, and the generated commands are no longer used.
The following example uses OdbcCommand, together with OdbcDataAdapter and OdbcConnection, to select rows from a data source. The example is passed an initialized DataSet, a connection string, a query string that is an SQL SELECT statement, and a string that is the name of the data source table. The example then creates an OdbcCommandBuilder.
Public Function SelectOdbcSrvRows( _ ByVal connectionString As String, ByVal queryString As String, _ ByVal tableName As String) As DataSet Dim dataSet As DataSet = New DataSet Using connection As New OdbcConnection(connectionString) Dim adapter As New OdbcDataAdapter() adapter.SelectCommand = _ New OdbcCommand(queryString, connection) Dim builder As OdbcCommandBuilder = _ New OdbcCommandBuilder(adapter) connection.Open() adapter.Fill(dataSet, tableName) ' Code to modify data in DataSet here ' Without the OdbcCommandBuilder this line would fail. adapter.Update(dataSet, tableName) End Using Return dataSet End Function
System.MarshalByRefObject
System.ComponentModel.Component
System.Data.Common.DbCommandBuilder
System.Data.Odbc.OdbcCommandBuilder
Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.
Reference
OdbcCommandBuilder MembersSystem.Data.Odbc Namespace
Note