You are on page 1of 14

ADO .

NET

ADO.NET is the new database technology of the .NET (Dot Net)


platform, and it builds on Microsoft ActiveX Data Objects
(ADO).
ADO is a language-neutral object model that is the keystone of
Microsoft's Universal Data Access strategy.
ADO.NET is an integral part of the .NET Compact Framework,
providing access to relational data, XML documents, and
application data.
ADO.NET defines DataSet and DataTable objects which are
optimized for moving disconnected sets of data across intranets
and Internets, including through firewalls. It also includes the
traditional Connection and Command objects, as well as an object
called a DataReader that resembles a forward-only, read-only ADO
recordset.
You can use ADO.NET to access data by using the new .NET
Framework data providers which are:
Data Provider for SQL Server (System.Data.SqlClient)
Data Provider for OLEDB (System.Data.OleDb)
Data Provider for ODBC (System.Data.Odbc)
Data Provider for Oracle (System.Data.OracleClient)
The ADO.NET classes are found in System.Data.dll and are
integrated with the XML classes in System.Xml.dll

There are two central components of ADO.NET classes:


the DataSet, and the .NET Framework Data Provider.
Data Provider is a set of components including:
The Connection object
The Command object
The DataReader object
The DataAdapter object
DataSet object represents a disconnected cache of data which is
made up of DataTables and DataRelations that represent the
result of the command
ADO.NET
Data Provider
Object
DataSet
DataAdapter
Model
Data
Source

Connection

Command

DataReader

Connection Object
In ADO .NET, use the connection object to
Prepare the ConnectionString
Call Open() on the connection
Connection String
String of Key - Value pairs delimited by semicolon
The components of connection string are as follows
Data Source: Used by the connection object to identify the data
source and to establish the connection. Name or network address
of the instance
Integrated Security or Trusted_Connection: indicates the
current windows account credentials that are used for
authentication
Database or initial catalog: The name of database to establish
connection
User ID or uid: Indicates the name of the user
Password or pwd: Verifies the password settings

Connection Authentication
The Connection string is used to specify the connection
authentication
The two forms of authentication are as follows
SQL Server
Integrated windows
SQL Server authentication: Specifies user id and password in the
connection string
E.g. Data Source=SomeMachine;Initial
Catalog=pubs;uid=sa;pwd=***
Integrated windows authentication: Uses windows login
credentials
E.g. Data Source=SomeMachine;Database=pubs;Integrated
Security=True
Disconnecting
It is mandatory to close the connection after its use
Using Close() method of the connection object

Command Object
The command object is the core of ADO .NET data processing. It
can be used to process the SELECT, INSERT, UPDATE and DELETE
statements. These commands are executed after opening a
connection to the database
The types of commands are as follows
ExecuteReader: Returns a DataReader object as a stream of
data
ExecuteScalar: Returns a singleton value
ExecuteNonQuery: Returns the number of rows affected by the
SQL statements
Commands

ExecuteReader

ExecuteScalar

ExecuteNonQuery

DataReader
DataReader is also called a firehose cursor. It does not have a
public constructor. Only the classes in the same assembly
(System.Data) can access the constructor.
The features of the DataReader are as follows
Retrieves a read-only and forward-only stream of data
Creates command objects by using ExecuteReader method
Represents a live connection
Stores only one row at a time in memory
Results are stored in the network buffer of the client
Uses NextResult method for multiple result sets
To access the column values:
Use column ordinals myReader[0],myReader[1], and so on
Use column names myReader[EmpName]
The purpose of calling the Close() method is to
Fill the output and return parameters
Set the RecordsAffected property of the DataReader

DataAdapter
DataAdapters are used to communicate between the data source
and DataSet
The DataAdapter use
Connection object to connect a data source
Command object to retrieve and update data
The Fill method to populate data to the DataSet
The Update method help to send the changes to the database
E.g. Reading the DataSet
E.g. Mutiple Recordsets

DataSet
An in-memory representation of data provides a consistent
relational programming model by using the DataSet, which is
independent of the data source.
A DataSet, a resident of the System.Data namespace, is most
precisely defined as a provider-neutral, in-memory, and
disconnected relational data structure.
It provides support for the standard view, add, remove, and
update data operations for the data it represents, and it isnt
limited only to database data.
The DataSet is used to:
Track the changes made to its data
Store the relations between multiple tables by using the objects
such as DataTable and DataRelationCollection
Read and Write Extensible Markup Language (XML) files

DataSet Object Model


DataSet is composite of several components, includes:
DataSet
DataRelationCollection
DataTableCollection
DataTable
DataRowCollection
DataView

DataRow

Constraints
DataColumnCollection
PrimaryKey

DataColumn

DataView
A DataView provides a customized view of a single DataTable
E.g. DataView empView = new
DataView(empDS.Tables[EmpInfo])
The features of DataView are as follows
Allows multiple views for a single DataTable
Supports data binding on Win Forms and Web Forms
Accesses the DataTable through the DataTable.DefaultView
property
Controls the modifications by the AllowDelete, AllowNew, and
AllowEdit properties
Sort Property
The sort property takes a single or multiple columns with the ASC
or DESC
E.g. empView.Sort = EmpID,EmpName ASC;
Allow DefaultSort property
The AllowDefalutSort sorts the column in ascending order on the
primary key

DataView (Contd.)
Searching Property
The Search criteria passed to the methods is based on sort keys.
Method

Function

Find()

Returns an index of the row based on search criteria

FindRows()

Returns an array of rows

Filtering
The expressions with the WHERE clause are assigned to the filter
expression
Property

Description

RowFilter

Returns subset of rows based on a filter expression

RowStateFilter

Returns rows based on the RowState property

DataRelation
DataRelation defines the relationship between two different Data
Table objects. It relates Data Tables through DataColumns
E.g. DataColumn parentCol;
DataColumn childCol;
childCol = empDeptDS.Tables[EmpInfo].Columns[Deptno];
parentCol = empDeptDS.Tables[DeptInfo].Columns[Deptno];
DataRelation empdeptRel = new
DataRelation(EmpDept,parentCol,childCol);
empdeptDS.Relations.Add(empdeptRel);
Navigating Relationship
DataRelation allows navigation from one DataTable to another. It
retrieves rows of related tables by using DataRow.GetChildRows or
DataRow.GetParentRows

THANK YOU

You might also like