You are on page 1of 43

ADO.

NET:
3.2 Overview of the ADO.NET
ADO.NET is a set of classes provides a rich set of
components for creating distributed, data-sharing applications.
ADO.NET is an integral part of the Microsoft .NET
Framework.
All ADO.NET classes are located at the System.Data
namespace .
When compiling code that uses the System.Data namespace,
reference both System.Data.dll and System.Xml.dll.
Basically speaking, ADO.NET provides a set of classes to
support you to develop database applications and enable you
to connect to a data source to retrieve, manipulate and update
data with your database.

7/20/2017 2
The classes provided by ADO.NET are core to develop a
professional data-driven application and they can be
divided into the following three major components:

Data Provider
DataSet
DataTable
These three components are located at the different
namespaces. The DataSet and the DataTable classes are
located at the System.Data namespace. The classes of
the Data Provider are located at the different
namespaces based on the types of the Data Providers.

7/20/2017 3
.NET Data Provider

Connection

Command SelectCommand

DataReader InsertCommand
DeleteCommand
DataAdapter
UpdateCommand

DataSet DataColumn
DataTable DataRow
DataRelation DataConstraint
Data Provider contains four classes: Connection, Command,
DataAdapter and DataReader. These four classes can be used
to perform the different functionalities to help you to:
Set a connection between your project and the data
source using the Connection object
Execute data queries to retrieve, manipulate and update
data using the Command object
Move the data between your DataSet and your database
using the DataAdapter object
Perform data queries from the database (read-only) using
the DataReader object

7/20/2017 5
The DataSet class can be considered as a table container and
it can contain multiple data tables. These data tables are only a
mapping to those real data tables in your database. But these
data tables can also be used separately without connecting to
the DataSet. In this case, each data table can be considered as
a DataTable object.
The DataSet and DataTable classes have no direct relationship
with the Data Provider class; therefore they are often called Data
Provider-independent components.
Four classes such as Connection, Command, DataAdapter and
DataReader that belong to Data Provider is often called Data
Provider-dependent components.

7/20/2017 6
The Architecture of ADO.NET
The ADO.NET architecture can be divided into two logical pieces:
command execution and caching.
Command execution requires features like connectivity,
execution, and reading of results. These features are
enabled with ADO.NET Data Providers.
Caching of results is handled by the DataSet.

7/20/2017 7
3.4 The Components of ADO.NET

3.4.1 The Data Provider


The Data Provider can also be called a data driver and it can be
used as a major component for your data-driven applications. The
functionalities of the Data Provider, as its name means, are to:

Connect your data source with your applications


Execute different methods to perform the associated data
query and data accessing operations between your data
source and your applications
Disconnect the data source when the data operations are
done

7/20/2017 8
3.4.1 The Data Provider
The Data Provider is physically composed of a binary library
file and this library is in the DLL file format. Sometimes this
DLL file depends on other DLL files, so in fact a Data
Provider can be made up of several DLL files. Based on the
different kinds of databases, Data Provider can have several
versions and each version is matched to each kind of
database. The popular versions of the Data Provider are:
Open DataBase Connectivity (Odbc) Data Provider
(ODBC.NET)
Object Linking and Embeding DataBase (OleDb) Data
Provider (OLEDB.NET)
SQL Server (Sql) Data Provider (SQL Server.NET)
Oracle (Oracle) Data Provider (Oracle.NET)

7/20/2017 9
3.4.1 The Data Provider
The different data providers are located at the different namespaces, and
these namespaces hold the various data classes that you must import
into your code in order to use those classes in your project.
Table 3-1 lists the most popular namespaces used by the different data
providers and used by the DataSet and the DataTable.

Table 3-1. Namespaces for different Data Providers, DataSet and DataTable
Namespaces Descriptions
System.Data Holds the DataSet and DataTable classes
Holds the class collection used to access an OLEDB data source
System.Data.OleDb
Holds the classes used to access an SQL Server 7.0 data source
System.Data.SqlClient
or later
Holds the class collection used to access an ODBC data source
System.Data.Odbc
Holds the classes used to access an Oracle data source
System.Data.OracleClient

7/20/2017 10
3.4.1.1 The ODBC Data Provider
The ODBC.NET supports the following Data
Providers:

SQL Server
Microsoft ODBC for Oracle
Microsoft Access Driver (*.mdb)

7/20/2017 11
3.4.1.2 The OLEDB Data Provider
The OLE DB.NET data access technique supports the following
Data Providers:

Microsoft Access
SQL Server (7.0 or later)
Oracle (9i or later)

7/20/2017 12
3.4.1.3 The SQL Server Data Provider
This Data Provider provides access to a SQL Server version
7.0 or later database using its own internal protocol. The
functionality of the data provider is designed to be similar to
that of the .NET Framework data providers for OLE DB,
ODBC, and Oracle. All classes related to this Data Provider
are defined in a DLL file and is located at the
System.Data.SqlClient namespace. Although Microsoft
provides different Data Providers to access the data in SQL
Server database, such as the ODBC and OLE DB, for the
sake of optimal data operations, it is highly recommended to
use this Data Provider to access the data in an SQL Server
data source.

7/20/2017 13
3.4.1.4 The Oracle Data Provider
This Data Provider is an add-on component to the .NET
Framework that provides access to the Oracle database. All
classes related to this Data Provider are located in the
System.Data.OracleClient namespace. This provider relies
upon Oracle Client Interfaces provided by the Oracle Client
Software. You need to install the Oracle Client software on
your computer to use this Data Provider.
Microsoft provides multiple ways to access the data stored in
an Oracle database, such as Microsoft ODBC for Oracle and
OLE DB, you should use this Data Provider to access the
data in an Oracle data source since this one provides the
most efficient way to access the Oracle database.

7/20/2017 14
3.4.2 The Connection Class
Table 3-3 lists these popular Connection classes used for the
different data sources:

Table 3-3. The Connection classes and databases


Connection Class Associated Database
OdbcConnection ODBC Data Source
OleDbConnection OLE DB Database
SqlConnection SQL Server Database
OracleConnection Oracle Database

7/20/2017 15
3.4.2 The Connection Class
The connection string is a property of the Connection class
and it provides all necessary information to connect to your
data source. Regularly this connection string contains a quite
few parameters to define a connection, but only five of them
are popularly utilized for most data-driven applications:

Provider
Data Source
Database
User ID
Password
7/20/2017 16
The Provider parameter

Specifies the driver that the connection class


uses to communicate with the database.
The most common drivers are:
Microsoft.Jet.OLEDB.4.0 for Access
SQLOLEDB for SQL Server
MSDAORA for Oracle

7/20/2017 17
The Data Source parameter

is used to specify the server name of the


computer on which the database is running.

When connecting to an Access database,


this specifies the path and database name.

7/20/2017 18
The Database parameter

Is self-explanatory and specifies the


database name where your data resides

7/20/2017 19
User ID and Password parameters

They are used to specify your database login


credentials
The User ID and Password defined in the
database, not your Windows login
credentials.

7/20/2017 20
Connection

What is Connection?
Define Connection
SqlConnection conn=new SqlConnection();
Conn.ConnectionString=User ID=sa;password=;
Data Source=MyServer;Initial Catalog=Northwind;

ConnectionString Parameters
Provider
Data Source
Initial Catalog
Integrated Security
UserID/Password
Connected Environment (Scenario)

1. Open connection
2. Execute command
3. Process rows in reader
4. Close reader
5. Close connection
Disconnected Environment

Storage of data local copy from repository


Possibility to update the main data source
Advantages
Economy of server resources
Does not require continual connection
Drawbacks
Demands conflict resolution while data update
Data is not always up to date
Command Object

A command object is a reference to a SQL


statement or stored procedure
Properties
Connection
CommandType
CommandText
Parameters
Methods
ExecuteNonQuery
ExecuteReader
ExecuteScalar
DataReader Object

What is query?
Forward-only cursor
Read method
Read next record
Return true if record is exist
IsDbNull
Close method
NextResult for multiply select statements
What Are DataSets and DataTables
DataSet
DataTable

DataTable

Connection Stored
Procedure

Database

Server Data Store


The DataSet Object Model
Common collections
Tables (collection of DataTable objects)
Relations (collection of DataRelation objects)
Data binding to Web and Windows controls supported
Schema can be defined programmatically or using
XSD
DataColumn

DataTable DataRow

DataRelation Constraints
What Is a DataAdapter?
DataSet
DataAdapter Data source
DataTable
Fill
Update

DataTable
DataAdapter
Fill
Update
How to Bind a DataSet to a DataGrid

To bind programmatically

DataGrid dataGrid1 = new DataGrid();


sqlDataAdapter1.Fill(dataSet1, "Customers");
sqlDataAdapter2.Fill(dataSet1, "Orders");
dataGrid1.DataSource = dataSet1;
How to Access Data in a DataSet
Object
DataRow
objects

DataTable
objects

DataColumn
objects

DataColumn
objects
How to Update a Database in
ADO.NET
Client Server

DataSet DataAdapter
Database
Fill
Data
Update Data
DataTable

InsertCommand

UpdateCommand

DeleteCommand
How to Create a Database Record

Create a new row that matches the table


schema
DataRow myRow = dataTable.NewRow();

Add the new row to the dataset


dataTable.Rows.Add( myRow );

Update the database


sqlDataAdapter1.Update( dataSet );
Grid View

The GridView and Details View controls are


commonly used in a Master/Detail page. A
Master/Detail page is a page that displays a
list of items from a database along with the
details of a selected item in the list
Repeater

The Repeater control is used to display a repeated


list of items that are bound to the control
Following are the major differences between
repeater and gridview controls: 1- Repeater is a light
weight control and gridview is a heavy control. 2-
Repeater doesnt provide any built-in support to
format the data whereas the gridview control has a
very good support to format, sort and align the data.
3- Repeater provides more customization than
gridview. 4- It is difficult to display data with repeater
than gridview.
DataList

The DataList control is, like the Repeater


control, used to display a repeated list of
items that are bound to the control. However,
the DataList control adds a table around the
data items by default.
light weight control, and acts as a container
of repeated data items. The templates in this
control are used to define the data that it will
contain.
Details view

Details View help us to show data one record


at a time
Sql datasourse,accessdatasourse,Xmldatasourse

The following are asses to sql database

You might also like