You are on page 1of 48

Unit- V

ADO.NET
R.SHEEBA M.SC.,M.PHIL.,
ASSISTANT PROFESSOR,
DEPARTMENT OF COMPUTER SCIENCE
MADURAI SIVAKASI NADARS PIONEER MEENAKSHI
WOMEN’S COLLEGE, POOVANTHI
Introducing ADO.NET and Data Management

 Most applications need to handle data, whether it is in the form of a dataset, a text file, or a

spreadsheet. The majority of modern-day applications need to deal with various types of databases.

Therefore, to access this data the application needs to interact with various databases, such as Microsoft

SQL Server, Oracle, Microsoft Access and so on.

 ASP.NET application might use a database “behind the scenes” to retrieve information, which is then

processed and displayed in the appropriate format and location in the browser.
 ADO.NET is a part of the .NET framework architecture. It is a model used by .NET
applications to communicate with a database for retrieving, accessing, and updating data.
E-Commerce Sites

• Managing sales, customer, and inventory information.


E-Commerce SQL Databases
Online Knowledge Based and Customized Search Engines

 Involve less structured databases that store vast quantities of information, or links to
various documents and resources.
Google Knowledge Graph

 The “knowledge graph” is a database that collects millions of pieces of data about keywords people frequently
search for on the World wide web and the intent behind those keywords, based on the already available content.
With the knowledge graph, users can get information about people, facts and places that are interconnected
in one way or the other.

 Google’s goal is to provide accurate information in response to users’ search queries, within the
shortest possible time.

 what exactly is the Knowledge Graph? It’s Google’s systematic way of putting facts, people and
places together, to create interconnected search results that are more accurate and relevant.
just go to Google
and search for
“what is the
knowledge
graph?”
The answer is
displayed right
there – and that’s
also what the
Knowledge Graph
does.
 If you do a search these days, you’ll notice that Google bolds your keywords in the meta

      
description.

The keywords in
the this screenshot
are bolded, even
when some of the
search terms are
missing. Google is
mainly looking at
the relevance of the
page to what the
user is seeking.
Information-based sites such as web portals

 A web portal is a specially designed


website that brings information
from diverse sources, like emails,
online forums and search engines,
together in a uniform way.
Overview of Databases

 Database – Most common way to manage data.


 Database Technology – Useful for Business software which typically
requires hierarchical sets of related information.
 Example: (Relational Model)
 Sales Ordering program consists
 List of customers
 List of products
 List of sales
In relational model, a information is broken down into its smallest and most concise units.
A sales record doesn’t store all the information about the product that were sold; instead it
just stores a product ID that refers to a full record in a product table.
XML and ADO.NEt

 To create these tables, add all the information you need, and store it as an XML file using
ADO.NET.
 Additional products used for flexibility
 SQL server
 The SQL Server is a relational database management system from Microsoft. The system
is designed and built is to manage and store information. The system supports various
business intelligence operations, analytics operations, and transaction processing.
 SQL Server’s Enterprise Manger – Way to design and create tables in an SQL Server
database.
Characteristics of ADO.NET

 Disconnected Access
 Native XML
 Extended Data Format
 Managed Code
 Data Providers
Disconnected Access

 Disconnected data access is achieved through the DataAdapter object.


 This object establishes connection, executes the command, load data in the
DataSet.
 The dataset works independent of database.
Native XML

 ADO.NET uses XML natively to store data.

 Native support for XML is another principal feature for ADO.NET.

 When you retrieve information from a database into an ADO.NET Dataset, the data is
stored in memory in an XML format.

 We can modify values, remove rows, and add new records using XML Document Object
Model (DOM), which are available in .NET through the types in the System.Xml
namespaces.
XML File Format
Managed Code

 ADO.NET is built in the managed code environment, which means it can be used easily
in other .NET applications(Such as ASP.NET programs).
Data Providers

 The . Net Framework includes mainly three Data Providers for ADO.NET.

 The Microsoft SQL Server , OLEDB and ODBC are the main Data Providers included in
the . Net Framework.
Extended Data Format

 ADO.NET has a self-sufficiency that ADO never had.

 ADO.NET retains more information to allow you to effectively make changes without
needing to repeatedly reconnect to the database.

 These changes are found in DataSet object, ADO.NET’s replacement for the Recordset.

 ADO.NET allows you to add information for several interrelated tables, link them
together, disconnected relational database.
Comparing ADO and ADO.NET
The ADO.NET object model
Data Namespaces
 The ADO.NET components live in five main namespaces in the .NET class library.

Namespaces Description
System.Data This namespace is the core of ADO.NET framework. DataSet is the most import
class of this namespace. It also contains classes to represent tables, columns,
rows, relation and the constraint.

System.Data.Common It provides common classes, those works as base class. These classes are used
by all data providers. Examples are DbConnection and DbDataAdapter

System.Data.OleDb It provides classes that work with OLE-DB data sources using the .NET OleDb
data provider. If you are using the Microsoft Access as a database, then
System.Data.OleDb namespace will be used. Example of these classes are
OleDbConnection, OleDbCommand
Namespaces Description
System.Data.Odbc This namespace defines classes that work with the ODBC data
sources using the .NET ODBC data provider. It is collection of
classes used to access an ODBC data source. It contains classes
such as OdbcConnection, OdbcCommand
and OdbcDataReader
System.Data.SqlClient It defines the data provider for the SQL Server database. It contains
classes that are used to access a SQL Server database. Examples are
SqlConnection, SqlCommand and
SqlDataReader
System.Data.SqlTypes This namespace provides classes for specific data types for the SQL
Server database.
The ADO.NET objects

 ADO.NET relies on the functionality in a few core objects. These


objects can be divided into two groups:
 Those that are used to contain and manage data

(DataSet, DataTable, DataRow, and DataRelation)

 Those that are used to connect to a specific data source.


(the Connections, Commands, and DataReader classes)
 In the ADO.NET object model, the data residing in a database is

retrieved through a data provider. The data provider is the set of

components including the Connection, Command, DataReader, and

DataAdapter objects. An application can access data either through a

dataset or through a datareader object.

 The ADO.NET object model consists of two fundamental

components:

• Data Provider

• DataSet
The Data Objects

 The data objects allow you to store a local, disconnected copy of data. They don’t store a connection to
a data source.
 The data modelling objects
Data Provider

Selecting an appropriate data provider for a client application depends on the type of
data source being accessed. There are four .Net data providers available.
1. SQL Server: It's used to work specifically with Microsoft SQL Server. It exists in a
namespace within the System.Data.SqlClient.
2. OLE DB: It's used to work with the OLEDB provider. The System.Data.dll assembly
implements the OLEDB .NET framework data provider in the System.Data.OleDb
namespace.
3. ODBC: To use this type of provider, you must use an ODBC driver. The
System.Data.ODBC.dll assembly implements the ODBC .NET framework data provider.
This assembly is not part of the Visual Studio .NET installation.
4. Oracle: The System.Data.OracleClient.dll assembly implements the Oracle  .NET
framework data provider in the System.Data.OracleClient namespace. The Oracle client
software must be installed on the system before you can use the provider to connect to
an Oracle data source.
Data Provider Components

The four key components of a dataprovider are:

- Connection, Command, DataReader and DataAdapter 


Object Description
Connection It is used to establish a connection to a specific data source.
Command It is used to execute queries to perform database operations. This component
retrieves, inserts, deletes, and modifies data in a data source.
DataReader It is used to read data from data source. The DbDataReader is a base class for all
DataReader objects. This component retrieves data from a data source
in read-only and forward mode.
DataAdapter It populates a DataSet and resolves updates with the data source. The base class
for all DataAdapter objects is the DbDataAdapter class.
DataSet

 The DataSet class stores disconnected information drawn from a database


and allows you to manipulate in as a single, neatly packaged object.

 The DataSet class has no direct connection to a data source.

 DataSet contains two main types of objects:

 DataTables (provided in the table Property)

 DataRelations (provided in the Relations property)


 DataSet support the following methods:

 WriteXML and ReadXML - Allow the DataSet to be serialized to an XML file and read

back from a file.

 Merge - Adds the contents of one DataSet into another.

 Clear - Removes all the information in the DataSet.

 AcceptChanges, GetChanges, and RejectChanges – Allow you to work with the

modified data, examining, implementing, or reverting changes.


DataTable

 You can add information into a DataSet one table at a time.

 The DataTable object represents one of these tables.

 It contains a collection of DataRows, which contains all the data in the table.

 It also provides a collection of DataColumns, which contain information about each column in the table,
and a collection of constraints, which specifies additional column metadata such as the primary key.

 The DataTable class includes a Select method that allows you grab a subset of rows that match a specified
filter expression, in a specified sort order.

 These rows are returned as an array, not another DataTable.


DataRow

 Each DataRow represents a single row of information in the table.

 You can access individual values using the field name or field index.

 You can add new rows by using the following methods:

 BeginEdit, EndEdit and CancelEdit – Allows you to use edit mode to make a series of changes
to a row, and apply or cancel them all at once.

 Delete – Marks a row as deleted, and hides it from the default view used for the table.

 GetChildRows – Uses the table relations you have defined to get rows from a linked table.
DataColumn

 It stores information about the column, such as column’s data type, its default value, and
various restrictions.
DataRelation

 Each DataRelation object specifies a single parent/child relationship between two


different tables in a DataSet.

 For example: A DataRelation could indicate that the CustomerID column in the Orders
table corresponds to the CustomerID column in the Customers table.
DataView

 The DataView object provides a window onto a DataTable.

 Each DataTable has at least one DataView, which is used for data binding.

 The DataTable object is only concerned with data content.

 It also allows you to format a single DataTable in more than one way by creating separate
DataView objects.
The Data Source Interaction Objects

 The goal of the data source object is to create a connection and move information into a
DataSet or into a DataReader.

 The simplest way of accessing a database-going straight to the source with Command
objects and retrieving read-only rows through a DataReader.
Direct Data Access with ADO.NET
Using a DataSet with ADO.NET
Sql Connection and OleDbConnection

 These object allows you to establish a connection, which is the first


step in any database operation.

 They also allow you to start a transaction.

 These objects are similar to ADO’s Connection object.


Sql Command and OldDbCommand

 These objects represent an SQL statement or stored procedure that you can use against a data source to retrieve,
update, or modify data.

 You set information into a command using CommandType and CommandText properties.

 You can add any parameters you need using SqlParameter or OleDbParameter objects.

 To actually use your command, you must use a DataAdapter or use the following methods:

 ExecuteNonQuery

 ExecuteReader

 ExecuteScalar
These are the most common methods of the SqlCommand class as follows.

 ExecuteReader – Executes select SQL statements which can either be in your .NET code
or in the stored procedure. Returns a “DataReader” object.

 ExecuteNonQuery – Executes insert, update and delete SQL commands. Returns an


integer indicating the number of rows affected by the query.

 ExecuteScalar – Executes SQL command and returns only a single value like count, sum,
first record, etc.
SqlDataReader and OleDbReader

 There are two types of DataReader in ADO.NET. They are SqlDataReader and the OleDbDataReader.
The System.Data.SqlClient and System.Data.OleDb are containing these DataReaders respectively. 

 These objects provide a steady stream of data.

 They are fastest way to read data when all you want to do is display in a web page.

 They maintain a live connection with a data source, but they don’t allow any changes.

 To get a row of information from an SqlDataReader and OleDbReader object, you use the Read
method.
SqlDataAdapter and OleDbDataAdapter

 These objects, which derive from the common DataAdapter class, acts as a bridge between a Command
and a DataSet.

 For example: You can create an SQL statement that selects a set of rows, create a command that
represents it, and use the SqlDataAdapter or OleDbDataAdapter object to automatically retrieve all the
matching values and insert them into a supplied DataSet.

 Every DataAdapter can hold a reference to four different commands, one for each type of operation.
You set these commands through the DeleteCommand, InsertCommand, SelectCommand, and
UpdateCommand properties.
 Once you have set the appropriate commands, you can use one of the following methods:
 Fill – Executes the SelectCommand and places the results into the DataSet you supply.
 FillSchema – Uses the SelectCommand to retrieve information about a table, such as column
constraints, and add it to a DataSet. No data rows are transferred.
 Update – Scans through the supplied DataSet, and applies all the changes it finds to the data
source. In the process, it uses InsertCommand, UpdateCommand, or DeleteCommand as
required. For example: If it finds new rows that didn’t exist before, it uses the InsetCommand.
 Next Topic to be continued : SQL Basics

You might also like