ADO
ADO
NET
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
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()
FindRows()
Filtering
The expressions with the WHERE clause are assigned to the filter
expression
Property
Description
RowFilter
RowStateFilter
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