How to: Create and Configure Data Adapters Manually

You can create data adapters without using a wizard. No matter how you have created the adapter, you can always make configuration changes to it.

Note

In the previous version of Visual Studio, data adapters were used for communicating between an application and a database. While data adapters are still a main component of .NET Framework Data Providers (ADO.NET), TableAdapters are designer-generated components that simplify the process of moving data between your application and a database. For more information on working with TableAdapters, see TableAdapter Overview.

Note

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

By default in Visual Studio, design-time support for data adapters has been turned off. To this support for data adapters, you must add them to the Toolbox, as described in the first procedure below.

To add data adapters, connections, and commands to the Toolbox

  1. Right-click the Toolbox and choose Choose Items.

  2. Select the items you want to add from the .NET Framework Components tab of the Choose Toolbox Items dialog box. For example, to use the .NET Framework Data Provider for SQL Server select the SqlDataAdapter, SqlConnection, and SqlCommand.

  3. Click OK to add the selected items to the Toolbox.

To create a data adapter manually

  1. Make sure a connection object is available to the form or component that you are working with. For details about adding a standalone connection, see Establishing the Connection (ADO.NET).

  2. From the Data tab of the Toolbox, drag an OleDbDataAdapter, SqlDataAdapter, OdbcDataAdapter, or OracleDataAdapter object onto the design surface.

    The designer adds an instance of the adapter to the form or component and launches the Data Adapter Configuration Wizard.

  3. Close the wizard.

To configure a data adapter manually

  1. Select the adapter, and then in the Properties window, configure the commands used to read and update data. You must configure the SelectCommand object. If the adapter will be used to update a data source, you must also configure the UpdateCommand, DeleteCommand, and InsertCommand objects.

    For each command object, set the following properties.

    Property

    Description

    ActiveConnection

    Set to reference a connection object. You can create a connection object from the ActiveConnection property in the Properties window. Typically, each command object will reference the same connection object, but you have the option of using different connections for each command if your application requires it.

    CommandText

    Either the text of an SQL statement or the name of a stored procedure.

    For some providers, you can include multiple statements or stored procedure names in the CommandText property, delimited with semicolons (;). The statements or procedures are executed sequentially. This is useful for situations such as issuing a SELECT statement after an update or INSERT statement in order to get back refreshed records and any auto-generated values (such as default or auto-increment values).

    CommandType

    A value indicating how the value of the CommandText property should be interpreted:

    • Text - the command is an SQL statement.

    • StoredProcedure - the command is a reference to a stored procedure.

    • TableDirect - the text is not a command, but the name of a table.

    Parameters

    A collection of objects of type Parameter that you configure to pass values to the command. In the SelectCommand object, you create a parameters collection only if the command includes a parameter placeholder. UpdateCommand, InsertCommand, and DeleteCommand objects always require parameters. For details, see How to: Configure Parameters for Data Adapters.

  2. If you do not want the same column names in the data source and dataset, change the mappings. The default value for the MissingMappingAction property, Passthrough, will generate the same names in the data source and the dataset automatically. For more information, see How to: Map Data-Source Columns to Dataset Data-Table Columns.

  3. From the Data menu, choose Generate DataSet.

    Tip

    If you do not see the Data menu, click anywhere in the form or component designer; the menu only appears if the designer has the focus.

  4. If you want to see how the data adapter will fill a dataset, preview the results. For more information, see How to: Preview Data-Adapter Results.

See Also

Concepts

Populating a DataSet from a DataAdapter (ADO.NET)

What's New in Data

Creating Data Applications by Using Visual Studio

Other Resources

DataAdapters and DataReaders (ADO.NET)

Creating Data Adapters

Data Walkthroughs

ADO.NET