Freigeben über


How to: Fill a Dataset with Data

The phrase "filling a dataset with data" refers to loading data into the individual DataTable objects that make up the dataset. You fill the data tables by executing TableAdapter queries or by executing data adapter (for example, SqlDataAdapter) commands.

Whether you should use TableAdapters or data adapters depends on how you created the dataset. If you used the design tools in Visual Studio, such as the Data Source Configuration Wizard, your dataset contains TableAdapters. For more information on TableAdapters, see TableAdapter Overview. If you created your dataset programmatically, you will typically need to create data adapters to load data into the data tables.

Note

When dragging items from the Data Sources Window onto a form, the code to fill the data table with data is automatically added to the Form_Load event handler. Open your form in the code editor to see the exact syntax to fill your specific tables. If you do not want to fill the table when the form loads, you can move this code to some other method, or remove it entirely.

Filling a Dataset Using a TableAdapter

You can call a query on the TableAdapter to load data into data tables in a dataset. Pass the DataTable you want to fill to the TableAdapter query. If your query takes parameters, pass those to the method as well. If the dataset contains multiple tables, you should have separate TableAdapters for each table and must therefore fill each table separately.

Note

By default, every time you execute a TableAdapter query, the data in the table is cleared prior to the results of the query being loaded into the table. You can keep the existing data in the table and append the results by setting the TableAdapter's ClearBeforeFill property to false.

To fill a dataset with data using a TableAdapter

  1. Open your form or component in the Code Editor.

  2. Add code anywhere in your application where you need to load a data table with data. If your query does not take parameters, pass in the DataTable you want to fill. The code should look similar to the following:

    Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)
    
    this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
    
  3. If your query takes parameters, pass in the DataTable you want to fill and the parameters expected by the query. Depending on the actual parameters in your query, the code would look similar to the following examples:

    CustomersTableAdapter.FillByCity(NorthwindDataSet.Customers, "Seattle")
    CustomersTableAdapter.FillByCityAndState(NorthwindDataSet.Customers, "Seattle", "WA")
    
    customersTableAdapter.FillByCity(northwindDataSet.Customers, "Seattle");
    customersTableAdapter.FillByCityAndState(northwindDataSet.Customers, "Seattle", "WA");
    

Filling a Dataset Using a DataAdapter

You call the data adapter's Fill method. This causes the adapter to execute the SQL statement or stored procedure referenced in its SelectCommand property and put the results into a table in the dataset. If the dataset contains multiple tables, you should have separate data adapters for each table and must therefore fill each table separately.

To fill a dataset with data using a DataAdapter

  • Call the Fill method of the DataAdapter, passing in the DataSet or DataTable to load the data into. For example:

    sqlDataAdapter1.Fill(dataset1.Tables("Customers"))
    
    sqlDataAdapter1.Fill(dataset1.Tables["Customers"]);
    

    You should typically provide the name of the DataTable to load the data into. If you pass in the name of a DataSet instead of a specific data table, a DataTable named Table1 is added to the dataset and loaded with the results from the database (as opposed to loading the data in an existing DataTable in the dataset). For more information, see Populating a DataSet from a DataAdapter.

See Also

Concepts

Filling Datasets with Data

Fetching Data into Your Application

Preparing Your Application to Receive Data

Binding Controls to Data in Visual Studio

Editing Data in Your Application

Validating Data

Saving Data