System.Data.OleDb Namespace
Provides a means to automatically generate single-table commands used to reconcile changes made to a DataSet with the associated database.
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.
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 initializes an OleDbCommandBuilder.
public DataSet SelectOleDbSrvRows ( DataSet myDataSet, string myConn,
string query, string myTableName ) {
OleDbConnection myConn = new OleDbConnection ( myConn );
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter ( );
myDataAdapter.SelectCommand = new OleDbCommand ( query, myConn );
OleDbCommandBuilder custCB = new OleDbCommandBuilder ( myDataAdapter );
myConn.Open ( );
DataSet custDS = new DataSet ( );
myDataAdapter.Fill ( custDS, "Customers" );
// ... code to modify data in dataset here ...
// Without the OleDbCommandBuilder this line would fail
myDataAdapter.Update ( custDS, "Customers" );
myConn.Close ( );
return custDS;
}
Public Function SelectOleDbSrvRows ( myDataSet As DataSet, myConn As String, _
query As String, myTableName As String ) As DataSet
Dim myConn As New OleDbConnection ( myConn )
Dim myDataAdapter As New OleDbDataAdapter ( )
myDataAdapter.SelectCommand = New OleDbCommand ( query, myConn )
Dim custCB As OleDbCommandBuilder = New OleDbCommandBuilder ( myDataAdapter )
myConn.Open ( )
Dim custDS As DataSet = New DataSet
myDataAdapter.Fill ( custDS, "Customers" )
' ... code to modify data in DataSet here ...
' without the OleDbCommandBuilder this line would fail.
myDataAdapter.Update ( custDS, "Customers" )
myConn.Close ( )
SelectOleDbSrvRows = custDS
End Function |
|
C# |
VB |