Edit

OleDbDataAdapter Class

Definition

Represents a set of data commands and a database connection that are used to fill the DataSet and update the data source.

public ref class OleDbDataAdapter sealed : System::Data::Common::DbDataAdapter, ICloneable
public ref class OleDbDataAdapter sealed : System::Data::Common::DbDataAdapter
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("OleDbConnection is not AOT-compatible.")]
public sealed class OleDbDataAdapter : System.Data.Common.DbDataAdapter, ICloneable
public sealed class OleDbDataAdapter : System.Data.Common.DbDataAdapter
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("OleDbConnection is not AOT-compatible.")>]
type OleDbDataAdapter = class
    inherit DbDataAdapter
    interface IDataAdapter
    interface IDbDataAdapter
    interface ICloneable
type OleDbDataAdapter = class
    inherit DbDataAdapter
    interface IDataAdapter
    interface IDbDataAdapter
    interface ICloneable
Public NotInheritable Class OleDbDataAdapter
Inherits DbDataAdapter
Implements ICloneable
Public NotInheritable Class OleDbDataAdapter
Inherits DbDataAdapter
Inheritance
Attributes
Implements

Examples

The following example uses the OleDbCommand, OleDbDataAdapter and OleDbConnection, to select records from an Access data source, and populate a DataSet with the selected rows. The filled DataSet is then returned. To accomplish this, the method is passed an initialized DataSet, a connection string, and a query string that is an SQL SELECT statement.

public static OleDbDataAdapter CreateDataAdapter(string selectCommand,
    OleDbConnection connection)
{
    OleDbDataAdapter adapter = new OleDbDataAdapter(selectCommand, connection);

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

    // Create the Insert, Update and Delete commands.
    adapter.InsertCommand = new OleDbCommand(
        "INSERT INTO Customers (CustomerID, CompanyName) " +
        "VALUES (?, ?)");

    adapter.UpdateCommand = new OleDbCommand(
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " +
        "WHERE CustomerID = ?");

    adapter.DeleteCommand = new OleDbCommand(
        "DELETE FROM Customers WHERE CustomerID = ?");

    // Create the parameters.
    adapter.InsertCommand.Parameters.Add("@CustomerID",
        OleDbType.Char, 5, "CustomerID");
    adapter.InsertCommand.Parameters.Add("@CompanyName",
        OleDbType.VarChar, 40, "CompanyName");

    adapter.UpdateCommand.Parameters.Add("@CustomerID",
        OleDbType.Char, 5, "CustomerID");
    adapter.UpdateCommand.Parameters.Add("@CompanyName",
        OleDbType.VarChar, 40, "CompanyName");
    adapter.UpdateCommand.Parameters.Add("@oldCustomerID",
        OleDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    adapter.DeleteCommand.Parameters.Add("@CustomerID",
        OleDbType.Char, 5, "CustomerID").SourceVersion =
        DataRowVersion.Original;

    return adapter;
}
Public Function CreateDataAdapter(ByVal selectCommand As String, _
    ByVal connection As OleDbConnection) As OleDbDataAdapter

    Dim adapter As OleDbDataAdapter = _
        New OleDbDataAdapter(selectCommand, connection)

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    ' Create the commands.
    adapter.InsertCommand = New OleDbCommand( _
        "INSERT INTO Customers (CustomerID, CompanyName) " & _
         "VALUES (?, ?)")

    adapter.UpdateCommand = New OleDbCommand( _
        "UPDATE Customers SET CustomerID = ?, CompanyName = ? " & _
        "WHERE CustomerID = ?")

    adapter.DeleteCommand = New OleDbCommand( _
        "DELETE FROM Customers WHERE CustomerID = ?")

    ' Create the parameters.
    adapter.InsertCommand.Parameters.Add( _
        "@CustomerID", OleDbType.Char, 5, "CustomerID")
    adapter.InsertCommand.Parameters.Add( _
        "@CompanyName", OleDbType.VarChar, 40, "CompanyName")

    adapter.UpdateCommand.Parameters.Add( _
        "@CustomerID", OleDbType.Char, 5, "CustomerID")
    adapter.UpdateCommand.Parameters.Add( _
        "@CompanyName", OleDbType.VarChar, 40, "CompanyName")
    adapter.UpdateCommand.Parameters.Add( _
        "@oldCustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    adapter.DeleteCommand.Parameters.Add( _
        "@CustomerID", OleDbType.Char, 5, "CustomerID").SourceVersion = _
        DataRowVersion.Original

    Return adapter
End Function

Remarks

The OleDbDataAdapter serves as a bridge between a DataSet and data source for retrieving and saving data. The OleDbDataAdapter provides this bridge by using Fill to load data from the data source into the DataSet, and using Update to send changes made in the DataSet back to the data source.

When the OleDbDataAdapter fills a DataSet, it will create the appropriate tables and columns for the returned data if they do not already exist. However, primary key information is not included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey. You may also have the OleDbDataAdapter create the schema of the DataSet, including primary key information, before filling it with data using FillSchema. For more information, see Adding Existing Constraints to a DataSet.

Note that some OLE DB providers, including the MSDataShape provider, do not return base table or primary key information. Therefore, the OleDbDataAdapter cannot correctly set the PrimaryKey property on any created DataTable. In these cases you should explicitly specify primary keys for tables in the DataSet.

The OleDbDataAdapter also includes the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and TableMappings properties to facilitate the loading and updating of data.

When you create an instance of OleDbDataAdapter, properties are set to their initial values. For a list of these values, see the OleDbDataAdapter constructor.

Constructors

Name Description
OleDbDataAdapter()

Initializes a new instance of the OleDbDataAdapter class.

OleDbDataAdapter(OleDbCommand)

Initializes a new instance of the OleDbDataAdapter class with the specified OleDbCommand as the SelectCommand property.

OleDbDataAdapter(String, OleDbConnection)

Initializes a new instance of the OleDbDataAdapter class with a SelectCommand.

OleDbDataAdapter(String, String)

Initializes a new instance of the OleDbDataAdapter class with a SelectCommand.

Properties

Name Description
DeleteCommand

Gets or sets an SQL statement or stored procedure for deleting records from the data set.

InsertCommand

Gets or sets an SQL statement or stored procedure used to insert new records into the data source.

SelectCommand

Gets or sets an SQL statement or stored procedure used to select records in the data source.

UpdateCommand

Gets or sets an SQL statement or stored procedure used to update records in the data source.

Methods

Name Description
Fill(DataSet, Object, String)

Adds or refreshes rows in the DataSet to match those in an ADO Recordset or Record object using the specified DataSet, ADO object, and source table name.

Fill(DataTable, Object)

Adds or refreshes rows in a DataTable to match those in an ADO Recordset or Record object using the specified DataTable and ADO objects.

Events

Name Description
RowUpdated

Occurs during Update(DataSet) after a command is executed against the data source. The attempt to update is made. Therefore, the event occurs.

RowUpdating

Occurs during Update(DataSet) before a command is executed against the data source. The attempt to update is made. Therefore, the event occurs.

Explicit Interface Implementations

Name Description
ICloneable.Clone()

For a description of this member, see Clone().

IDbDataAdapter.DeleteCommand

For a description of this member, see DeleteCommand.

IDbDataAdapter.InsertCommand

For a description of this member, see InsertCommand.

IDbDataAdapter.SelectCommand

For a description of this member, see SelectCommand.

IDbDataAdapter.UpdateCommand

For a description of this member, see UpdateCommand.

Applies to

See also