Data Access With ADO
Data Access With ADO
NET
ADO.NET
constantly open
DB
DB
connection
ADO.NET client Database
ADO.NET
temporary (offline)
DB
DB
connection
ADO.NET client
Disconnected Database
Model
Connected Model
ADO.NET
constantly open
DB
DB
connection
ADO.NET client Database
Disconnected Model
ADO.NET
temporary (offline)
DB
DB
connection
OO ORM
Programming Framework
Language
ORM Model:
Benefits and Problems
ORM model benefits
Increased productivity – writing less code
Use objects with associations instead of tables and
SQL commands
Integrated object query mechanism
ORM model drawbacks:
Less flexibility – SQL is automatically generated
Performance issues (sometimes)
ADO.NET Architecture
ADO.NET Architecture With
Different Model
What is ADO.NET?
ADO.NET is a standard .NET class library for accessing databases,
processing data and XML
ActiveX Data Object.
An API for working with data in .NET
ADO.NET
Programming Model
OLE DB sources
SQL Server 2000
(MS Access, MS Oracle ODBC Data
SQL Server 2005
Excel, Active Database Source
SQL Server 2008
Directory, etc.)
Data Providers in ADO.NET
System.Data.SqlClient and
System.Data.SqlTypes
Data Provider classes for accessing SQL Server
System.Data.OleDb
Classes for accessing OLE DB data sources
System.Data.Odbc
Classes for accessing ODBC data sources
System.Data.Oracle
Classes for accessing Oracle databases
ADO.NET: Connected Model
SQ L
SqlClient Data Provider
SqlConnection
Establish database connection to SQL Server
SqlCommand
Executes SQL commands on the SQL Server
through an established connection
Could accept parameters (SQLParameter)
SqlDataReader
Retrieves data (record set) from SQL Server as a
result of SQL query execution
ADO.NET Classes for the
Connected Model
SqlDataReader XmlReader
SqlCommand
SqlParameter
SqlParameter
SqlConnection SqlParameter
Database
The SqlConnection Class
SqlCommand selectIdentityCommand =
new SqlCommand("SELECT @@Identity", dbCon);
int insertedRecordId = (int)
(decimal) selectIdentityCommand.ExecuteScalar();
SqlCommand: Using Stored
Procedures
SqlCommand requires following configuration to
execute a stored procedure.
Set the stored procedure name
Set the command type as
CommantType.StoredProcedure
Set the SqlParameter to send parameter to the SP.
SqlCommand: Using Stored
Procedures Example
SqlCommand cmd = new SqlCommand(
"CustOrderHist", conn);
cmd.CommandType =
CommandType.StoredProcedure;
cmd.Parameters.Add( new
SqlParameter("@CustomerID", custId));
cmd.ExecuteReader();
ADO.Net DataSet and Adapters
A DataSet is an in-memory
data store that can hold
numerous tables.
DataSets only hold data and
do not interact with a data
source.
It is the SqlDataAdapter that
manages connections with the
data source and gives us DataSet
disconnected behavior.
ADO.Net DataSet and
Adapters(2)
The SqlDataAdapter opens a connection only
when required and closes it as soon as it has
performed its task.
The SqlDataAdapter performs the following
tasks when filling a DataSet with data:
Open connection
Retrieve data into DataSet
Close connection
ADO.Net DataSet and
Adapters(3)
Creating a DataSet Object
DataSet dsCustomers = new DataSet();
Creating A SqlDataAdapter
SqlDataAdapter daCustomers
= new SqlDataAdapter(
"select CustomerID, CompanyName from
Customers", conn);
Filling the DataSet
daCustomers.Fill(dsCustomers, "Customers");
Using the DataSet
dgCustomers.DataSource = dsCustomers;
ADO.Net DataSet additional
functionalities
Adding Multiple Tables
Adding a Relationship Between Tables
Merging DataSet Contents
Copying DataSet Contents
ADO.Net DataView
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=
C:\Library.mdb;Persist Security Info=False
References
http://www.csharp-
station.com/Tutorial/AdoDotNet
http://msdn.microsoft.com/en-
us/library/e80y5yhx(v=vs.80)
http://www.codeproject.com
Download the application and presentation
https://docs.google.com/open?
id=0B5b3ZcIvnbMrdHlOSDRXWG9mbDA
Data Access with ADO.NET
Questions?