Export (0) Print
Expand All

OleDbDataAdapter Class

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

Namespace: System.Data.OleDb
Assembly: System.Data (in system.data.dll)

public sealed class OleDbDataAdapter : DbDataAdapter, IDbDataAdapter, IDataAdapter, ICloneable
public final class OleDbDataAdapter extends DbDataAdapter implements IDbDataAdapter, IDataAdapter, 
	ICloneable
public final class OleDbDataAdapter extends DbDataAdapter implements IDbDataAdapter, IDataAdapter, 
	ICloneable

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.

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;


using System;
using System.Data;
using System.Data.OleDb;

class Class1
{
    static void Main()
    {
    

    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;
    

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Data.Common.DataAdapter
         System.Data.Common.DbDataAdapter
          System.Data.OleDb.OleDbDataAdapter

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

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.

.NET Framework

Supported in: 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft