Agenda
- Difference between Connected Architecture and Disconnected Architecture
- DataReader and DataSet.
- Features of DataSet
- Working with DataSet
- Methods of DataAdapter
- Accessing data from DataSet
- DataTable
- Referring to a cell under DataTable
Let's start with the details.
ADO.NET provides the following two models for accessing data from a Data Source:
- Connection-Oriented Architecture
- Disconnected Oriented Architecture
1. Connection-Oriented Architecture
In this case, we require a continuous connection with the Data Source for accessing data in it.
Here the “DataReader” class holds the data on client machines.
2. Disconnected Oriented Architecture
In this case, we do not require a continuous connection with the Data Source for accessing data.
Here the “DataSet” class holds the data in the client machines.
DataSet
It is a class present under the “System.Data” namespace design for holding and managing data on client machines apart from a DataReader.
Features Of DataSet
- It is also capable of holding multiple tables.
- It is a designed disconnected architecture that doesn't require a permanent connection with a Data Source for holding data.
- It provides scrollable navigation to data, that allows us to move in any direction.
In other words, either top to bottom or bottom to top.
- It is updatable, in other words, changes can be performed to data present in it and also send changes back to the DB.
Working With DataSet
This class is responsible for the loading of data into a DataReader from a DataSource and is a command.
In the same way, the DataAdapter class is used for communication between a DataSource and DataSet.
Simply
DataReader < - Command -> DataSource.
DataSet <->DataAdapter <->DataSource.
Methods of DataAdapter
- Fill (DataSet ds,string TableName).
- Update (DataSet ds,string TableName)
Fill in the method to load data from a DataSource into a DataSet.
Update is to transfer data from a DataSet to a DataSource.
DataAdapter is internally a collection of the following 4 methods:
- Select Command.
- Insert Command.
- Update Command.
- Delete Command.
When we call the Fill() method of Adapter, the following action takes place internally.
- Open a connection with the DataSource.
- Execute the Select command under it on the DataSource and loads data from the table to the DataSet.
- Close the Connection.
Siince a DataSet is updatable, changes can be made to data that is loaded into it, like adding, modifying and deleting records.
After making all the changes to the data in a dataset if we want to send those changes back to the DataSource then call the Update() method on the DataAdapter that performed the following:
- Re-opened a connection with the DataSource.
- Changes made in the dataset will be sent back to the table where in this process it will use the insert, update and delete commands of the DataAdapter.
- Close the Connection.
Accessing Data From DataSet
DataReader provides us a pointer vases access to the data, so we can get data only in sequential order, whereas a dataset provides us index-based access to data so we can get the data from any location.
Dataset is a collection of tables where each table is represented as a DataTable class and identified by the index position.
DataTable
DataTable is the collection of tables.
The following syntax is used for a DataTable:
<DataSet>.Tables[Index]/[Name]
For example:
Ds.Tables[0]
Or
Ds.tables[“Company”]
Every DataTable is again a collection of Rows and Columns where each row is represented as a DataRow class and identified by its index position.
Each column is represented as a DataColumn class and identified by index position or name.
DataRow
It is a collection of rows.
Syntax:
<datatable>.Rows[Index].
For example:
Ds.tables[0].rows[0]
DataColumns
It is a collection of Columns.
Syntax:
<datatable>.Columns[Index] OR columns[Name]
For example:
Ds.Tables[0].Column[0]
Or:
Ds.Tables[0].Column[“ENO”]
Ds.tables[0].rows[0]
The following is the syntax for referring to a cell under a Data Table:
<datatable>.Rows[row] [col]
For example:
Ds.Tables[0].Rows[0][0]
Or:
Ds.Tables[0].Rows[0][“ENO”]