unit4_netc#
unit4_netc#
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.
It uses RecordSet to store the data from It uses Dataset to store the data from
datasource datasource
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
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();
}
} } }