Professional Documents
Culture Documents
Fourth Edition
12
Chapter 12
ADO.NET
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Introduction to Ado.net
• ADO.NET is the data access component for the .NET Framework.
• ADO.NET was built with the disconnected mode in mind - meaning the
entire database table(s) can be retrieved to a temporary file or to a local
machine if the database is stored on the network.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
ADO.NET Architecture
• ADO.NET does not provide a single set of classes that work with all
types of database management systems. However, you can use the
ADO.NET library of code to access data stored in many different
proprietors’ database management systems.
ADO.NET Components
• The two main components of ADO.NET for accessing and manipulating data:
• The DataSet
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Data Providers
• A data provider is a set of classes that understands how to
communicate with a specific data source or database management
system.
• The four data provider database sources currently included with .NET
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Data Providers (cont…)
• Each data provider includes a collection of classes used to access a data
source, such as a database.
• The four core classes that make up each data provider are ..
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
ADO.NET Namespaces
• Each of the data provider classes is encapsulated into a different namespace.
Namespaces Description
It contains the common classes for connecting, fetching data
System.Data from database. Classes are like as DataTable, DataSet,
DataView etc.
Contains the classes that are used to connect to a Microsoft
System.Data.SqlClient SQL Server database such as SqlCommand, SqlConnection,
SqlDataAdapter.
Contains classes required to connect to most ODBC drivers.
System.Data.Odbc
These classes include OdbcCommand, OdbcConnection.
Contains classes such as OracleConnection, OracleCommand
System.Data.OracleClient
required to connect to an Oracle database.
It contains classes for connecting, fetching data from any
database(like msaccess, db2, oracle, sqlserver, mysql).
System.Data.OleDb
Classes are like as OleDbDataAdapter, OleDbDataReader etc.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
The SqlConnection object
• The Connection object provides a connection to the database
• First Import - using System.Data.SqlClient; for SQL Server
– SqlConnection con = new SqlConnection(
"Data Source=(local);
Initial Catalog=Northwind; Connection String
Integrated Security=SSPI"
);
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
SqlCommand Object
• The Command object enables access to database commands to return
data, modify data, run stored procedures, and send or retrieve
parameter information.
• It provide three methods which are used to execute commands on the
database:
– ExecuteNonQuery: Executes commands that have no return
values such as INSERT, UPDATE or DELETE
– ExecuteScalar : Returns a single value from a database query
– ExecuteReader: Returns a result set by way of a DataReader
object
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
SqlDataAdapter Object
• We use Fill method to load data from the data source into the DataSet
and Update method to send changes made in the DataSet back to the
data source.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
SqlDataReader Object
• There is not possible to sort, filter, or manipulate the data while using
a DataReader, since it is read-only and forward-only.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
DataSet Object
• ADO.NET does not require that you keep a continuous live connection to
the database and process one retrieved record at a time.
• Additional classes are available that enable you to connect to the database
long enough to retrieve records into memory (DataSet).
• A dataset is a cache of records retrieved from some data source that may
contain one or more tables from the data source.
– The interaction between the dataset and the actual database is controlled
through a data adapter.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Differences between DataSet and DataReader
DataSet DataReader
DataSet object can contain multiple
rowsets from the same data source DataReader provides forward-only
as well as from the relationships and read-only access to data.
between them.
Dataset is a disconnected Datareader is connected
architecture. architecture.
Dataset can persist data. Datareader can not persist data.
A DataSet is well suited for data that
It has live connection while reading
needs to be retrieved from multiple
data
tables.
DatsSet is slower than DataReader,
Speed performance is better in
Due to overhead.
DataReader
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
SqlCommandBuilder Object
• It can only be used for datasets that map to a single database table.
• The data retrieved from the sql database table must have a primary key.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Practice
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Create Sql Server Database
• Create database named “ContactDB”
• GO
• USE ContactDB
• GO
• [MOBILE] VARCHAR(100))
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Design
textBoxId
textBoxName
textBoxMobile
buttonInsert
buttonUpdate
buttonDelete
dataGridViewContact
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Code
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Code cont…
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Code cont…
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Code cont…
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Contact Form Code cont…
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
END
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved