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. For more information, see Introduction to Data Adapters.
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.
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.
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
Open your form or component in the Code Editor.
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:
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:
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
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 (ADO.NET).