Académique Documents
Professionnel Documents
Culture Documents
ADO.NET
.NET Data Access and Manipulation
Overview
What is ADO.NET?
ADO.NET Architecture
ADO.NET Core Objects
Disconnected vs. connected data access models
Steps of Data Access
Entity Framework?
What is ADO.NET?
ADO Stands for ActiveX Data Object
data-access technology that enables applications to connect to
data stores and manipulate data contained in them in various
ways
Provides a bridge between the front end controls and the
back end database.
What is ADO.NET?
The data access classes for the .NET framework
Designed for highly efficient data access
Tight integration with XML
Support both connected and disconnected data architecture
Ability to combine data from multiple and varied data
sources
What is ADO.NET?
An object oriented framework that allows you to interact
with database systems
ADO.NET Architecture
.NET Data Providers
SQL .NET
Data Provider
SQL SERVER
OLE DB System.Data.OleDb
ODBC System.Data.Odbc
Oracle System.Data.OracleClient
ADO.NET Core Objects
Object Description
11 4/14/2019
Connected Data Access Model
Cont.…
In Connection Oriented Data Access Architecture the application
makes a connection to the Data Source and then interact with it
through SQL requests using the same connection.
In this case the application stays connected to the database system even
when it is not using any Database Operations.
On the other hand the disconnected approach makes no attempt to
maintain a connection to the data source.
13 4/14/2019
Cont.…
In Connection Oriented Data Access model , when you read data from a
database by using a Data Reader object, an open connection must be
maintained between your application and the Data Source.
14 4/14/2019
Steps of Data Acces : Connected
Environment
Create connection
Create command (select-insert-update-delete)
Open connection
If SELECT -> use a DataReader to fetch data
If UPDATE,DELETE, INSERT -> use command object’s
methods
Close connection
Connection Object
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 Connection object represents a connection to the data source.
The connection could be shared among different command objects.
16 4/14/2019
Command
The Command object represents the command or a stored
procedure sent to the database for retrieving or manipulating data.
The command object provides a number of Execute methods that
can be used to perform the SQL queries in a variety of fashions.
17 4/14/2019
DataReader Object
The DataReader object is an alternative to the DataSet and
DataAdapter combination.
This object provides a connection oriented access to the data
records in the database.
These objects are suitable for read-only access, such as populating a
list and then breaking the connection.
DataReader requires a live connection with the databse and
provides a very intelligent way of consuming all or part of the
result set.
18 4/14/2019
Data Adaptor
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.
The DataAdapter object acts as a mediator between the DataSet
object and the database.
This helps the Dataset to contain data from multiple databases or
other data source.
19 4/14/2019
DataSet Class
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.
20 4/14/2019
Cont.…
The dataset represents a subset of the database.
It does not have a continuous connection to the database.
To update the database a reconnection is required.
The DataSet contains DataTable objects and DataRelation objects.
The DataRelation objects represent the relationship between two tables.
21 4/14/2019
Cont.…
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.
22 4/14/2019
static void Main()
{
string connectionString =
Properties.Settings.Default.connStr;
string queryString = "SELECT CategoryID, CategoryName FROM
dbo.Categories;";
SqlConnection connection = new
SqlConnection(connectionString);
EXAMPLE
SqlCommand command = new SqlCommand(queryString,connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("\t{0}\t{1}“,reader[0],reader[1]);
}
reader.Close();
connection.close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Connected – Update, Delete, Insert
Command class core methods:
ExecuteNonQuery : Executes a SQL statement
against a connection object
ExecuteReader: Executes the CommandText against
the Connection and returns a DbDataReader
ExecuteScalar: Executes the query and returns the
first column of the first row in the result set returned
by the query
Connected – Update, Delete, Insert
string connString =
Properties.Settings.Default.connStr;
SqlConnection conn = new
SqlConnection(connString);
SqlCommand cmd = new SqlCommand("delete from
Customers" + "where custID=12344", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
You can keep any other variable to reach at runtime using this
technique
Disconnected Data Access Model
Cont.…
Disconnected Data Access Architecture is more flexible and powerful
than Connection Oriented Data Access.
ADO. Net provides a new solution by introduce a new component called
Dataset.
The DataSet is the central component in the ADO.NET Disconnected
Data Access Architecture.
A DataSet is an in-memory data store that can hold multiple tables at the
same time.
29
Cont.…
DataSets only hold data and do not interact with a Data Source.
One of the key characteristics of the DataSet is that it has no knowledge
of the underlying Data Source that might have been used to populate it.
30
Cont.…
Unlike the DataReader, the DataSet is not connected directly to a Data
Source through a Connection object when you populate it.
It is the DataAdapter that manages connections between Data Source
and Dataset by fill the data from Data Source to the Dataset and giving
a disconnected behavior to the Dataset.
The DataAdapter acts as a bridge between the Connected and
Disconnected Objects.
31 4/14/2019
Cont.…
32 4/14/2019
Steps of Data Access: Disconnected
Environment
Defining the connection string
Defining the connection
Defining the command
Defining the data adapter
Creating a new DataSet object
SELECT -> fill the dataset object with the result of the
query through the data adapter
Reading the records from the DataTables in the datasets
using the DataRow and DataColumn objects
UPDATE, INSERT or DELETE -> update the database
through the data adapter
using System;
using System.Data;
using System.Data.SqlClient;
namespace SampleClass
{
class Program
{
static void Main(string[] args)
EXAMPLE
{
string connStr =
Properties.Settings.Default.connStr;
SqlConnection conn = new SqlConnection(connStr);
string queryString = "SELECT * from titles;";
SqlDataAdapter da = new
SqlDataAdapter(queryString,conn);
DataRow dr = ds.Tables[0].Rows[0];
dr.Delete(); DELETE
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
DataRow dr = ds.Tables[0].Rows[0];
dr["CustomerName"] = "John";
UPDATE
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
DataRow dr = ds.Tables[0].NewRow();
dr["CustomerName"] = "John";
dr["CustomerSurName"] = "Smith"; INSERT
ds.Tables[0].Rows.Add(dr);
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
Pros and Cons
Connected Disconnected
Database Resources - +
Network Traffic - +
Memory Usage + -
Data Access - +
Reading Assignment
To minimize the code written by developers new UI tools
and objects have been intoduced with .NET Framework 3.5.
What is Entity Framework?
The end