Session 11
ADO.NET - I
Exploring ASP.NET / Session 11 / 2 of 33
Review
An ASP.NET application is a collection of all the ASP.NET pages, .aspx
files, and various other files that are required to provide the essential
functionality of the application
When an instance of the HttpApplication class is created, a few events,
such as Application_Start, are fired. The event-handlers for these events
are stored in a file called Global.asax
The Application object is a built-in ASP.NET object, that represents an
instance of the ASP.NET application
In ASP.NET, variables can have two levels of scope:
Page-Level Variables
Object-level Variables
Object-level variables are of two types:
Application-level variables
Session-level variables
To ensure that application-level variables are not updated by more than
one user simultaneously, the Application object makes use of the Lock()
and UnLock() methods.
Exploring ASP.NET / Session 11 / 3 of 33
Review Contd…
The Server object acts as an interface to the HTTP service, and exposes
properties and methods of the HTTP server
The Server object has many methods that are used to control various
features of the web server. Some of these methods are:
Execute and Transfer()
HTMLEncode()
UrlEncode()
MapPath()
The Session object is used to store information about a user, that is
retained for the duration of the user session.
Exploring ASP.NET / Session 11 / 4 of 33
Objectives
Explain DataSets
Explain and use the .NET Data
Providers
Create a DataGrid
Explore DataReader
Exploring ASP.NET / Session 11 / 5 of 33
Data Connectivity in ADO.NET
Most databases can only maintain a small number of connections
simultaneously
Performance of the application is dependant on the number of
users
Web applications maintaining a continuous connection with a
database is not feasible, since it cannot be known when another
request for data will be made by the browser
When there are many users in an organization and two users need
to share the same data, then some means must be created by
which these users can pass the data back and forth
To tackle the above mentioned issues, Microsoft has created the
disconnected data architecture for ADO.NET.
Applications using ADO.NET connect to the database only to
retrieve or update data
Exploring ASP.NET / Session 11 / 6 of 33
.NET DATA Provider
CONNECTION
COMMAND
DATA ADAPTER
DATA READER
ADO.NET Object Model
DATASET
DATATABLE
DataRow
DataColumn
UniqueConstraint
ForeignKeyConstraint
DATABASE
Exploring ASP.NET / Session 11 / 7 of 33
DataSets
DataSet
<Dataset name>.Tables.Add(<datatable object name>);
Syntax for adding DataTable
DataSet is an object, in which the data retrieved from the database can be
stored. The DataSet can contain one or more tables, and information about the
relationships and constraints.
Exploring ASP.NET / Session 11 / 8 of 33
DataSets Example
<%@ Page Debug = "true" %>
<%@ Import namespace="System.Data" %>
<html>
<title> DataTable </title>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
Response.Write("<center><b><u>Data
Table</center></b></u> <br>");
DataSet myds = new DataSet();
DataTable mydt = new DataTable ("Squares");
DataRow mydr;
mydt.Columns.Add(new DataColumn
("Numbers",typeof(Int32)));
Exploring ASP.NET / Session 11 / 9 of 33
DataSets Contd…
mydt.Columns.Add(new DataColumn("Squares",typeof(Int32)));
for (int i = 0; i < 10; i++)
{
mydr = mydt.NewRow();
mydr[0] = i;
mydr[1] = i * i;
mydt.Rows.Add(mydr);
}
myds.Tables.Add(mydt);
dgMyGrid.DataSource = myds.Tables ["Squares"].
DefaultView;
dgMyGrid.DataBind();
}
</script>
Exploring ASP.NET / Session 11 / 10 of 33
DataSets Output
<form runat="server">
<center>
<asp:DataGrid id="dgMyGrid" runat= "server"/>
</center>
</form>
</html>
Exploring ASP.NET / Session 11 / 11 of 33
.NET Data Providers
The .NET data provider contains objects that provide access to
data from various data sources
The data sources can either be based on the databases, or a
non-database data stores like XML and Excel Spreadsheets.
.NET Data Providers perform all the interactions, such as,
retrieval, insertion, updation and deletion of data, required
between the DataSet and the database
Establish connection with
the database
Retrieve and manipulate data in
the database
Exploring ASP.NET / Session 11 / 12 of 33
Types of .NET Data Providers
The four types of .NET Data Providers
available are
.NET Data Provider for SQL Server
.NET Data Provider for OLE DB
.NET Data Provider for ODBC
.NET Data Provider for Oracle
Exploring ASP.NET / Session 11 / 13 of 33
Connection Objects
<%@Import
Namespace="System.Data.OleD
b" %>
<%@ Import
Namespace="System.Data.
SqlClient" %>
<%@ Import
Namespace="System.Data" %>
<%@ Import
Namespace="System.Data" %>
OLE DB .NET Data Provider SQL .NET Data Provider
In order to get the required classes for data access, it is
necessary to first import the following namespaces