ASP.
NET Database
Programming
Contents
Basics of ADO.NET
ADO.NET Object Model
Managed Providers
Dataset Class
ADO.NET Basics
• Microsoft ADO.NET is improvement after ADO (ActiveX Data
Objects)
• ADO.NET provides platform interoperability and scalable
data access
• In the .NET Framework, data is transmitted in the Extensible
Markup Language (XML) format.
• Therefore, any application that can read the XML format can
process data.
Application 1 XML Application 2
ADO.NET Basics
Drawback of ADO in Data Access
• ADO Model uses the concepts of recordsets (similar to tables
and views)
• In distributed web applications, data needs to be exchanged from
different components at different tiers
• They also might run on variety of platforms
• The format of data being exchanged should be understood by
all the components
Data Transfer
Application 1 Not in common
Application 2
Oracle format SQL Server
ADO.NET Basics
Drawback of ADO in Data Access
• Transmission of data requires the conversion of data types
that are recognized by the receiving components.
• This conversion is called COM marshalling.
• Interoperability is limited when using ADO recordsets.
What ADO.NET do?
• Like ADO, ADO.NET also allows you to access data when
disconnected from actual data sources
• Unlike ADO, ADO.NET uses XML as the data format
• Since XML is a universal data format, ADO.NET expands the
boundaries of interoperability to the Internet
ADO.NET Basics
What ADO.NET do?
• Instead of recordsets, ADO.NET uses the DataSet and
DataReader objects to access and manipulate data.
Features of ADO.NET
Interoperability
Maintainability
Programmability
Performance
Scalability
Disconnected Architecture in ADO.NET
Laptop Charging
DataSet
Database
Authors
Connection Authors
Authors
DataAdapter
Select … from Authors
15
DataSet
Database
Authors
Connection Publishers
Publishers
DataAdapter
Publishers
Select … from Publishers
Connected and Disconnected Model
ADO.NET Object Model
• For the .NET applications, the primary data access
technology to be used would be ADO.NET
• The ADO.NET Object Model is primarily divided into two
levels:
Connected Layer: Consists of classes with the
Managed Providers
Disconnected Layer: Used with DataSet
Managed Providers
• Collection of classes in the .NET Framework that provide
a foundation for the ADO.NET programming model
• In the .NET Framework, the OLE DB and ADO layers are
merged into one layer.
• This results in high performance, and at the same time
allows components to be called from any language
• The Managed Data Providers include classes that can be
used for the following:
Accessing data from SQL Server 7.0
Accessing the other OLE DB providers
Managed Providers for OLEDB
• System.Data.OleDB namespace includes classes allows
you to access OLE DB data sources.
Managed Providers for OLEDB Data Sources
Managed Providers for OLEDB
Managed Providers for SqlServer
• System.Data.SqlClient namespace includes classes allows
you to access data stored on SQL Server
Managed Providers for SqlServer
Managed Providers for SqlServer
Managed Providers for SqlServer
How fill Authors Table into the DataSet? –
Through Code
DataSet
Database
Authors
2 Connection Authors
Authors
1
DataAdapter 2
Select … from Authors 3
How fill Authors Table into the DataSet? – Through Code
connection = New SqlConnection
("server=localserver; uid=sa; pwd=; database=Sales")
command = New SqlDataAdapter
("SELECT * FROM Authors Where AuthorId=@ID", connection)
param1 = New SqlParameter ("@ID", SqlDbType.Int)
param1.Value = 2
command.SelectCommand.Parameters.Add(param1)
dataset = New DataSet()
command.Fill(dataset, “Authors")
How fill Dept, Emp Table into the DataSet? –
Through Code
D1
Dept
DataSet 1
Dept 2
Connection
DataAdapter
Emp D2
Emp
e1 19 1
e2 23 2
e3 32 1
How fill Authors Table into the DataSet? – Through Code
connection is a SqlConnection class object that represents a connection to
the SQL Server database.
command is a SqlDataAdapter class object that represents a set of data
commands and a database connection.
param1 is a SqlParameter class object that represents the parameter to be
passed in the T-SQL command.
dataset is a DataSet class object that represents the DataSet that is filled by
the query results.
DataSet Class
• The DataSet comprises the Disconnected Layer of ADO.NET.
• The DataSet consists of a local buffer of tables and relations.
• A DataSet Contains collection of DataTables (Tables Collection)
• A DataTable represents one table of in-memory data
• A DataTable consists of
• DataColumn (Columns Collection)
• DataRow (Rows Collection)
• A DataTable remembers the original state along with the current state,
tracking the kinds of changes that have occurred.
• The data access classes are included in the System.Data namespace
DataSet Class
Dataset
Tables Relations
(Collection) (Collection)
Table Relation
Columns Rows Constraints
(Collection) (Collection) (Collection)
Column Row Constraint
Data Access Classes in ADO.NET
Class Description
DataSet Represents a complete collection of tables, relationships, and
constraints.
DataAdapter Represents a database query or stored procedure that is used
to populate the DataSet object.
DataTable Represents a data source that stores data in row and column
format.
DataColumn Represents a column in a DataTable.
DataRow Represents a row in a DataTable.
ADO vs ADO.NET
Feature ADO ADO.NET
Memory resident data Uses the RecordSet Uses the DataSet object,
representation object
Relationship between Requires a JOIN query Provides the DataRelation
multiple tables object without a Join Query
Data navigation Scans the rows Uses a navigation model for
sequentially nonsequential access to
rows in a table. Tracks
relationships to navigate
from rows in one table to
corresponding rows in
another table.
ADO vs ADO.NET
Feature ADO ADO.NET
Disconnected access Supports the connected ADO.NET uses
access. However, ADO standardized calls for the
also supports the DataSetCommand object to
disconnected data access communicate with a
by the RecordSet object database, which in turn
although it is not communicates with the
designed for it. OLE DB provider.
Sometimes, the DataSet
object directly
communicates with the
APIs provided by a
database management
system.
ADO vs ADO.NET
Feature ADO ADO.NET
Programmability Uses the Connection object Uses the typed
to transmit commands for programming characteristic
mapping a data source that of XML
has an underlying data
construct.
Sharing Uses COM marshalling Data Transmits a DataSet with an
disconnected data types that are defined by XML file. The XML format
between tiers or COM will share disconnected places no restrictions on
components data between components. data`types. ADO.NET
Hence, ADO performs data requires no data type
type conversions, which conversions, resulting in
results in low performance. improved performance.
ADO vs ADO.NET
Feature ADO ADO.NET
Transmitting data Problematic, because Supported because
through firewalls firewalls are typically ADO.NET use XML for
configured to prevent representing data. Using
system-level requests, such HTTP, XML data can pass
as COM marshalling. through firewalls
Scalability Database locks and active Disconnected access to
database connections for long database data without
durations result in limited retaining database locks or
database resources, allowing active database connections
fewer users to access data for lengthy periods does not
simultaneously. limit the database resources.
This allows more users to
access data simultaneously.
Communicating with OLEDB Data Sources Using ADO.NET
• To connect SQL Server, classes in the
System.Data.SqlClient namespace are used.
• To connect to OLE DB data sources, classes in the
System.Data.OleDb namespace are used.
• Both the SQL Server and OLE DB providers are
managed providers.
• These providers act as a thin layer that connects the
application to the database without adding any
unnecessary overhead, such as converting from OLE
DB-compatible data types to native SQL Server data
types and vice versa when communicating between the
client and the server.
Communicating with OLEDB Data Sources Using ADO.NET
• The SQL Server does not depend on OLE DB/ODBC.
• Instead, it uses the Tabular Data Stream (TDS)
protocol of SQL Server to natively communicate with the
SQL Server.
• The use of the TDS provides a tremendous performance
boost to applications.
System.Data.OleDb.OleDbConnection class
• This class encapsulates the connection to an OLE DB
data source.
• Applications that need to use an OLE DB provider to
connect to the data source should use this class
• When the .NET Framework is shipped by Microsoft, it
provides managed data providers for some of the popular
DBMSs like MS Access and SQL Server.
• But, the data providers for the other DBMSs will be
developed by the respective vendors.
System.Data.OleDb.OleDbConnection class
Access
Access
ASP.NET Connection Database
ASP.NET Database
Application
Application
System.Data.OleDb.OleDbConnection class
page_Load()
DB_Connect();
Response.write(“Connection Established”)
void DB_Connect()
{
cn = new OleDbConnection("Provider=Microsoft.
Jet.OLEDB.4.0;Data Source=C:\ADODemo\
Employee.mdb")
cn.Open()
}
System.Data.OleDb.OleDbConnection class
page_Load() Error Handling Code
try
{
DB_Connect()
Response.write(“Connection Established”)
}
catch(Exception ex)
{
Response.write(“Exception Error” + ex.Message)
}
finally
{
cn.close() Connection Close
}
System.Data.OleDb.OleDbConnection class
System.Data.OleDb.OleDbCommand class
• This class encapsulates the commands that need to be sent to
the OLE DB data source.
• Applications use the OleDbCommand class to create select,
insert, update, and delete commands that need to be sent to
the data source.
• This class can be used to execute stored procedures besides
sending input parameters to the stored procedure
System.Data.OleDb.OleDbCommand class
System.Data.OleDb.OleDbCommand class
Insert_Click
//Connect to Database
cn = New OleDbConnection("Provider=Microsoft.
Jet.OLEDB.4.0;Data Source=C:\ADODemo\Employee.mdb")
cn.Open()
//Retrieve Form Values
String sID, sFName, sLName, sAge, sSQL;
sID = txteID.Text
sFName = txtFName.Text
sLName = txtLName.Text
sAge = txtAge.Text
System.Data.OleDb.OleDbCommand class
//Make the insert query
sSQL = “INSERT INTO employees values(" & sID & ",'" &
sFName & "','" & sLName & "'," & sAge & ")"
//Make the OleDbCommand object
cmdInsert = New OleDbCommand(sSQL, cn)
//Execute the query
cmdInsert.ExecuteNonQuery()
response.write ("Data recorded!")
End Sub
System.Data.OleDb.OleDbDataReader class
Equivalent to a forward-only, read-only Recordset object in
ADO.
This class is useful to all applications that want to retrieve
data returned from a query to the database and want to
process one record at a time.
A classic example for this would be to populate a list box
with values retrieved from, say, a master table.
System.Data.OleDb.OleDbDataReader class
Display_Click
//Connect to Database
cn = New OleDbConnection("Provider=Microsoft.
Jet.OLEDB.4.0;Data Source=C:\ADODemo\Employee.mdb")
cn.Open()
//Retrieve Form Employee Table Values
String sSQL;
sSQL = “SELECT * FROM employees"
//Make the OleDbCommand object
cmdSelect = New OleDbCommand(sSQL, cn)
OleDbDataReader
System.Data.OleDb.OleDbDataReader class
//This query should return an OleDbDataReader so we use the
//ExecuteReader method
drEmp = cmdSelect.ExecuteReader()
sbResults.Append ("<Table>")
do while drEmp.Read()
{
sbResults.Append ("<TR><TD>")
sbResults.Append ( drEmp.GetInt32(0).ToString())
sbResults.Append ("</TD><TD>")
sbResults.Append ( drEmp.GetString(1))
sbResults.Append ("</TD><TD>")
System.Data.OleDb.OleDbDataReader class
sbResults.Append ( drEmp.GetString(2))
sbResults.Append ("</TD><TD>")
sbResults.Append ( drEmp.GetInt32(3).ToString())
sbResults.Append ("</TD><TR>")
loop
sbResults.Append ("</Table>")
lblResult.text = sbResults.ToString()
HTML Part
<body>
<h3><font face="Verdana">Employee Details</font></h3>
<p></p>
<asp:label id="lblResult" runat="server" text=""/>
</body>
System.Data.DataSet, System.Data.DataTable,
System.Data.DataRow, and System.Data.DataColumn classes
• DataSet is a generic class provided by the .NET
Framework.
• This class is useful on the client side to store data in a
manner that is much more functional and powerful than
the ADO Recordset object.
• Data in a DataSet is in XML format, makes it very well
suited to Web applications, and makes cross-platform access
possible
• The tables are stored in DataTable objects, and
DataRelation objects represent the relationship between
tables.
• The rows and columns in a table are stored in DataRow and
DataColumn objects
System.Data.DataSet, System.Data.DataTable,
System.Data.DataRow, and System.Data.DataColumn classes
DataSet
Database
EMP
2 OleDBConnection Emp
3 Emp
1
OleDBDataAdapter 2
Select … from Emp 3
Filling data into DataSet and Display data using DataTable
cn = New SqlConnection oleDBConnection
("server=localserver; uid=sa; pwd=; database=Sales")
cmd = New SqlDataAdapter oleDBDataAdapter ("SELECT
* FROM Employee Where EmpId<>@ID", cn)
param1 = New SqlParameter oleDBParameter("@ID",
SqlDbType.Int)
param1.Value = 2
cmd.SelectCommand.Parameters.Add(param1)
dataset = New DataSet()
cmd.Fill(dataset, “Emp")
Filling data into DataSet and Display data using DataTable
dtEmp DataTable;
drEmp DataRow;
dcEmp DataColumn
sbResult = new stringbuilder()
' Iterate through all the DataTables in the DataSet
For Each dtEmp in myDataSet.Tables
sbResult.Append("<Table>")
‘Iterate through all the DataRows in the DataTable
For Each drEmp In dtEmp.Rows
sbResult.Append("<TR>")
' Iterate tall the DataColumns in the DataRow
For Each dcEmp in dtEmp.Columns
Filling data into DataSet and Display data using DataTable
sbResult.Append("<TD>")
sbResult.Append(drEmp(dcEmp))
sbResult.Append("</TD>")
Next dcEmp
sbResult.Append("</TR>")
Next drEmp
sbResult.Append("</Table>")
Next dtEmp
lblResult.Text = sbResult.ToString()
Recap for End Semester
Recap for End Semester
Portion : Unit I, II, III & V
Test Dates : 17.11.20 – 23.11.20
CIA Publication : 26.11.2020
Semester Exams : 30.11.2020
Unit Test IV : 03.11.2020 (12:50 to 01.05 p.m.)
Portion: Unit V [10 Marks]
Assignment V & VI : 03.11.2020 [10 Marks]
Recap for End Semester
Assignment Demo Dates (May Change)
Group Date
Team A (18UCS108) 07.11.2020 (D Day)
Team B (18UCS131) 10.11.2020 (F Day)
Team C (18UCS104)
Team D (18UCS110) 11.11.2020 (A Day)
Team E (18UCS154)
Recap for End Semester
End Semester Exam Pattern
Time: 2 Hours (60 Marks)
Section – A: 8 x 2 = 16 (No choice)
Section – B: 4 x 5 = 20 (Either or)
Section – C: 3 x 8 = 24 (3 out of 4)
Course Outline
Unit I
• Client / Server Architecture
• Understanding .NET framework
Unit II
• Getting Started with ASP.NET
Unit III
• Forms with Web Controls
Unit IV
• Rich Web Controls
Unit V
• ADO.NET
Unit I
Application Types
• Dektop Applications
• Web Applications
• Mobile Applications
Introduction to Web Development
• Tim Berners Lee
• WWW
• Hypertext Linking
• Web Page, URL, Website
• Web Browser, Web Server
• HTML Documents
• Web Communication Protocols – HTTP, IP Address,
TCP/IP, HTTPS
Unit I
Introduction to Web Development
• Web Hosting
• ISP
• Domain Name Registration
Client/Server Architecture
• Tier
• Single Tier – Advantages, Disadvantages
• Two Tier – Server, Client, Advantages, Disadvantages
• Three Tier – Why we need to move?
• Client (UI)
• Business Logic
• Back End (Data Tier)
• Multi Tier
Unit I
.NET Enterprise Vision
• .NET Framework
• Two Core Components – CLR, Base Class Library
• Managed Code – Intermediate Language (IL)
• Multiple Language Support
• Common Type System (CTS)
• Common Language Specification (CLS)
• Assemblies
Unit I
Visual Studio .NET
C++ C# VB Perl J# …
Common Language Specification
ASP .NET Windows
Web Forms Web Services
Mobile Internet Toolkit
Forms
ADO .NET and XML
.NET Framework (Base Class Library)
Common Language Runtime
Operating System
Unit I
Code Compilation and Execution
Compilation
Also called
Source Language Code
Assembly
Code Compiler MSIL
Metadata (.EXE or
.DLL file)
Before
installation or
the first time
Execution each method is
called
Native JIT
Code Compiler
Unit I
Visual Studio .NET - Snapshot
Unit I
34
Unit I
Benefits of .NET Framework
1. Less Coding and Increased Reuse of Code
2. Deployment
3. Reliability
4. Security
5. Use across Platforms & Languages
6. Use of Service Oriented Architecture
7. Integration with Legacy Systems
Unit II
ASP.NET
ASPX, ASP – side by side
• ASP.NET vs ASP Simplified programming
model
• Features Simplified deployment
Better performance
Caching
Security
Powerful controls
AJAX
Simplified form validation
Code behind pages
More powerful data access
Web services
Better session management
Unit II
ASP.NET
• NameSpace – Imports / Using - System
• Stateless Web Paradigm – Static Web Sites, Dynamic
Websites
• Code Behind – VB.NET, C#.NET
Page Life Cycle
• Page Request
• Starting of Page Life Cycle
• Page Initialization
• Page Load
• Validation
• Post back Event handling
• Page Rendering & Unload
Unit II
Execution Model
Source Visual Basic C# C++
code
Unmanaged
Compiler Compiler Compiler
Component
Managed Assembly Assembly Assembly
code IL Code IL Code IL Code
Common Language Runtime
JIT Compiler
Native Code
National University of Mongolia
Operating System Services
Unit II
ASP.NET Objects
• Server
• Request
• Response
• Session
• Application
• Global.asax
Creating an ASP.NET Application
Unit II
Deploying ASP.NET Web Application
ASP.NET
Create Setup Deploy in IIS
Web
Project Server
Application
Techniques for
Deployment
• XCOPY
Deployment Accessing
• Copying a Website from
Website Browser
• Creating a set up
project
Unit III
ASP.NET Web Forms
• Two Parts
• a) Front End (.aspx)
• b) Code Behind (.aspx.cs)
Web Form Server Controls
• HTML Server Controls
• ASP.Net Server Controls (Id, RunAt)
• Validation Controls
• User Controls
• Mobile Controls
Unit III
ASP.NET vs. PHP
Feature PHP ASP.NET
HTML Yes Yes
CSS Yes Yes
Server Controls No Yes
Javascript Yes Yes + Validation controls
Database Conn Yes Yes
Cookies & Sessions Yes Yes
VIEWSTATE No Yes
POSTBACK No Yes
Unit III
Server Controls
• Label
• TextBox
• Checkbox and CheckBoxList
• Radiobutton and RadioButtonList
• ListBox
• DropDownList Control
• HyperLink Control
• Table Control
• Image Control
• Button, Link Button, Image Button
Validation Controls
• RequiredField
• Range
• Regular Expression
• Compare
• Custom Validator
• Summary
Unit III
Working with Events
• Event Handlers
• With Events
• Handles
Unit V
ADO.NET Basics
• Drawback in ADO
• ADO.NET uses XML for interoperability
• Features of ADO.NET
Disconnected Architecture in ADO.NET
Connection – Data Adapter
ADO.NET Object Model
• Connected and Disconnected Layer
• Managed Data Providers
• SQL Data Provider (System.Data.SqlClient)
• OLEDB Data Provider (System.Data.OleDB)
• Code – How to fill dataset?
sqlConnection – sqlAdapter – sqlParameter –
DataSet
Unit V
DataSet Class
Dataset
Tables Relations
(Collection) (Collection)
Table Relation
Columns Rows Constraints
(Collection) (Collection) (Collection)
Column Row Constraint