Export (0) Print
Expand All

OleDbCommandBuilder Class

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

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

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Data.OleDb.OleDbCommandBuilder

[Visual Basic]
NotInheritable Public Class OleDbCommandBuilder
   Inherits Component
[C#]
public sealed class OleDbCommandBuilder : Component
[C++]
public __gc __sealed class OleDbCommandBuilder : public Component
[JScript]
public class OleDbCommandBuilder 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 OleDbDataAdapter does not automatically generate the SQL statements required to reconcile changes made to a DataSet with the associated data source. However, you can create an OleDbCommandBuilder object to automatically generate SQL statements for single-table updates if you set the SelectCommand property of the OleDbDataAdapter. Then, any additional SQL statements that you do not set are generated by the OleDbCommandBuilder.

The OleDbCommandBuilder registers itself as a listener for RowUpdating events whenever you set the DataAdapter property. You can only associate one OleDbDataAdapter or OleDbCommandBuilder object with each other at one time.

To generate INSERT, UPDATE, or DELETE statements, the OleDbCommandBuilder uses the SelectCommand property to retrieve a required set of metadata automatically. If you change the SelectCommand after the metadata has is retrieved (for example, after the first update), you should call the RefreshSchema method to update the metadata.

The OleDbCommandBuilder 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 SelectCommand itself is replaced. Otherwise the InsertCommand, UpdateCommand, and DeleteCommand properties retain their previous values.

If you call Dispose, the OleDbCommandBuilder is disassociated from the OleDbDataAdapter, and the generated commands are no longer used.

Example

[Visual Basic, C#, C++] The following example uses the OleDbCommand, along OleDbDataAdapter and OleDbConnection, 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 OleDbCommandBuilder.

[Visual Basic] 
Public Shared Function SelectOleDbSrvRows(myConnection As String, mySelectQuery As String, myTableName As String) As DataSet
    Dim myConn As New OleDbConnection(myConnection)
    Dim myDataAdapter As New OleDbDataAdapter()
    myDataAdapter.SelectCommand = New OleDbCommand(mySelectQuery, myConn)
    Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(myDataAdapter)

    myConn.Open()

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

    ' Code to modify data in DataSet here 

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

    myConn.Close()
End Function 'SelectOleDbSrvRows

[C#] 
public static DataSet SelectOleDbSrvRows(string myConnection, string mySelectQuery, string myTableName)
{
   OleDbConnection myConn = new OleDbConnection(myConnection);
   OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
   myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
   OleDbCommandBuilder cb = new OleDbCommandBuilder(myDataAdapter);

   myConn.Open();

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

   //code to modify data in DataSet here

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

   myConn.Close();

   return ds;
}

[C++] 
public:
    static DataSet* SelectOleDbSrvRows(String* myConnection, String* mySelectQuery, String* myTableName)
    {
        OleDbConnection* myConn = new OleDbConnection(myConnection);
        OleDbDataAdapter* myDataAdapter = new OleDbDataAdapter();
        myDataAdapter->SelectCommand = new OleDbCommand(mySelectQuery, myConn);
        OleDbCommandBuilder* cb = new OleDbCommandBuilder(myDataAdapter);

        myConn->Open();

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

        //code to modify data in DataSet here

        //Without the OleDbCommandBuilder 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.OleDb

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

OleDbCommandBuilder Members | System.Data.OleDb Namespace

Show:
© 2014 Microsoft