Module 13
Module 13
ADO.NET 2.0
By
SRIRAM . B
Objective
Introduction to ADO.NET & Features of ADO.NET
ADO.NET Architecture
Connecting database through Connection Object
Executing Queries through Command Object
Retrieving records through DataReader
Connect to a database by using DataAdapter
Dataset Object Model
Accessing Databases through Dataset
Command Builder
Data Relation Object
ADO.NET 2.0 Features
Introduction to
ADO.NET
Introduction to ADO.NET
This includes all of the System.Data Namespace and one class from
the System.XML Namespace.
Locking is possible
Better Security
Data persist in Binary format Locking is not reqd.
Data persist in XML
Diff. between ADO Recordset &
ADO.NET Dataset
ADO.NET Dataset ADO Recordset
Holds more than one table Holds only one table
You can retrieve data from two
databases like oracle, Sql server Not Possible
and merge them in one dataset
Cannot be transmitted on
HTTP
Namespaces in ADO.NET
System.Data
System.Data.OleD
B
System.Data.SqlCl
ient
System.XML
Features of
ADO.NET
Features of ADO.NET
SqlConnection("DataSource=stg1\\sqlexpress; Initial
Catalog=Northwind;Integrated Security=True");
SqlConnection("DataSource=stg1\\sqlexpress; Initial
Catalog=Northwind;Integrated Security=True");
Connection String Property For Oracle, MS
Access & SQL Server 6.5
For Oracle
OleDbConnection o = new OleDbConnection(“Provider = MSDAORA;
Data Source= ORACLE817; Userid = OLEDB; Password=
OLEDB;”)
For MS Access
Con.Close();
Console.ReadLine();
}
}
}
Example 2 – Execute Scalar
using System;
using System.Data;
using System.Data.SqlClient;
namespace SriConsole.ADO.NET
{
public class ExecuteScalar2
{
static void Main(string[] args)
{
String empname;
int exp;
Console.WriteLine(empname);
Console.WriteLine (exp);
Con.Close();
Console.ReadLine ();
}
}
}
Example 1 – Execute Reader
using System;
using System.Data;
using System.Data.SqlClient;
namespace SriConsole.ADO.NET
{
public class ExecuteReader
{
static void Main(string[] args)
{
Con.Open();
Example 1 – Execute Reader..
SqlCommand Cmd = new SqlCommand("Select * from emp",Con);
SqlDataReader dr = Cmd.ExecuteReader();
while(dr.Read())
{
int empid = Convert.ToInt32(dr.GetValue(0));
string empname= dr.GetString(1);
int exp = Convert.ToInt32(dr.GetValue(2));
Console.WriteLine (empid+""+empname+""+exp);
}
dr.Close();
Con.Close();
Console.ReadLine ();
}
}
}
Example 1 – Execute NonQuery()
using System;
using System.Data;
using System.Data.SqlClient;
namespace SriConsole.Ado.net
{
SqlConnection Con =
new SqlConnection("data source=.\\sqlexpress;
initial catalog=db; Integrated Security=SSPI");
Con.Open();
Example 1 – Execute NonQuery()..
using System.Data.SqlClient;
namespace SriConsole.ADO.NET
{
public class SQLUserInput
{
static void Main(string[] args)
{
SqlConnection Con = new
SqlConnection("data source=.\\sqlexpress;initial
catalog=db; Integrated Security=SSPI");
Con.Open();
Example 2 – Execute NonQuery()..
for(int i=0;i<=2;i++)
{
Console.WriteLine("Enter the eid");
int empid = Convert.ToInt32(Console.ReadLine());
cmd.ExecuteNonQuery();
Con.Close();
}
}
}
}
Command
Builder
Command Builder
c.Open();
Example – Command Builder..
SqlDataAdapter da = new SqlDataAdapter("select *
from employee",c);
DataSet ds=new DataSet();
da.Fill(ds);
DataRow dr=ds.Tables[0].NewRow();
dr["empid"]=107;
dr["empname"]="k";
ds.Tables[0].Rows.Add(dr);
SqlCommandBuilder cb=new SqlCommandBuilder(da);
da.Update(ds.Tables[0]);
Console.WriteLine(ds.Tables[0].Rows[0][1]);
Console.WriteLine(ds.Tables[0].Rows.Count);
}
}
}
DataReader
Data Reader
Is used to retrieve data from a data source in a read-
only and forward-only mode.
Stores a single row at a time in the memory.
Are of two types :-
•SQLDataReader for SQL Server
•OleDbDataReader for OLEDB
Commonly used methods:
• Read()
• Close()
• NextResult()
DataAdapter
DataAdapter
Creates a dataset and updates the database.
Handles data transfer between the database and
the dataset through its properties and methods.
Displays the data through the process of table
mapping.
Are of two types:
• SqlDataAdapter
• OleDbDataAdapter
Methods :- Fill, FillSchema, Update
DataAdapter Methods
Fill
Executes the command and fill the Dataset object with
data from the datasource
FillSchema
namespace SriConsole.ADO.NET
{
public class SelectAdapter
{
static void Main(string[] args)
{
SqlConnection Con = new SqlConnection("data
source=.\\sqlexpress;initial catalog=db; user
id=sa");
Con.Open();
Example 1 - DataAdapter..
SqlDataAdapter da = new SqlDataAdapter ("Select *
from emp",Con);
DataSet ds = new DataSet();
da.Fill(ds,"emp");
Console.WriteLine(ds.Tables[0].Rows[0]
[0].ToString());
Console.ReadLine();
}
}
}
Data Consumer Object
Data Set
Data Table
Data Row
Data Column
Data Relation
DataSet
DataSet Object Model
DATASET
DataRelation
DataTable
Typed DataSet
Untyped
DataSet
Typed DataSet
HasChanges
Indicates that has any changes been made since the dataset
was loaded or accept changes method was executed.
Diff. Between Dataset & DataReader
Dataset is a disconnected architecture, while DataReader has a live
connection while reading data. To catch data & pass to different tier
“Dataset” forms the best choice using XML.
When application needs to access data from more than one table
“Dataset” forms the best choice where as “DataReader” holds only one
table.
DataReader provides Forward only & Read only access to data And does
not support for moving back while reading records.
Methods :-
Find :- Takes array of values and returns the index
of the row
FindRow :- Takes array of values but returns a
collection of DataRow
AddNew :- Adds a new row to the DataView
Delete :- Deletes the specified row from DataView
namespace SriConsole.ADO.NET
{
class DRO
{
DataRelation dr=ds.Relations.Add("emp
details",ds.Tables["dept"].Columns["dno"],ds.Tables[
"emp"].Columns["dno"]);
Example 1 – DataRelation Object..
foreach(DataRow dt in ds.Tables["dept"].Rows)
{
Console.WriteLine("\t Department No:"+dt["dno"]);
Bulk Copy
Asynchronous Processing
Generic Coding with DB Provider
Factories
Transaction Management
State Management
Bulk Copy
Bulk Copy
The SqlBulkCopy feature in ADO.NET 2.0 enables us
to copy a large volume of data between a source
data store and a destination data table.
DbConnection dbConnection =
dbProviderFactory.CreateConnection();
Hierarchy
System.Data.Common.DbProvider.Fa
ctory
DB Provider Factory
Transactions
DB Transactions
Transaction – Concurrency Problems
Transactions Isolation Level
Settings Isolation Level
Transactions in ASP.NET 1.1
Reset Isolation Levels
Save Point in Transactions
Transactions
Demo
Setting the Save Point
Transaction
Handling
Transaction Scope
Transactions Completed Event
Setting Isolation Level
Session State
Configuration
Session State Configuration
Session State Management
In Process Mode
State Server Mode
SQL Server Mode
Disabling Session State
Demo
Session Ends
Exercise
Relax