You are on page 1of 24

Starting out with Visual C#

Fourth Edition

12
Chapter 12
ADO.NET

C# Programming: From Problem Analysis to Program Design


4th Edition
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Topics
 Introduction to Ado.net
 ADO.NET Architecture
 Data Providers
 DataSet
 ADO.NET Namespaces
 Practice

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 is a set of classes used to communicate between an application


front end and a database to retrieve, manipulate, and update data in
databases.

• It offers the capability of working with databases in a connected or


disconnected mode.

• 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:

• .NET Framework Data Providers

• 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"
);

• There are four Connection String Parameter Name:-


1. Data Source: Identifies the server and it could be local machine, machine
domain name, or IP Address.
2. Initial Catalog: Database name.
3. Integrated Security: Set to SSPI or True to make connection with user's
Windows login.
4. User ID : Name of user configured in SQL Server, Password: Password
matching SQL Server User ID.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
The SqlConnection object (cont…)

• The following shows a connection string, using the User ID and


Password parameters:

– SqlConnection con = new SqlConnection(


"Data Source=PC-Server;
Initial Catalog=Northwind;
User ID=YourUserID;
Password=YourPassword");

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

• The DataAdapter serve as a bridge between a DataSet and data source


for retrieving and saving data.

• 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.

• 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

• The DataReader provides a high-performance stream of data from the


data source.

• It provides a forward-only, read only, connected recordset

 Limitations of the DataReader

• 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

• Command builder is used to generate SQL statements automatically so


that you do not have to do additional SQL programming beyond the initial
SELECT statement used to retrieve the records.

• 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.

• A primary key is a value that uniquely identifies the row.

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”

• Under ContactDB, create contact table


• CREATE DATABASE ContactDB

• GO

• USE ContactDB

• GO

• CREATE TABLE contact(

• [ID] INT PRIMARY KEY,


• [NAME] VARCHAR(100),

• [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

You might also like