Database Programming with ADO
Database Programming with ADO
Introduction to ADO.NET:
ADO is a rich set of classes, interfaces, structures, and enumerated types that manage
data access from various types of data stores.
Connected Architecture
Disconnected Architecture
King
The following figure shows how to work with the connected and disconnected
architectures.
Difference between
Connected and Disconnected Architecture
Connected Model Disconnected Model
.NET Application .NET Application
Open Connection
Run Commands
Manipulate Data
Close Connection
Data Data
Base Base
ADO.NET Architecture:
ADO.NET Components
The two main components of ADO.NET for accessing and manipulating data are the .NET
Framework data providers and the DataSet.
The .NET Framework Data Providers are components that have been explicitly designed
for data manipulation and fast, forward-only, read-only access to data.
King
The Connection object provides connectivity to a data source. The Command object
enables access to database commands to return data, modify data, run stored
procedures, and send or retrieve parameter information.
The DataReader provides a high-performance stream of data from the data source.
Finally, the DataAdapter provides the bridge between the DataSet object and the data
source.
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.
The .Net Framework data provider for SQL Server - provides access to Microsoft
SQL Server.
The .Net Framework data provider for OLE DB - provides access to data sources
exposed by using OLE DB.
The .Net Framework data provider for ODBC - provides access to data sources
exposed by ODBC.
The .Net Framework data provider for Oracle - provides access to Oracle data
source.
The EntityClient provider - enables accessing data through Entity Data Model
(EDM) applications.
2. The DataSet
The ADO.NET DataSet is explicitly designed for data access independent of any data
source. As a result, it can be used with multiple and differing data sources, used with
XML data, or used to manage data local to the application.
The DataSet contains a collection of one or more DataTable objects consisting of rows
and columns of data, and also primary key, foreign key, constraint, and relation
information about the data in the DataTable objects.
The following diagram illustrates the relationship between a .NET Framework data
provider and a DataSet.
King
The DataSet class is present in the System.Data namespace. The following table
describes all the components of DataSet –
DataSet Components
1. DataTableCollection - It contains all the tables retrieved from the data source.
2. DataRelationCollection - It contains relationships and the links between tables in a data
set.
3. ExtendedProperties - It contains additional information, like the SQL statement for
retrieving data, time of retrieval, etc.
4. DataTable - It represents a table in the DataTableCollection of a dataset. It consists of the
DataRow and DataColumn objects. The DataTable objects are case-sensitive.
5. DataRelation - It represents a relationship in the DataRelationshipCollection of the
dataset. It is used to relate two DataTable objects to each other through the DataColumn
objects.
6. DataRowCollection - It contains all the rows in a DataTable.
7. DataView - It represents a fixed customized view of a DataTable for sorting, filtering,
searching, editing and navigation.
8. PrimaryKey - It represents the column that uniquely identifies a row in a DataTable.
9. DataRow - It represents a row in the DataTable. The DataRow object and its properties
and methods are used to retrieve, evaluate, insert, delete, and update values in the
DataTable. The NewRow method is used to create a new row and the Add method adds a
row to the table.
10. DataColumnCollection - It represents all the columns in a DataTable.
ADO.NET Architecture
Connection DataTableCollection
DataRowCollection
DataAdapte
Command r DataColumnsCollectio
n
ConstraintsCollectio
DataReader n
DataRelationCollection
Database
The data residing in a data store or database is retrieved through the data provider.
Various components of the data provider retrieve data for the application and update
data.
Datasets store data in a disconnected cache and the application retrieves data
from it.
Data Readers provide data to the application in a read-only and forward-only
mode.
When you decide whether your application should use a DataReader (see Retrieving
Data Using a DataReader) or a DataSet (see DataSets, DataTables, and DataViews),
consider the type of functionality that your application requires. Use a DataSet to do the
following:
Cache data locally in your application so that you can manipulate it. If you only
need to read the results of a query, the DataReader is the better choice.
Remote data between tiers or from an XML Web service.
King
If you do not require the functionality provided by the DataSet, you can improve the
performance of your application by using the DataReader to return your data in a
forward-only, read-only manner.
Although the DataAdapter uses the DataReader to fill the contents of a DataSet , by
using the DataReader, you can boost performance because you will save memory that
would be consumed by the DataSet, and avoid the processing that is required to create
and fill the contents of the DataSet.
ADO.NET Objects
ADO.NET Objects
1. Connection - This component is used to set up a connection with a data source.
2. Command - A command is a SQL statement or a stored procedure used to
retrieve, insert, delete or modify data in a data source.
3. DataReader - Data reader is used to retrieve data from a data source in a read-
only and forward-only mode.
4. DataAdapter - This is integral to the working of ADO.Net since data is transferred
to and from a database through a data adapter. It retrieves data from a database
into a dataset and updates the database. When changes are made to the dataset,
the changes in the database are actually done by the data adapter.
Connection Object
1) One of the first ADO.NET objects is the connection object that allows you to
establish a connection to a data source.
2) The connection objects have the methods for opening and closing connections, for
beginning a transaction of data.
3) The .Net Framework provides two types of connection classes:
The sqlconnection object, that is designed specially to connect to Microsoft SQL
Server and the OleDbConnection object, that is designed to provide connection to
a wide range of databases, such as Microsoft Access and Oracle.
King
Connection String:
The connection string is different for each of the various data providers available in .NET.
No Provider Description
1 System.Data.SqlClient Provides data for Microsoft SQL Server
2 System.Data.OleDb Provides data access for data sources exposed using OLE DB
3 System.Data.Odbc Provides data access for data source exposed using ODBC.
4 System.Data.OracleClient Provides data access for Oracle.
Example:
Imports System.Data.SqlClient
King
End Sub
End Class
Provider;Data Source;
Example:
Imports System.Data.OleDb
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Me.EmployeesTableAdapter.Fill(Me.NwindDataSet.Employees)
End Sub
Example:
Imports System.Data.Odbc
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Me.ProductsTableAdapter.Fill(Me.DataSet1.Products)
End Sub
King
End Sub
End Class
Dsn=myDSN;UID;Pwd;
Imports System.Data
Imports Oracle.DataAccess.Client ' ODP.NET Oracle managed provider
Imports Oracle.DataAccess.Types
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim oradb As String = "Data Source=orcl;User Id=System;Password=king;"
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim cmd As New OracleCommand
cmd.Connection = conn
cmd.CommandText = "select department_name from departments where department_id = 10"
cmd.CommandType = CommandType.Text
Dim dr As OracleDataReader = cmd.ExecuteReader()
dr.Read()
Label1.Text = dr.Item("department_name")
conn.Dispose()
End Sub
End Class
The Oracle Data Provider for .NET (ODP.NET) is Oracle's high performance ADO.NET 2.0
compliant data provider that exposes a complete set of Oracle specific features and
tuning options including support for Real Application Clusters, XML DB, and advanced
security. It is available for free download from the Oracle Technology Network website.
King
When ODP.NET and any required Oracle client connectivity software is installed,
application development using Visual Studio can begin. It is a good idea to confirm client
connectivity before starting development. If you can connect to Oracle using SQL*Plus
on the same machine as Visual Studio, then you know that your Oracle client-side
software is properly installed and configured.
Prerequisites:
1. Install Microsoft Visual Studio 2010
2. Install Oracle Database 9.2 or later or Oracle Database XE
3. Install Oracle 11g Oracle Data Access Components (ODAC) with Oracle Developer
Tools for Visual Studio version 11.2.0.1.2 or later from OTN
Adding a Reference
Because your project needs access to an Oracle database, it is necessary to add a
reference to the dll containing the data provider.
2. Scroll down the list of Component Names and select Oracle.DataAccess. Click OK.
--End--