Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Five different overloads of the DataAdapter.Fill() method

4.75/5 (8 votes)
20 Sep 2012CPOL2 min read 102K  
A look into the five different overloads of the DataAdapter.Fill() method.

Introduction

The SqlDataAdapter serves as a bridge between a DataSet and SQL Server for retrieving and saving data. The SqlDataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet, using the appropriate Transact-SQL statements against the data source. The update is performed on a by-row basis. For every inserted, modified, and deleted row, the Update method determines the type of change that has been performed on it (Insert, Update, or Delete). Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. When the SqlDataAdapter fills a DataSet, it creates the necessary 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 SqlDataAdapter 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 (ADO.NET).

SqlDataAdapter is used in conjunction with SqlConnection and SqlCommand to increase performance when connecting to a SQL Server database.

The SqlDataAdapter also includes the SelectCommand, InsertCommand, DeleteCommand, UpdateCommand, and TableMappings properties to facilitate the loading and updating of data.

When an instance of SqlDataAdapter is created, the read/write properties are set to initial values. For a list of these values, see the SqlDataAdapter constructor.

The InsertCommand, DeleteCommand, and UpdateCommand are generic templates that are automatically filled with individual values from every modified row through the parameters mechanism.

Step 1

First Overload

DataAdapter.Fill (ds) here fetches the data from User and fills in the DataSet ds.

Image 1

Output

Image 2

Step 2

Second Overload

Here DataAdapter.Fill (dt) fetches records from Users and fills in the DataTable named dt.

Image 3

Output

Image 4

Step 3

Third Overload

Here a datatable named User has been filled by the DataAdapter.Fill () method.

Image 5

Output

Image 6

Step 4

Fourth Overload

This overload takes three parameters. The types of parameters are int, int, and DataTable. Here first parameter is the starting record, second parameter is the maximum number of record and the last parameter is the DataTable where we have to fill the records. The ranges from which the records are to be fetched are mentioned here using the parameters.

Image 7

Output

Image 8

Step 5

Fifth Overload

At last, this overload takes four parameters. The types of parameters are DataTable, int, int, string. The first parameter is the name of the dataset, second parameter is the starting record number, third parameter is the maximum number of records and the last parameter is the name of the table.

At first ds is filled with all the records of Users. And then, DataAdapter.Fill () method has filled the DataTable nm with the specified range of records.

Image 9

Output

Image 10

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)