Export (0) Print
Expand All

OdbcCommandBuilder Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

Automatically generates single-table commands used to reconcile changes made to a DataSet with the associated data source. This class cannot be inherited.

For a list of all members of this type, see OdbcCommandBuilder Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.Odbc.OdbcCommandBuilder

[Visual Basic]
NotInheritable Public Class OdbcCommandBuilder
   Inherits Component
[C#]
public sealed class OdbcCommandBuilder : Component
[C++]
public __gc __sealed class OdbcCommandBuilder : public Component
[JScript]
public class OdbcCommandBuilder extends Component

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

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. Then, the OdbcCommandBuilder 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 (for example, after the first update), you then should 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 proper 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 result in errors.

The OdbcCommandBuilder also uses the Connection, CommandTimeout, and Transaction properties referenced by the SelectCommand. The user should call RefreshSchema if any 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.

Example

[Visual Basic, C#, C++] The following example uses OdbcCommand, along 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.

[Visual Basic] 
Public Shared Function SelectOdbcSrvRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
    Dim myConn As New OdbcConnection(myConnection)
    Dim myDataAdapter As New OdbcDataAdapter()
    myDataAdapter.SelectCommand = New OdbcCommand(mySelectQuery, myConn)
    Dim cb As OdbcCommandBuilder = New OdbcCommandBuilder(myDataAdapter)

    myConn.Open()

    Dim ds As DataSet = New DataSet
    myDataAdapter.Fill(ds, myTableName)

    ' Code to modify data in DataSet here 

    ' Without the OdbcCommandBuilder this line would fail.
    myDataAdapter.Update(ds, myTableName)

    myConn.Close()
End Function 'SelectOdbcSrvRows

[C#] 
public static DataSet SelectOdbcSrvRows(string myConnection, string mySelectQuery, string myTableName)
{
   OdbcConnection myConn = new OdbcConnection(myConnection);
   OdbcDataAdapter myDataAdapter = new OdbcDataAdapter();
   myDataAdapter.SelectCommand = new OdbcCommand(mySelectQuery, myConn);
   OdbcCommandBuilder cb = new OdbcCommandBuilder(myDataAdapter);

   myConn.Open();

   DataSet ds = new DataSet();
   myDataAdapter.Fill(ds, myTableName);

   //code to modify data in DataSet here

   //Without the OdbcCommandBuilder this line would fail
   myDataAdapter.Update(ds, myTableName);

   myConn.Close();

   return ds;
}

[C++] 
public:
    static DataSet* SelectOdbcSrvRows(String* myConnection, String* mySelectQuery, String* myTableName)
    {
        OdbcConnection* myConn = new OdbcConnection(myConnection);
        OdbcDataAdapter* myDataAdapter = new OdbcDataAdapter();
        myDataAdapter->SelectCommand = new OdbcCommand(mySelectQuery, myConn);
        OdbcCommandBuilder* cb = new OdbcCommandBuilder(myDataAdapter);

        myConn->Open();

        DataSet* ds = new DataSet();
        myDataAdapter->Fill(ds, myTableName);

        //code to modify data in DataSet here

        //Without the OdbcCommandBuilder this line would fail
        myDataAdapter->Update(ds, myTableName);

        myConn->Close();

        return ds;
    };

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Data.Odbc

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Data (in System.Data.dll)

See Also

OdbcCommandBuilder Members | System.Data.Odbc Namespace

Show:
© 2014 Microsoft