Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Chapter No. 5 Data Access with ADO.

NET
● Overview of ADO.NET Objects
● Create and retrieve Database Connections
● SqlDataSource Controls
● ASP.NET Data-Bound Controls
● GridView, Repeater, DataList, Details View, Form View
● Overview of ADO.Net Objects:
ADO.Net Architecture:-
The most important concept in ADO.NET architecture is “Data Provider”. Data Provider provides
access to data sources (SQL Server, Access, Oracle). In short, it provides an object to achieve
functionalities like opening and closing connection, retrieve data, and update data. In the below figure,
you can see the four main sections of a data provider:
● Connection.
● Command object (this is the object for using Stored Procedures).
● Data Adapter (this object acts as a bridge between the data store and the dataset).
● Data Reader (this object reads data from the data store in forward only mode).
● DataSet object represents disconnected and cached data. If you see the diagram, it is not in
direct connection with the data store (SQL Server, Oracle, etc.) rather it talks with the data
adapter, who is responsible for filling the dataset. The dataset can have one or more datatables
and relations.
● The DataView object is used to sort and filter data in the datatable.

ADO.Net:-
ADO.NET is the new database technology used in .NET platform. ADO.NET is the next step
of Microsoft ActiveX Data Objects (ADO). It shares much of the ADO functionality. The ADO.NET
covers the classes in the System.Data namespace. ADO.NET is a set of classes that expose the data
access services of the .NET Framework.
✔ ADO stands for ActiveX Data Objects
✔ ADO.NET is a database technology of .NET Framework used to connect application system
and database server.
✔ ADO.NET is a part of the .NET Framework
✔ ADO.NET consists of a set of classes used to handle data access
✔ ADO.NET uses XML to store and transfer data among applications, which is not only an
industry standard but also provide fast access of data for desktop and distributed applications.
✔ ADO.NET is scalable and interoperable
ADVANTAGES OF ADO.NET
1) Interoperability. ADO.NET uses XML as the format for transmitting data from a database to a
local memory.
2) Maintainability. When an increasing number of users work with an application, the increased use
can strain resources. By using n-tier applications, you can spread application logic across
additional tiers. ADO.NET architecture uses local in-memory caches to hold copies of data,
making it easy for additional tiers to trade information.
3) Programmability. The ADO.NET programming model uses strongly typed data. Strongly typed
data makes code more concise and easier to write because Microsoft Visual Studio .NET provides
statement completion.
4) Performance. ADO.NET helps you to avoid costly data type conversions because of its use of
strongly typed data.
5) Scalability. The ADO.NET programming model encourages programmers to conserve system
resources for applications that run over the Web. Because data is held locally in in memory caches,
there is no need to retain database locks or maintain active database connections for extended
periods.
CONNECTED VS DISCONNECTED: For much of the history of computers, the only environment
available was the connected environment. In many situations, people do not work entirely in a
connected or disconnected environment, but rather in an environment that combines the two
approaches.
Connected: A connected environment is one in which an application is constantly connected to a data
source. A connected architecture has following advantages:
a) A secure environment is easier to maintain.
b) Concurrency is easier to control.
c) Data is more likely to be current than in other scenarios.
A connected scenario has the following disadvantages:
a) It must have a constant network connection.
b) Scalability
Disconnected: A disconnected environment is one in which an application is not constantly connected
to a source of data. Users can take a subset of data with them on a disconnected computer, and then
merge changes back into the central data store.
A disconnected environment provides the following advantages:
a) You can work at any time that is convenient for you, and can connect to a data source at any
time to process requests.
b) Other users can use the connection.
c) A disconnected environment improves the scalability and performance.
A disconnected environment has the following disadvantages:
● Data is not always up to date.
● Change conflicts can occur and must be resolved.
.NET DATA PROVIDER
A .NET data provider is used for connecting to a database, executing commands, and retrieving
results. Those results are either processed directly, or placed in an ADO.NET DataSet in order to be
exposed to the user in an ad-hoc manner, combined with data from multiple sources, or remoted
between tiers. The .NET data provider is designed to be lightweight, creating a minimal layer between
the data source and your code, increasing performance without sacrificing functionality. The
ADO.NET object model includes the following data provider classes:
1. SQL Server .NET Data Provider
2. OLE DB .NET Data Provider
3. Oracle .NET Data Provider
4. ODBC .NET Data Provider
5. Other Native .NET Data Provider
1) SQL Server Data Provider: To use the SQL Server .NET Data Provider, you need to include the
System.Data.SqlClient namespace in your applications. Using this provider is more efficient than
using the OLE DB .NET Data Provider because it does not pass through an OLE DB or Open
Database Connectivity (ODBC) layer. It provides optimized access to SQL Server 2000 and SQL
Server 7.0 databases.
2) OLE DB Data Provider: To use the OLE DB .NET Data Provider, you need to include the
System.Data.OleDb namespace in your applications. .NET Provides access to SQL Server
versions 6.5 and earlier. It also provides access to other databases, such as Oracle, Sybase,
DB2/400, and Microsoft Access.
3) Oracle .NET Data Provider: To use the Oracle Database, a native Oracle .NET data driver is the
best choice. To use the Oracle .NET Data Provider, you need to include the
System.Data.OracleClient namespace in your applications. Oracle itself also provides a .NET
data provider, referenced as Oracle.DataAccess.Client. This is a separate download that you have
to get from Oracle. Whether you use a .NET data provider from the database vendor or just use the
one provided with the .NET framework is your choice.
4) ODBC .NET Data Provider: If you have a data source for which no native or OLE DB provider
is available, the ODBC .NET data provider is good alternative because most database provide an
ODBC interface. It is referenced with this using directive: System.Data.Odbc
5) Other Native .NET Data Provider: If there is a native .NET data provider available specifically
for your database, then you may want to use that .NET data provider instead. Many other database
vendors and third-party companies provide native.

.NET data providers; the choice between using the native providers and using something
generic like the ODBC provider will depend on your circumstances. If you value portability over
performance, then go generic. If you want to get the best performance or make the best use of a
particular database„s features, go native.

● Create and Retrieve Database Connection:

Providers: An ADO.NET provider is a class that can communicate with a specific type of database or
data store, such as SQL Server, Oracle, MS-Access, etc. database. The providers included with the
.NET Framework are:

1. SqlServer:- The .NET Framework Data Provider for SQL Server in the System.Data.SqlClient
namespace. This provider is the default for the SqlDataSource control.
2. OleDb:- The .NET Framework Data Provider for OLE DB in the System.Data.OleDb
namespace.
3. Odbc:- The .NET Framework Data Provider for ODBC in the System.Data.Odbc namespace.

Connection Strings: A connection string provides the information that a provider needs to
communicate with a particular database. Depending on the provider, a connection string usually
supplies the server or location of the database server, the database name and authentication
information.

Creating Connection:
1) Open Visual Studio, select “Server Explorer” Connect to Database Add Connection. Window
will open.(like the following image.)

2) Select “Data Source” such as “Microsoft Access Database”


3) Click on “Browse” for selection of database name. Then click “Test Connection”.
4) Design web form using form controls.
5) Double click on the form and add some code
using System.Data.OleDb;
OledbConnection con;
OledbConnection con= new OledbCOnnection ("Provider =
Microsoft.Jet.OLEDB.4.0; Data Source=C:\mydatabase.mdb");
Retrieve and display data
OLEDBCommand cmd=new OLEDBCOmman();
cmd.Connection=con;
cmd.CommandType=CommandType.Text;
cmd.CommandText="Select * from emp";
OledbDataReader dr= new OledbDataReader();
dr=cmd.Executereader();
while (dr.Read()==true)
{
// Do some coding here
}
● SqlDataSource Controls:
As you may guess, the SquDataSource control in ASP.NET 3.5 is designed to work with a
SQL Server database. It uses SQL Server .NET data provider internally to connect to the database.
SQL Server .NET data provider classes are defined in the System.Data.SqlClient
namespace.The SqlDataSource data source control represents data in an SQL relational database to
data-bound controls. You can use the SqlDataSource control in conjunction with a data-bound control
to retrieve data from a relational database and to display, edit, and sort data on a Web page with little
or no code. SqlDataSource control inherited from DataSourceControl class, which provides common
functionality for all of these data source controls.
The SqlDataSource class provides a FilterExpression property that can be used to filter the
results of calling the SqlDataSource class' Select method.
To connect to a database, you must set the ConnectionString property to a valid connection
string. The SqlDataSource can support any SQL relational database that can be connected to using an
ADO.NET provider, such as the SqlClient, OleDb, Odbc, or OracleClient. The steps are as follows-
(1) Create a web site and add a SqlDataSourceControl on the web form.
(2) Click on the Configure Data Source option.
(3) Click on the New Connection button to establish connection with a database.
(4) Once the connection is set up, you may save it for further use. At the next step, you are asked
to configure the select statement:
(5) Select the columns and click next to complete the steps. These buttons allow you to specify the
insert, update, and delete commands of SQL. This way, you can manipulate the data.
(6) Add a GridView control on the form. Choose the data source and format the control using
AutoFormat option.
(7) After this the formatted GridView control displays the column headings, and the application is
ready to execute.
(8) Finally execute the application.
● ASP.Net Data bound controls:
Every ASP.NET web form control inherits the DataBind method from its parent Control class,
which gives it an inherent capability to bind data to at least one of its properties. This is known as
simple data binding or inline data binding.
Simple data binding involves attaching any collection (item collection) which implements the
IEnumerable interface, or the DataSet and DataTable classes to the DataSource property of the control.
On the other hand, some controls can bind records, lists, or columns of data into their structure
through a DataSource control. These controls derive from the BaseDataBoundControl class. This is
called declarative data binding.
The data source controls help the data-bound controls implement functionalities such as, sorting,
paging, and editing data collections.
The BaseDataBoundControl is an abstract class, which is inherited by two more abstract classes:
● DataBoundControl
● HierarchicalDataBoundControl
The abstract class DataBoundControl is again inherited by two more abstract classes:
● ListControl
● CompositeDataBoundControl
The controls capable of simple data binding are derived from the ListControl abstract class and these
controls are:
● BulletedList ● DropDownList
● CheckBoxList ● ListBox
● RadioButtonList
The controls capable of declarative data binding (a more complex data binding) are derived from the
abstract class CompositeDataBoundControl. These controls are:
● DetailsView ● GridView
● FormView ● RecordList
● GridView:
ASP.NET provides a number of tools for showing tabular data in a grid, including the
GridView control. It was introduced with ASP.NET 2.0. 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. The GridView control can be
bound to a data source control, in order to bind a data source control, set the DataSourceID property of
the GridView control to the ID value of the data source control. It's considered a replacement for the
DataGrid control from .NET 1.1. Therefore, it is also known as a super DataGrid. The GridView
control offers improvements such as the ability to define multiple primary key fields, improved user
interface customization using bound fields and templates and a new model for handling or canceling
events. Performance is slow compared to DataGrid and ListView.
The GridView control supports the following features:
● Improved data source binding capabilities
● Tabular rendering – displays data as a table
● Item as row
● Built-in sorting capability
● Built-in select, edit and delete capabilities
● Built-in paging capability
● Built-in row selection capability
● Multiple key fields
● Programmatic access to the GridView object model to dynamically set properties, handle
events and so on
● Richer design-time capabilities
● Control over Alternate item, Header, Footer, Colors, font, borders, and so on.
● Slow performance as compared to Repeater and DataList control
● Repeater:
The Repeater control was introduced with ASP.NET 1.0. The ASP.NET Repeater is a basic
container control that allows you to create custom lists from any data available to the page. It provides
a highly customized interface. It renders a read-only template; in other words, it supports only the
ItemTemplate to define custom binding. 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. You would require writing an explicit code to do paging using this control. The
Repeater repeats a layout of HTML you write, it has the least functionality of the rest of the three
controls.
The Repeater control supports the following features:
● List format
● Item as row
● Paging, Sorting and Grouping requires custom code writing
● DataList:
The DataList control was introduced with ASP.NET 1.0. DataList is the next step up from a
Repeater; except you have very little control over the HTML that the control renders. DataList allows
you to repeat columns horizontally or vertically. The DataList control renders data as a table and
enables you to display data records in various layouts, such as ordering them in columns or rows. You
can configure the DataList control to enable users to edit or delete a record in the table. We can use a
DataList control where we need a single-column list. The DataList control works like the Repeater
control, used to display the data in a repeating structure, such as a table. It displays data in a format
that you can define using a template and styles. However, it arranges the data defined in the template
within various HTML structures. This includes options for horizontal or vertical layout and it also
allows you to set how the data should be repeated, as flow or table layout. The DataList control does
not automatically use a data source control to edit data. Instead, it provides command events in which
you can write your own code for these scenarios. You can configure the DataList control where the
user can edit or delete a record in the table.
The DataList control supports the following features:
● Support for binding data source controls such as SqlDataSource, LinqDataSource and
ObjectDataSource
● Good for columns
● Item as cell
● Updatable

● DetailsView:
The DetailsView control was introduced with ASP.NET 2.0. The DetailsView control uses a
table-based layout where each field of the data record is displayed as a row in the control. Unlike the
GridView control, the DetailsView control displays one row from a data source at a time by rendering
an HTML table. The DetailsView supports both declarative and programmatic data binding. The
DetailsView control is often used in master-detail scenarios where the selected record in a master
control determines the record to display in the DetailsView control. It shows the details for the row in
a separate space. We can customize the appearance of the DetailsView control using its style
properties. Alternatively, we can also use Cascading Style Sheets (CSS) to provide styles to a
DetailsView control. A DetailsView control appears as a form of recording and is provided by
multiple records as well as insert, update and delete record functions.
The DetailsView control supports the following features:
● Tabular rendering
● Supports column layout, by default two columns at a time
● Optional support for paging and navigation.
● Built-in support for data grouping
● Built-in support for edit, insert and delete capabilities

● FormView: The FormView was introduced with ASP.NET 2.0. The FormView control renders a
single data item at a time from a data source, even if its data source exposes a multiple records data
item from a data source. It allows for a more flexible layout when displaying a single record. The
FormView control renders all fields of a single record in a single table row. In contrast, the
FormView control does not specify a pre-defined layout for displaying a record. Instead, you
create templates that contain controls to display individual fields from the record. The template
contains the formatting, controls and binding expressions used to lay out the form. When using
templates, we can place any control such as a dropdown list, checkbox and we can even place
tables and rich controls like a GridView and so on. A FormView is a databound control used to
insert, display, edit, update and delete data in ASP.NET that renders a single record at a time. A
FormView control is similar to a DetailView in ASP.NET but the only difference is that a
DetailsView has a built-in tabular rendering whereas a FormView requires a user-defined template
to insert, display, edit, update and delete data.
The FormView control supports the following features:
● Template driven
● Supports column layout
● Built-in support for paging and grouping
● Built-in support for insert, edit and delete capabilities

*-*-*

You might also like