Export (0) Print
Expand All

OleDbDataAdapter Constructor (String, OleDbConnection)

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

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

public OleDbDataAdapter(
	string selectCommandText,
	OleDbConnection selectConnection
)

Parameters

selectCommandText
Type: System.String

A string that is an SQL SELECT statement or stored procedure to be used by the SelectCommand property of the OleDbDataAdapter.

selectConnection
Type: System.Data.OleDb.OleDbConnection

An OleDbConnection that represents the connection.

This implementation of the OleDbDataAdapter opens and closes an OleDbConnection if it is not already open. This can be useful in an application that must call the Fill method for two or more OleDbDataAdapter objects. If the OleDbConnection is already open, you must explicitly call Close or Dispose to close it.

When you create an instance of OleDbDataAdapter, the following read/write properties are set to the following initial values.

Properties

Initial value

MissingMappingAction

MissingMappingAction.Passthrough

MissingSchemaAction

MissingSchemaAction.Add

You can change the value of either of these properties through a separate call to the property.

The following example creates an OleDbDataAdapter and sets some of its properties.

public static OleDbDataAdapter CreateDataAdapter(
    OleDbConnection connection)
{
    string selectCommand = 
        "SELECT CustomerID, CompanyName FROM Customers";
    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;
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft