You are on page 1of 6

UNIT 05 (.

NET Framework using C#)


ADO.Net
(1) Overview of ADO.NET:
ADO.NET is a set of classes (a framework) to interact with data sources such as databases and XML files. ADO
is the acronym for ActiveX Data Objects. It allows us to connect to underlying data or databases. It has classes
and methods to retrieve and manipulate data.
The following are a few of the .NET applications that use ADO.NET to connect to a database, execute
commands and retrieve data from the database.
i. ASP.NET Web Applications
ii. Console Applications
iii. Windows Applications.
ADO.NET provides a bridge between the front end controls and the back end database. The ADO.NET objects
encapsulate all the data access operations and the controls interact with these objects to display data, thus hiding
the details of movement of data.
The following figure shows the ADO.NET:

Figure: ADO.NET
The DataSet Class:
 The dataset represents a subset of the database. It does not have a continuous connection to the database.
To update the database a reconnection is required. The DataSet contains DataTable objects and
DataRelation objects. The DataRelation objects represent the relationship between two tables.
The DataTable Class:
 The DataTable class represents the tables in the database. It has the following important properties; most
of these properties are read only properties except the PrimaryKey property.

Kushal Johari, Faculty of Computer Applications Page 1


The DataRow Class:
 The DataRow object represents a row in a table.

The DataAdapter Object:


 The DataAdapter object acts as a mediator between the DataSet object and the database. This helps the
Dataset to contain data from multiple databases or other data source.
The DataReader Object:
 The DataReader object is an alternative to the DataSet and DataAdapter combination. This object
provides a connection oriented access to the data records in the database. These objects are suitable for
read-only access, such as populating a list and then breaking the connection.
DbCommand and DbConnection Objects:
 The DbConnection object represents a connection to the data source. The connection could be shared
among different command objects.
 The DbCommand object represents the command or a stored procedure sent to the database from
retrieving or manipulating data.

(2) ADO. NET Classes:


Important Classes in ADO.NET are:
i. Connection Class
ii. Command Class
iii. DataReader Class
iv. DataAdaptor Class
v. DataSet.Class
(i) Connection Class:
 In ADO.NET, we use connection classes to connect to the database. These connection classes also
manage transactions and connection pooling. To learn more about connection classes, start here:
Connection in ADO.NET.
(ii) Command Class:
The Command class provides methods for storing and executing SQL statements and Stored Procedures.
 ExecuteReader: Returns data to the client as rows. This would typically be an SQL select statement or
a Stored Procedure that contains one or more select statements. This method returns a DataReader object
that can be used to fill a DataTable object or used directly for printing reports and so forth.
 ExecuteNonQuery: Executes a command that changes the data in the database, such as an update,
delete, or insert statement, or a Stored Procedure that contains one or more of these statements. This
method returns an integer that is the number of rows affected by the query.
(iii) DataReader Class:
 The DataReader is used to retrieve data. It is used in conjunction with the Command class to execute an
SQL Select statement and then access the returned rows.
(iv) DataAdaptor Class:
 The DataAdapter is used to connect DataSets to databases. The DataAdapter is most useful when using
data-bound controls in Windows Forms, but it can also be used to provide an easy way to manage the
connection between your application and the underlying database tables, views and Stored Procedures.

Kushal Johari, Faculty of Computer Applications Page 2


(v) DataSet Class:
 The DataSet is the heart of ADO.NET. The DataSet is essentially a collection of DataTable objects. In
turn each object contains a collection of DataColumn and DataRow objects. The DataSet also contains a
Relations collection that can be used to define relations among Data Table Objects.

(3) Connected and Disconnected Architecture of ADO.NET:


There are the following two types of connection architectures:
i. Connected Architecture:
a. In the connected architecture of ADO.net, connection must be opened to access the data retrieved
from database is called as connected architecture.
b. The application remains connected with the database throughout the processing.
c. Connected architecture is when you constantly make trips to the database for any CRUD (Create,
Read, Update and Delete) operation you wish to do.

Fig: Connected Architecture


ii. Disconnected Architecture:
a. In the disconnected architecture of ADO.net, the application automatically connects/disconnects
during the processing.
b. The application uses temporary data on the application side called a DataSet i.e. in which data
retrieved from database can be accessed even when connection to database was closed is called
as disconnected architecture.
c. Disconnected architecture is a method of retrieving a record set from the database and storing it
giving you the ability to do many CRUD (Create, Read, Update and Delete) operations on the
data in memory, and then it can be re-synchronized with the database when reconnecting.

Fig: Connected Architecture

Kushal Johari, Faculty of Computer Applications Page 3


Difference between Connected and Disconnected Architecture of ADO.NET:

Sno Connected Architecture Disconnected Architecture


1 It is connection oriented. It is dis_connection oriented.
2 Datareader used DataSet used
3 Connected methods gives faster performance Disconnected get low in speed and performance.
4 Connected can hold the data of single table disconnected can hold multiple tables of data
5 Data Reader can't persist the data Data Set can persist the data
6 It is Read only, we can't update the data. We can update data

Data List and Repeater, Data grid Controls


(4) Overview of List-Bound Controls:
ASP.NET provides a wide variety of rich controls that can be bound to data. Under the Data tab of the Visual
Studio Toolbox, you can get several controls that could be used to display data from a data source,
The standard ASP.NET data presentation controls are:
i. DataList
ii. DetailsView
iii. FormView
iv. GridView
v. ListView
vi. Repeater
We can divide these data presentation controls into the following two main groups.

GROUP 1 GROUP 2
1 DetailsView 1 DataList
2 FormView 2 GridView
3 ListView
4 Repeater
Description: Description:

These controls show collections of


These two controls can only show
records and are commonly called data
individual record at the same time.
list controls.

DataList:
 The Repeater control is a Data Bind Control, also known as container controls. The Repeater control is
used to display a repeated list of items that are bound to the control. This control may be bound to a
database table, an XML file, or another list of items. It has no built-in layout or styles, so you must
explicitly declare all layout, formatting and style tags within the controls templates.
GridView:
 The GridView control is used to display the values of a data source in a table. Each column represents a
field where each row represents a record. It can also display empty data. The GridView control provides
many built-in capabilities that allow the user to sort, update, delete, select and page through items in the
control.
Kushal Johari, Faculty of Computer Applications Page 4
ListView:
 The ListView control resembles the GridView control. The only difference between them is that the
ListView control displays data using user-defined templates instead of row fields. Creating your own
templates gives you more flexibility in controlling how the data is displayed. It enables you to bind to
data items that are returned from a data source and display them. The data can be displayed in pages
where you can display items individually, or you can group them.
Repeater:
 The Repeater control is used to display a repeated list of items that are bound to the control. This control
may be bound to a database table, an XML file, or another list of items.

(5) Creating a Repeater Control:


Advantage of Repeater Control:
Repeater is the fastest control in data controls available in Asp.Net. So, we can say performance of repeater
control is far better than other data control like GridView.
A Repeater has five inline templates to format it:
i. <HeaderTemplate> - Displays Header text for a Data Source collection and applies a different style for
the Header text.
ii. <AlternatingItemTemplate> - Changes the background color or style of alternating items in a Data
Source collection.
iii. <Itemtemplate> - It defines how the each item is rendered from the Data Source collection.
iv. <SeperatorTemplate> - It will determine the separator element that separates each item in the item
collection. It will be a <br> or <Hr> HTML element.
v. <FooterTemplate> - Displays a footer element for the Data Source collection.

(6) Introduction to the Data Grid:


.NET Framework provides DataGrid control to display data on the web page.
It was introduced in .NET 1.0 and now has been deprecated.
DataGrid is used to display data in scrollable grid. It requires data source to populate data in the grid.
It is a server side control and can be dragged from the toolbox to the web form.
Data Source for the DataGrid can be either a DataTable or a database.

(7) Advanced Data Grid Features:


In this example: I am showing the data in Data Grid tool or Grid View tool (in old version) through front end.
However, you can make the connection of Data Grid tool with the database and retrieve the data from database.

Kushal Johari, Faculty of Computer Applications Page 5


Take the Data Grid tool or Grid View tool (in old version) in your designing window and do the mentioned
code at Form Load:

namespace DataGrid
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("ID");
table.Columns.Add("Name");
table.Columns.Add("Email");
table.Rows.Add("1001", "Kushal Johari", "kushal.j@invertis.org");
table.Rows.Add("1002", "Shefali Singh", "shefali.s@invertis.org");
table.Rows.Add("1003", "Shivangi Ghildiyal", "shivangi.g@invertis.org");
table.Rows.Add("1004", "Archana Saxena", "archana.s@invertis.org");
GridView1.DataSource = table;
GridView1.DataBind();
}
}
}

Output:
It will display the data in table form at runtime:

Kushal Johari, Faculty of Computer Applications Page 6

You might also like