SlideShare une entreprise Scribd logo
1  sur  20
ADO.NET
.NET Data Access and Manipulation
Presented By: Wani Zahoor
Overview
 What is ADO.NET?
 Disconnected vs. connected data access models
 ADO.NET Architecture
 ADO.NET Core Objects
 Steps of Data Access
 Advanced Techniques and UI Tools
What is ADO.NET?
 A data-access technology that enables applications to connect to data stores
and manipulate data contained in them in various ways
 Former version was ADO (ActiveX Data Object)
What is ADO.NET?
 An object oriented framework that allows you to interact with database
systems
Objective of ADO.NET
 Support disconnected data architecture,
 Tight integration with XML,
 Common data representation
 Ability to combine data from multiple and varied data sources
 Optimized facilities for interacting with a database
ADO.NET Architecture
ADO.NET Core Objects
 Core namespace: System.Data
 .NET Framework data providers:
Data Provider Namespace
SQL Server System.Data.SqlClient
OLE DB System.Data.OleDb
ODBC System.Data.Odbc
Oracle System.Data.OracleClient
ADO.NET Core Objects
Object Description
Connection Establishes a connection to a specific data source. (Base
class: DbConnection)
Command Executes a command against a data source. Exposes
Parameters and can execute within the scope of a
Transaction from a Connection. (The base class:
DbCommand)
DataReader Reads a forward-only, read-only stream of data from a
data source. (Base class: DbDataReader)
DataAdapter Populates a DataSet and resolves updates with the data
source. (Base class: DbDataAdapter)
DataTable Has a collection of DataRows and DataColumns
representing table data, used in disconnected model
DataSet Represents a cache of data. Consists of a set of DataTables
and relations among them
Connected Data Access Model
Disconnected Data Access
Model
Pros and Cons
Connected Disconnecte
d
Database
Resources
- +
Network Traffic - +
Memory Usage + -
Data Access - +
Steps of Data Access:
Disconnected Environment
 Defining the connection string
 Defining the connection
 Defining the command
 Defining the data adapter
 Creating a new DataSet object
 SELECT -> fill the dataset object with the result of
the query through the data adapter
 Reading the records from the DataTables in the
datasets using the DataRow and DataColumn
objects
 UPDATE, INSERT or DELETE -> update the
database through the data adapter
using System;
using System.Data;
using System.Data.SqlClient;
namespace SampleClass
{
class Program
{
static void Main(string[] args)
{
string connStr =
Properties.Settings.Default.connStr;
SqlConnection conn = new SqlConnection(connStr);
string queryString = "SELECT * from titles;";
SqlDataAdapter da = new
SqlDataAdapter(queryString,conn);
DataSet ds = new DataSet();
da.fill(ds);
// Work on the data in memory using
// the DataSet (ds) object
}
}
}
EXAMPLE
Disconnected –
Update, Delete, Insert
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlCommandBuilder cmdBuilder = new
SqlCommandBuilder(da);
da.Fill(ds);
DataRow dr = ds.Tables[0].Rows[0];
dr.Delete();
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
DataRow dr = ds.Tables[0].Rows[0];
dr["CustomerName"] = "John";
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
DELETE
UPDATE
INITIAL CODE
DataRow dr = ds.Tables[0].NewRow();
dr["CustomerName"] = "John";
dr["CustomerSurName"] = "Smith";
ds.Tables[0].Rows.Add(dr);
da.UpdateCommand = builder.GetUpdateCommand();
da.Update(ds);
INSERT
Steps of Data Acces :
Connected Environment
 Create connection
 Create command (select-insert-update-
delete)
 Open connection
 If SELECT -> use a DataReader to fetch
data
 If UPDATE,DELETE, INSERT -> use
command object’s methods
 Close connection
static void Main()
{
string connectionString =
Properties.Settings.Default.connStr;
string queryString = "SELECT CategoryID, CategoryName FROM
dbo.Categories;";
SqlConnection connection = new
SqlConnection(connectionString);
SqlCommand command = new SqlCommand(queryString,connection);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("t{0}t{1}“,reader[0],reader[1]);
}
reader.Close();
connection.close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
EXAMPLE
Connected – Update, Delete, Insert
 Command class core methods:
ExecuteNonQuery : Executes a SQL
statement against a connection object
ExecuteReader: Executes the
CommandText against the Connection
and returns a DbDataReader
ExecuteScalar: Executes the query
and returns the first column of the first
row in the result set returned by the
query
Connected – Update, Delete, Insert
string connString =
Properties.Settings.Default.connStr;
SqlConnection conn = new
SqlConnection(connString);
SqlCommand cmd = new SqlCommand("delete from
Customers" + "where custID=12344", conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Can be an update or insert command
Choosing a DataReader or a
Dataset
 The type of functionality application requires
should be considered
 Use a dataset to:
 Cache data locally in your application so that you can manipulate it
 Remote data between tiers or from an XML Web service
 Interact with data dynamically such as binding to a Windows Forms control or
combining and relating data from multiple sources
 Perform extensive processing on data without requiring an open connection to the
data source, which frees the connection to be used by other clients
 If readonly data is needed use DataReader to
boost performance
After .NET Framework 2.0
An example of databinding model

Contenu connexe

Tendances (20)

String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Asp.Net Core MVC with Entity Framework
Asp.Net Core MVC with Entity FrameworkAsp.Net Core MVC with Entity Framework
Asp.Net Core MVC with Entity Framework
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
RDBMS
RDBMSRDBMS
RDBMS
 
jQuery
jQueryjQuery
jQuery
 
Introduction to ado.net
Introduction to ado.netIntroduction to ado.net
Introduction to ado.net
 
Java swing
Java swingJava swing
Java swing
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 
Components of .NET Framework
Components of .NET FrameworkComponents of .NET Framework
Components of .NET Framework
 
Alter table command
Alter table commandAlter table command
Alter table command
 
MYSQL
MYSQLMYSQL
MYSQL
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 

En vedette

Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5Ehtsham Khan
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use itnspyre_net
 
Dotnet differences compiled -1
Dotnet differences compiled -1Dotnet differences compiled -1
Dotnet differences compiled -1Umar Ali
 
05 entity framework
05 entity framework05 entity framework
05 entity frameworkglubox
 
Entity Framework and Domain Driven Design
Entity Framework and Domain Driven DesignEntity Framework and Domain Driven Design
Entity Framework and Domain Driven DesignJulie Lerman
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Bishoy Demian
 

En vedette (13)

Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5Getting started with entity framework 6 code first using mvc 5
Getting started with entity framework 6 code first using mvc 5
 
Entity framework and how to use it
Entity framework and how to use itEntity framework and how to use it
Entity framework and how to use it
 
Ado .net
Ado .netAdo .net
Ado .net
 
Dotnet differences compiled -1
Dotnet differences compiled -1Dotnet differences compiled -1
Dotnet differences compiled -1
 
05 entity framework
05 entity framework05 entity framework
05 entity framework
 
Entity Framework and Domain Driven Design
Entity Framework and Domain Driven DesignEntity Framework and Domain Driven Design
Entity Framework and Domain Driven Design
 
Getting started with entity framework
Getting started with entity framework Getting started with entity framework
Getting started with entity framework
 
Introducing Entity Framework 4.0
Introducing Entity Framework 4.0Introducing Entity Framework 4.0
Introducing Entity Framework 4.0
 
Ado.net
Ado.netAdo.net
Ado.net
 
ASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NETASP.NET 09 - ADO.NET
ASP.NET 09 - ADO.NET
 
For Beginers - ADO.Net
For Beginers - ADO.NetFor Beginers - ADO.Net
For Beginers - ADO.Net
 
ADO.NET -database connection
ADO.NET -database connectionADO.NET -database connection
ADO.NET -database connection
 
Ef code first
Ef code firstEf code first
Ef code first
 

Similaire à ADO.NET

Similaire à ADO.NET (20)

ADO.net control
ADO.net controlADO.net control
ADO.net control
 
Ado.Net Architecture
Ado.Net ArchitectureAdo.Net Architecture
Ado.Net Architecture
 
Data management with ado
Data management with adoData management with ado
Data management with ado
 
3-ADO.NET.pdf
3-ADO.NET.pdf3-ADO.NET.pdf
3-ADO.NET.pdf
 
Introduction to ado
Introduction to adoIntroduction to ado
Introduction to ado
 
5.C#
5.C#5.C#
5.C#
 
Unit4
Unit4Unit4
Unit4
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
ADO.NET by ASP.NET Development Company in india
ADO.NET by ASP.NET  Development Company in indiaADO.NET by ASP.NET  Development Company in india
ADO.NET by ASP.NET Development Company in india
 
ADO.NETObjects
ADO.NETObjectsADO.NETObjects
ADO.NETObjects
 
Ch06 ado.net fundamentals
Ch06 ado.net fundamentalsCh06 ado.net fundamentals
Ch06 ado.net fundamentals
 
ASP.NET Session 11 12
ASP.NET Session 11 12ASP.NET Session 11 12
ASP.NET Session 11 12
 
2310 b 10
2310 b 102310 b 10
2310 b 10
 
Marmagna desai
Marmagna desaiMarmagna desai
Marmagna desai
 
Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.Lecture 6. ADO.NET Overview.
Lecture 6. ADO.NET Overview.
 
Csharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptxCsharp_dotnet_ADO_Net_database_query.pptx
Csharp_dotnet_ADO_Net_database_query.pptx
 
LECTURE 14 Data Access.pptx
LECTURE 14 Data Access.pptxLECTURE 14 Data Access.pptx
LECTURE 14 Data Access.pptx
 
Ado.net
Ado.netAdo.net
Ado.net
 
ADO .NET by Sonu Vishwakarma
ADO .NET by Sonu VishwakarmaADO .NET by Sonu Vishwakarma
ADO .NET by Sonu Vishwakarma
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 

Plus de Wani Zahoor

DotNet Framework
DotNet FrameworkDotNet Framework
DotNet FrameworkWani Zahoor
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoorWani Zahoor
 
Antennas in Wireless Communication
Antennas in Wireless CommunicationAntennas in Wireless Communication
Antennas in Wireless CommunicationWani Zahoor
 

Plus de Wani Zahoor (6)

VPN Network
VPN NetworkVPN Network
VPN Network
 
Java threads
Java threadsJava threads
Java threads
 
DotNet Framework
DotNet FrameworkDotNet Framework
DotNet Framework
 
Deadlocks by wani zahoor
Deadlocks by wani zahoorDeadlocks by wani zahoor
Deadlocks by wani zahoor
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Antennas in Wireless Communication
Antennas in Wireless CommunicationAntennas in Wireless Communication
Antennas in Wireless Communication
 

Dernier

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Dernier (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

ADO.NET

  • 1. ADO.NET .NET Data Access and Manipulation Presented By: Wani Zahoor
  • 2. Overview  What is ADO.NET?  Disconnected vs. connected data access models  ADO.NET Architecture  ADO.NET Core Objects  Steps of Data Access  Advanced Techniques and UI Tools
  • 3. What is ADO.NET?  A data-access technology that enables applications to connect to data stores and manipulate data contained in them in various ways  Former version was ADO (ActiveX Data Object)
  • 4. What is ADO.NET?  An object oriented framework that allows you to interact with database systems
  • 5. Objective of ADO.NET  Support disconnected data architecture,  Tight integration with XML,  Common data representation  Ability to combine data from multiple and varied data sources  Optimized facilities for interacting with a database
  • 7. ADO.NET Core Objects  Core namespace: System.Data  .NET Framework data providers: Data Provider Namespace SQL Server System.Data.SqlClient OLE DB System.Data.OleDb ODBC System.Data.Odbc Oracle System.Data.OracleClient
  • 8. ADO.NET Core Objects Object Description Connection Establishes a connection to a specific data source. (Base class: DbConnection) Command Executes a command against a data source. Exposes Parameters and can execute within the scope of a Transaction from a Connection. (The base class: DbCommand) DataReader Reads a forward-only, read-only stream of data from a data source. (Base class: DbDataReader) DataAdapter Populates a DataSet and resolves updates with the data source. (Base class: DbDataAdapter) DataTable Has a collection of DataRows and DataColumns representing table data, used in disconnected model DataSet Represents a cache of data. Consists of a set of DataTables and relations among them
  • 11. Pros and Cons Connected Disconnecte d Database Resources - + Network Traffic - + Memory Usage + - Data Access - +
  • 12. Steps of Data Access: Disconnected Environment  Defining the connection string  Defining the connection  Defining the command  Defining the data adapter  Creating a new DataSet object  SELECT -> fill the dataset object with the result of the query through the data adapter  Reading the records from the DataTables in the datasets using the DataRow and DataColumn objects  UPDATE, INSERT or DELETE -> update the database through the data adapter
  • 13. using System; using System.Data; using System.Data.SqlClient; namespace SampleClass { class Program { static void Main(string[] args) { string connStr = Properties.Settings.Default.connStr; SqlConnection conn = new SqlConnection(connStr); string queryString = "SELECT * from titles;"; SqlDataAdapter da = new SqlDataAdapter(queryString,conn); DataSet ds = new DataSet(); da.fill(ds); // Work on the data in memory using // the DataSet (ds) object } } } EXAMPLE
  • 14. Disconnected – Update, Delete, Insert SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataSet(); SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da); da.Fill(ds); DataRow dr = ds.Tables[0].Rows[0]; dr.Delete(); da.UpdateCommand = builder.GetUpdateCommand(); da.Update(ds); DataRow dr = ds.Tables[0].Rows[0]; dr["CustomerName"] = "John"; da.UpdateCommand = builder.GetUpdateCommand(); da.Update(ds); DELETE UPDATE INITIAL CODE DataRow dr = ds.Tables[0].NewRow(); dr["CustomerName"] = "John"; dr["CustomerSurName"] = "Smith"; ds.Tables[0].Rows.Add(dr); da.UpdateCommand = builder.GetUpdateCommand(); da.Update(ds); INSERT
  • 15. Steps of Data Acces : Connected Environment  Create connection  Create command (select-insert-update- delete)  Open connection  If SELECT -> use a DataReader to fetch data  If UPDATE,DELETE, INSERT -> use command object’s methods  Close connection
  • 16. static void Main() { string connectionString = Properties.Settings.Default.connStr; string queryString = "SELECT CategoryID, CategoryName FROM dbo.Categories;"; SqlConnection connection = new SqlConnection(connectionString); SqlCommand command = new SqlCommand(queryString,connection); try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Console.WriteLine("t{0}t{1}“,reader[0],reader[1]); } reader.Close(); connection.close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } EXAMPLE
  • 17. Connected – Update, Delete, Insert  Command class core methods: ExecuteNonQuery : Executes a SQL statement against a connection object ExecuteReader: Executes the CommandText against the Connection and returns a DbDataReader ExecuteScalar: Executes the query and returns the first column of the first row in the result set returned by the query
  • 18. Connected – Update, Delete, Insert string connString = Properties.Settings.Default.connStr; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand("delete from Customers" + "where custID=12344", conn); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); Can be an update or insert command
  • 19. Choosing a DataReader or a Dataset  The type of functionality application requires should be considered  Use a dataset to:  Cache data locally in your application so that you can manipulate it  Remote data between tiers or from an XML Web service  Interact with data dynamically such as binding to a Windows Forms control or combining and relating data from multiple sources  Perform extensive processing on data without requiring an open connection to the data source, which frees the connection to be used by other clients  If readonly data is needed use DataReader to boost performance
  • 20. After .NET Framework 2.0 An example of databinding model