Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

unit4_netc#

Uploaded by

chhavisagar1234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

unit4_netc#

Uploaded by

chhavisagar1234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

UNIT 4

ADO.NET
• ADO.NET is a set of classes (a framework) to interact with data
sources such as databases and XML files. ADO is the acronym
for ActiveX Data Objects. It allows us to connect to underlying
data or databases. It has classes and methods to retrieve and
manipulate data.
ADO
• ADO is a Microsoft technology
• ADO stands for ActiveX Data Objects
• ADO is a Microsoft Active-X component
• ADO is automatically installed with Microsoft IIS
• ADO is a programming interface to access data in a database
• ADO stands for ActiveX Data Objects and it relies on COM
whereas ADO.NET relies on managed providers defined by the
.NET CLR (Common Language Runtime).
• ADO.NET provides consistent access to data sources such as
SQL Server, as well as data sources exposed through OLE DB
and XML.
Difference between ADO &
ADO.NET
ADO ADO.Net
It is a COM(Component Object Modelling) It is a CLR(Common Language Runtime) based
based Library. Library.

ADO works in the connected mode to access ADO.Net works in the disconnected mode to
data. access data.

Locking features is available Locking features is not available


Data is stored in Binary Format Data is stores in XML
XML integration is not possible XML integration is possible

It uses RecordSet to store the data from It uses Dataset to store the data from
datasource datasource

Dataset object of ADO.Net includes collection


Using classic ADO, you can obtain information
of DataTable wherein each DataTable will
from one table or set of tables through join. You
contain records fetched from a particular table.
cannot fetch records from multiple tables
Hence multiple table records are maintained
independently
independently
In ADO, You can create only Client side In ADO.Net, You can create both Client &
cursor. Server side cursor

Derives information about data implicitly at Leverages known metadata at design time in
run time, based on metadata that is often order to provide better run-time performance
expensive to obtain. and more consistent run-time behavior.

Firewall might prevent execution of Classic ADO.Net has firewall proof and its execution
ADO will never be interrupted

You cannot send multiple transaction using a You can send multiple transaction using a
single connection instance single connection instance
ADO.NET ARCHITECTURE
• ADO.NET uses a multilayer architecture that has components
such as the Connection, Reader, Command,
Adapter and DataSet objects. ADO.NET introduced data
providers that are a set of special classes to access a specific
database, execute SQL commands and retrieve data. Data
providers are extensible; developers can create their own
providers for proprietary data source Some examples of data
providers include SQL server providers, OLE DB and Oracle
providers.
DataSet
• The ADO.NET DataSet is explicitly designed for data access
independent of any data source. As a result, it can be used
with multiple and differing data sources, used with XML data,
or used to manage data local to the application.
The DataSet contains a collection of one or
more DataTable objects consisting of rows and columns of
data, and also primary key, foreign key, constraint, and relation
information about the data in the DataTable objects.
Data Providers
• The .NET Framework Data Providers are components that
have been explicitly designed for data manipulation and fast,
forward-only, read-only access to data.
• The Connection object provides connectivity to a data source.
The Command object enables access to database commands
to return data, modify data, run stored procedures, and send
or retrieve parameter information.
• The DataReader provides a high-performance stream of data
from the data source.
• Finally, the DataAdapter provides the bridge between
the DataSet object and the data source.
• The DataAdapter uses Command objects to execute SQL
commands at the data source to both load the DataSet with
data and reconcile changes that were made to the data in
the DataSet back to the data source.
Connected and Disconnected Data Access
Architecture

• The ADO.NET Framework supports two models of Data Access


Architecture, Connection Oriented Data Access Architecture
and Disconnected Data Access Architecture.
• 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 these cases the application stays connected to the
database system even when it is not using any Database
Operations.
• ADO.Net solves this problem by introduces 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.
• 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.
Difference between Connected
and disconnected architecture
• ADO.NET introduces the connected DataAdapter class to
bridge the data source and disconnected classes by way of the
connected classes.
• The DataAdapter is an abstraction of the connected classes
that simplifies filling the
disconnected DataSet or DataTable classes with data from the
data source and updating the data source to reflect any
changes made to the disconnected data.
CONNECTED CLASSES
The following classes are used by ADO.NET to communicate
directly with the data source:
• ConnectionMaintains 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.
The Connection class has methods to open and close the
connection, for transactions to be initiated on the connection,
as well as control other properties of the connection.
• CommandExecutes SQL statements or stored procedures
against the data source. The command class has
a ParameterCollection object containing Parameter objects
that allow parameterized SQL statements and stored
procedures to be used against the data source.
• DataReaderProvides connected forward-only, read-only
access to the data source. It is optimized for speed.
The DataReader is instantiated through a Command object.
• ParameterAllows parameters for both parameterized queries
and stored procedures to be defined and set to appropriate
values. The Parameter class is accessed through
the ParametersCollection object within a Command object. It
supports input and output parameters as well as return values
from stored procedures.
• TransactionAllows 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 and either all committed or cancelled.
• DataAdapterBridges the data source and the
disconnected DataSet or DataTable classes.
The DataAdapter wraps the connected classes to provide this
functionality. It provides a method to retrieve data into a
disconnected object and a method to reconcile modified data
in the disconnected object with the data source.
The CommandBuilder class can generate the logic to reconcile
changes in simple situations; custom logic can be supplied to
deal with complex situations and optimize performance.
DISCONNECTED CLASSES
• The following ADO.NET classes allow data to be retrieved from
the data set, examined and modified offline, and reconciled
with the data source through the DataAdapter:
• DataSetProvides a consistent way to deal with disconnected
data completely independently of the data source.
The DataSet is essentially an in-memory relational database,
serving as a container for
the DataTable, DataColumn, DataRow, Constraint,
and DataRelation objects.
• The XML format serializes and transports a DataSet.
A DataSet can be accessed and manipulated either as XML or
through the methods and properties of
the DataSet interchangeably; the XmlDataDocument class
represents and synchronizes the relational data within
a DataSet object with the XML Document Object Model
(DOM).
• DataTableAllows disconnected data to be examined and
modified through a collection
of DataColumn and DataRow classes. The DataTable allows
constraints such as foreign keys and unique constraints to be
defined using the Constraint class.
• DataColumnCorresponds to a column in a table.
The DataColumn class stores metadata about the structure of
the column that, together with constraints, defines the
schema of the table. The DataColumn can also create
expression columns based on other columns in the table.
• DataRowCorresponds to a row in a table and can examine and
update data in the DataTable.
The DataTable exposes DataRow objects through
the DataRowCollection object it contains. The DataRow caches
changes made to data contained in its columns, storing both
original and current values. This allows changes to be
cancelled or to be later reconciled with the data source.
• ConstraintAllows constraints to be placed on data stored
within a DataTable. Unique and foreign key constraints can be
created to maintain data integrity.
• DataRelationProvides a way to indicate a relationship
between different DataTable objects within a DataSet.
The DataRelation relates columns in the parent and child
tables allowing navigation between the parent and child tables
and referential integrity to be enforced through cascading
updates and deletes.
• DataViewAllows 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
specified criteria are displayed.
ADO.NET Framework Data
Providers
• Data provider is used to connect to the database, execute
commands and retrieve the record. It is lightweight
component with better performance. It also allows us to place
the data into DataSet to use it further in our application.
.NET Framework Data Providers Objects
.NET Framework Data Provider for SQL Server
• Data provider for SQL Server is a lightweight component. It
provides better performance because it directly access SQL
Server without any middle connectivity layer. In early versions,
it interacts with ODBC layer before connecting to the SQL
Server that created performance issues.
• The .NET Framework Data Provider for SQL Server classes is
located in the System.Data.SqlClient namespace. We can
include this namespace in our C# application by using the
following syntax.

using System.Data.SqlClient;
• This namespace contains the following important classes.
.NET Framework Data Provider for Oracle
• It is used to connect with Oracle database through Oracle
client. The data provider supports Oracle client software
version 8.1.7 or a later version. This data provider supports
both local and distributed transactions.
• Oracle Data Provider classes are located
into System.Data.OracleClient namespace. We must use
both System.Data.OracleClient and System.data to connect
our application with the Oracle database

using System.Data;
using System.Data.OracleClient;
Which .NET Framework Data Provider is better
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{ class Program
{ static void Main(string[] args)
{
new Program().CreateTable();
}
public void CreateTable()
{ SqlConnection con = null;
try
{ // Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("create table student(id int not null,
name varchar(100), email varchar(50), join_date date)", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Table created Successfully");
}
catch (Exception e)
{ Console.WriteLine("OOPs, something went wrong."+e);
}
// Closing the connection
finally
{ con.Close();
} } } }
using System;
using System.Data.SqlClient;
namespace AdoNetConsoleApplication
{ class Program
{ static void Main(string[] args)
{ new Program().CreateTable();
}
public void CreateTable()
{ SqlConnection con = null;
try
{ // Creating Connection
con = new SqlConnection("data source=.; database=student; integrated security=SSPI");
// writing sql query
SqlCommand cm = new SqlCommand("insert into student
(id, name, email, join_date)values('101','Ronald Trump','ronald@example.com','1/12/2017')", con);
// Opening Connection
con.Open();
// Executing the SQL query
cm.ExecuteNonQuery();
// Displaying a message
Console.WriteLine("Record Inserted Successfully");
}
catch (Exception e)
{ Console.WriteLine("OOPs, something went wrong."+e);
}
// Closing the connection
finally
{ con.Close();
}
} } }

You might also like