Você está na página 1de 24

ADO.

NET
AGENDA
 Introduction to ADO.NET
 ADO Vs ADO.NET

 ADO.NET Architecture

 ADO.NET Namespaces

 Connected & Disconnected DB

 Connected Classes

 Disconnected Classes
INTRODUCTION TO ADO.NET
 ADO.NET is a collection of classes, interfaces,
structures, and enumerated types that manage data
access from relational data stores within the .NET
Framework
 Being a Part of the .NET framework, ADO.NET was
built with the new world of XML, disconnected data,
web and HTTP in mind
 It is a rewrite of ADO for the .NET framework

 Is not a replacement of ADO for COM developers

 ADO.NET is a natural evolution of ADO, built around


n-tier development and architected with XML at its
core
ADO VS ADO.NET

Featurea ADO ADO.NET


In Memory In ADO, it is the In ADO.NET , it is the
Representation of a RECORDSET. It can DATASET. It can
Data .contain only one Table contain one or more
tables represented by
.Data Table Object
Relationship Requires the JOIN Supports the
between Multiple Query DataRelation Object
Tables
Data Visitation Scans the RecordSet Uses a navigation
Rows Sequentially paradigm for non-
sequential access
Disconnected Provided by RecordSet Communicates with
Access but typically supports standardized calls to
connected access the DataAdapter
ADO VS ADO.NET
Feature ADO ADO.NET

Programmability Uses connection Object Uses strongly typed


.to transmit commands programming
characteristics of
.XML
Sharing Uses COM Marshalling Transmits a Dataset
Disconnected Data to transmit with an XML File
between tiers or .disconnected recordset
.components
Transmitting Data Problematic because Supported , Dataset
.through firewalls firewall are typically object use XML which
configured to prevent can pass through
.system-level requests .firewalls
Scalability Database locks and Disconnected access
active database to database data
connections for long without retaining
durations database locks
ADO.NET ARCHITECTURE
ADO.NET NAMESPACES
System.data Core namespace, defines types that
represent data

System.Data.Common Types shared between managed providers

System.Data.OleDb Types that allow connection to OLE DB


compliant data sources

System.Data.SqlClient Types that are optimized to connect to


Microsoft SQL Server

System.Data.SqlTypes Native data types in Microsoft SQL Server


DATA
 ADO.NET Supports two different programming environments:
 Connected
 Connection
 Command
 DataReader
 Parameter
 Transaction
 Data Adapter

 Disconnected
 DataSet
 DataTable
 DataColumn
 DataRow
 Constraint
 DataRelation
 DataView
CONNECTED DATABASE
 The Connected environment provides forward-only,
read-only access to data in the data source and the
ability to execute commands against the data source.

 The connected classes provide a common way to work


with connected data regardless of the underlying data
source.

 We will see the various classes that they include..


CONNECTION CLASS
 Maintains information required to connect to the
data source through a connection string.
 The connection string contains information such as
the name of the data source and its location, and
authorization credentials and settings.
 It has the methods to open and close the connection.

 It has the methods for transactions to be initiated on


the connection
 It also controls other properties of the connection
COMMAND CLASS
 Information is submitted to a database as a query via a
Connection object.
 It executes SQL statements or stored procedures
against the data source.
 It has a parameter collection object containing
parameter objects that allow parameterized SQL.
 Input and output parameters are supported, along
with return values as part of the command syntax
 Results are returned in the form of streams. Accessed
by:
 DataReader object
 DataSet object via a DataAdapter
PARAMETER
 Allows Parameters for both parameterized queries
and stored procedures to be defined.

 The parameter class is accessed through the


ParametersCollection object within the Command
Object

 It supports input and output parameters as well as


return values from stored procedures.
TRANSACTION
 A transaction is a group of operations combined into a
logical unit of work that is either guaranteed to be
executed as a whole or rolled back.
 Transactions help the database in satisfying all the
ACID (Atomic, Consistent, Isolated, and Durable)
Properties.
 Transaction processing is an indispensible part of
ADO.NET
 This class allows transactions to be created on a
connection so that multiple changes to data in a data
source are treated as a single unit of work.
 It also decides that whether transactions are all
committed or cancelled.
DATAREADER
 Provides connected forward-only, read only access to
the data source.

 It is optimized for speed.

 It is instantiated using command object.

 When a DataReader is used, parts of the ADO.NET


model are cut out, providing faster and more efficient
data access
DATAADAPTER
 Provides a set of methods and properties to retrieve
and save data between a DataSet and its source data
store
 Allows the use of stored procedures

 Connects to the database to fill the DataSet and also


update the database
 It bridges the data source and the disconnected
DataSet or DataTable classes.
 It wraps the connected classes to provide this
functionality.
 It provides a method to retrieve data into a
disconnected object with the data source.
DISCONNECTED CLASSES
 The disconnected environment allows data retrieved
from the data source to be manipulated and later
reconciled with the data source.

 The disconnected classes provide a common way to


work with disconnected data regardless of the
underlying data source

 The following are the various disconnected classes


available:
DATASET
 Replaces the ADO Recordset
 Represents a cache of data that contains tables, columns,
relationships, and constraints, just like a database
 Regardless of where the source data comes from, data can
all be placed into DataSet objects
 Tracks changes that are made to the data it holds before
updating the source data
 DataSet are also fully XML-featured

 Works with all current models of data storage: flat,


relational, and hierarchical
 It is essentially an in-memory Relational DB, serving as a
container for the DataTable, DataColumn, DataRow,
Constraint and DataRelation Objects.
DATATABLE
 A DataTable, which represents one table of in-memory relational
data, most commonly as a member of a DataSet.
 Allows disconnected data to be examined and modified through
a collection of DataColumn and DataRow classes.
 It allows constraints as foreign key and unique constraints to be
defined using Constraint class
 When you first create a DataTable, it does not have a schema
(that is, a structure).
 To define the schema of the table, you must create and add
DataColumn objects to the Columns collection of the table.
 You can also define a primary key column for the table, and
create and add Constraint objects to the Constraints collection
of the table.
 After you have defined the schema for a DataTable, you can add
rows of data to the table by adding DataRow objects to the Rows
collection of the table.
DATACOLUMN AND DATAROW
 DataColumn corresponds to a column in a DataTable.

 It stores metadata about the structure of the column


that, together with the constraints, defines the schema
of the table.

 DataRow corresponds to a row in a DataTable.

 It caches changes made to data contained in its


columns, storing both original and current values.
CONSTRAINT
 You can use constraints to enforce restrictions on the data in
a DataTable, in order to maintain the integrity of the data.
 A constraint is an automatic rule, applied to a column or
related columns, that determines the course of action when
the value of a row is somehow altered.
 Constraints are enforced when the EnforceConstraints
property of the DataSet is true.
 There are two kinds of constraints in ADO.NET: the
ForeignKeyConstraint and the UniqueConstraint.
 A ForeignKeyConstraint enforces rules about how updates
and deletes to related tables are propagated
 The UniqueConstraint object, which can be assigned either
to a single column or to an array of columns in a DataTable,
ensures that all data in the specified column or columns is
unique per row
DATARELATION
 A DataRelation is used to relate two DataTable objects within the
Dataset.
 Relationships are created between matching columns in the parent
and child tables, where, the DataType value for both columns must
be identical.
 Relationships can also cascade various changes from the parent
DataRow to its child rows.
 To control how values are changed in child rows, add a
ForeignKeyConstraint to the ConstraintCollection of the DataTable
object.
 When a DataRelation is created, it first verifies that the relationship
can be established.
 After it is added to the DataRelationCollection, the relationship is
maintained by disallowing any changes that would invalidate it.
 Between the period when a DataRelation is created and added to the
DataRelationCollection, it is possible for additional changes to be
made to the parent or child rows.
DATAVIEW
 It allows data , once retrieved into a DataSet or
DataTable, to be viewed in different ways.

 It allows data to be sorted based on column values and


for a subset of the data to be filtered so that only rows
matching specific criteria are displayed.

 A major function of the DataView is to allow for data


binding on both Windows Forms and Web Forms.

Você também pode gostar