0% found this document useful (0 votes)
11 views7 pages

Chapter 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Chapter 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 5

Data Access With ADO.Net Object

ADO.NET Introduction
It is a module of .Net Framework which is used to establish connection between application
and data sources. Data sources can be such as SQL Server and XML. ADO.NET consists of
classes that can be used to connect, retrieve, insert and delete data.
All the ADO.NET classes are located into System.Data.dll and integrated with XML classes
located into System.Xml.dll.
ADO.NET has two main components that are used for accessing and manipulating data are the
.NET Framework data provider and the DataSet.

Data Providers
The .Net Framework includes mainly three Data Providers for ADO.NET. They are the Microsoft
SQL Server Data Provider , OLEDB Data Provider and ODBC Data Provider . SQL Server uses the
SqlConnection object , OLEDB uses the OleDbConnection Object and ODBC uses
OdbcConnection Object respectively.

A data provider contains Connection, Command, DataAdapter, and DataReader objects. These
four objects provides the functionality of Data Providers in the ADO.NET.
Connection
The Connection Object provides physical connection to the Data Source. Connection object
needs the necessary information to recognize the data source and to log on to it properly, this
information is provided through a connection string.
The Command Object uses to perform SQL statement or stored procedure to be executed at the
Data Source. The command object provides a number of Execute methods that can be used to
perform the SQL queries in a variety of fashions.
DataReader
The DataReader Object is a stream-based , forward-only, read-only retrieval of query results
from the Data Source, which do not update the data. DataReader requires a live connection
with the databse and provides a very intelligent way of consuming all or part of the result set.
DataAdapter
DataAdapter Object populate a Dataset Object with results from a Data Source . It is a special
class whose purpose is to bridge the gap between the disconnected Dataset objects and the
physical data source.
DataSet

DataSet provides a disconnected representation of result sets from the Data Source, and it is
completely independent from the Data Source. DataSet provides much greater flexibility when
dealing with related Result Sets.
DataSet contains rows, columns,primary keys, constraints, and relations with other DataTable
objects. It consists of a collection of DataTable objects that you can relate to each other with
DataRelation objects. The DataAdapter Object provides a bridge between the DataSet and the
Data Source.

The SqlDataSource Control


The SqlDataSource control represents a connection to a relational database such as SQL
Server or Oracle database, or data accessible through OLEDB or Open Database Connectivity
(ODBC). Connection to data is made through two important properties ConnectionString and
ProviderName.
The following code snippet provides the basic syntax of the control:
<asp:SqlDataSource runat="server" ID="MySqlSource"
ProviderName='<%$ ConnectionStrings:LocalNWind.ProviderName %>'
ConnectionString='<%$ ConnectionStrings:LocalNWind %>'
SelectionCommand= "SELECT * FROM EMPLOYEES" />

<asp:GridView ID="GridView1" runat="server" DataSourceID="MySqlSource" />

Configuring various data operations on the underlying data depends upon the various
properties (property groups) of the data source control.
The following table provides the related sets of properties of the SqlDataSource control, which
provides the programming interface of the control:

roperty Group Description

DeleteCommand, Gets or sets the SQL statement, parameters, and type for
deleting rows in the underlying data.
DeleteParameters,
DeleteCommandType

FilterExpression, Gets or sets the data filtering string and parameters.


FilterParameters

InsertCommand, Gets or sets the SQL statement, parameters, and type for
inserting rows in the underlying database.
InsertParameters,
InsertCommandType

SelectCommand, Gets or sets the SQL statement, parameters, and type for
retrieving rows from the underlying database.
SelectParameters,
SelectCommandType

SortParameterName Gets or sets the name of an input parameter that the


command's stored procedure will use to sort data.

UpdateCommand, Gets or sets the SQL statement, parameters, and type for
updating rows in the underlying data store.
UpdateParameters,
UpdateCommandType

The following code snippet shows a data source control enabled for data manipulation:

<asp:SqlDataSource runat="server" ID= "MySqlSource"


ProviderName='<%$ ConnectionStrings:LocalNWind.ProviderName %>'
ConnectionString=' <%$ ConnectionStrings:LocalNWind %>'
SelectCommand= "SELECT * FROM EMPLOYEES"
UpdateCommand= "UPDATE EMPLOYEES SET LASTNAME=@lame"
DeleteCommand= "DELETE FROM EMPLOYEES WHERE EMPLOYEEID=@eid"
FilterExpression= "EMPLOYEEID > 10">
.....
.....
</asp:SqlDataSource>

Data Bound Controls


The data bound controls used in ASP.NET to display data in various forms and do various
database activities such as Add, Edit, Update and Delete operations. The control makes the data
more organized and presents the data in an efficient way for the viewers.
The standard ASP.NET data presentation controls are:
 DataList
 DetailsView
 FormView
 GridView
 ListView
 Repeater

Repeater Control
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
 No default output
 More control and complexity
 Item as row
 Paging, Sorting and Grouping requires custom code writing
 only Web control that allows you to split markup tags across the templates
 no built-in selection capabilities
 no built-in support for edit, insert and delete capabilities
 no built-in support for paging, sorting and grouping capabilities
 no built-in layout or styles, need to declare all layout, formatting and style tags explicitly
within the control's templates
 Strictly emits the markup specified in its templates, nothing more and nothing less.

DataList Control
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
 Directional rendering
 Good for columns
 Item as cell
 Updatable
 Control over Alternate item
 Paging function needs handwriting.
GridView Control
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

DetailsView control
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 control
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