DataSet and its advantages

The DataSet in ADO.Net is a disconnected, in-memory representation of data. Its primary role is to store a collection of DataTables and the relations and constraints between those DataTables. DataSets only hold data and do not interact with a data source. It is the SqlDataAdapter that manages connections with the data source and gives us disconnected behavior. The SqlDataAdapter opens a connection only when required and closes it as soon as it has performed its task. DataSet and its advantages are explained below.

Creating a DataSet Object


DataSet dsObj = new DataSet();

The SqlDataAdapter contains all of the commands necessary to interact with the data source. DataAdapter will acts as a Bridge between DataSet and database. This dataadapter object is used to read the data from database and bind that data to dataset. Dataadapter is a disconnected oriented architecture.

Creating A SqlDataAdapter


SqlDataAdapter daObj = new SqlDataAdapter("Sql Query", ConnectionObject);

Filling the DataSet


daObj .Fill(dsObj , "Name of the table to be filled");

Fill method, in the code above, takes two parameters: a DataSet and a table name. The DataSet must be instantiated before trying to fill it with data. The second parameter is the name of the table that will be created in the DataSet.

Using the DataSet


datagridObj.DataSource = dsObj ; 
datagridObj.DataMember = "Name of the table";

Updating Changes


daObj .Update(dsObj , "Name of the table");




Demo Code