Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Database Programming with ADO

The document provides an overview of ADO.NET, a set of classes and interfaces for data access in .NET applications, enabling interaction with various relational databases. It describes the two main architectures of ADO.NET: connected and disconnected, and outlines key components such as .NET Framework data providers and DataSet. Additionally, it explains the use of connection strings for different data providers and includes code examples for establishing database connections and retrieving data.

Uploaded by

kmohmmad728
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Database Programming with ADO

The document provides an overview of ADO.NET, a set of classes and interfaces for data access in .NET applications, enabling interaction with various relational databases. It describes the two main architectures of ADO.NET: connected and disconnected, and outlines key components such as .NET Framework data providers and DataSet. Additionally, it explains the use of connection strings for different data providers and includes code examples for establishing database connections and retrieving data.

Uploaded by

kmohmmad728
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

 King

Database Programming with ADO.NET:

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.

 Enterprise applications handle a large amount of data. This data is primarily


stored in relational databases, like Oracle, SQL Server, Access, and so on. These
databases use Structured Query Language (SQL) for retrieval of data.
 To access enterprise data from a .NET application, an interface was needed. This
interface acts as a bridge between an RDBMS system and a .Net application.
ADO.NET is such an interface that is created to connect .NET applications to
RDBMS systems.
 In the .NET framework, Microsoft introduced a new version of Active X Data
Objects (ADO) called ADO.NET. Any .NET application, either Windows-based or web-
based, can interact with the database using a rich set of classes of the ADO.NET
library. Data can be accessed from any database using connected or disconnected
architecture.
 There were many data access technologies available prior to ADO.NET, primarily
the following:
o Open Database Connectivity (ODBC)
o Data Access Objects (DAO)
o Remote Data Objects (RDO)
o Active X Data Objects (ADO)
 ADO is a simple component-based object-oriented interface to access data
whether relational or non-relational databases. It is a successor of DAO and RDO.
 ADO reduces the number of objects. Their properties, methods, and events.
 ADO is built on COM; specifically Activex
 ADO supports universal data access using Object Linking and Embedding for
DataBases (OLEDB). This means that there are no restrictions on the type of data
that can be accessed.

ADO.NET provides mainly the following two types of architectures:

 Connected Architecture
 Disconnected Architecture
 King

Connected and Disconnected Architectures:

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

Retrieve Data as Client Side


Open Connection
Close Connection

Run Commands
Manipulate Data

Retrieve Results Open Connection

Close Connection Update Tables

Close Connection

Data Data
Base Base

ADO.NET Architecture:

Data processing has traditionally relied primarily on a connection-based, two-tier model.


As data processing increasingly uses multi-tier architectures, programmers are switching
to a disconnected approach to provide better scalability for their applications.

ADO.NET Components

The two main components of ADO.NET for accessing and manipulating data are the .NET
Framework data providers and the DataSet.

1 .NET Framework Data Providers

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.

There are following different types of data providers included in ADO.Net

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

11. DataColumn - It consists of the number of columns that comprise a DataTable.


 King

ADO.NET Architecture

.NET Providers DataSet

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.

An application accesses data either through a dataset or a data reader.

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

Choosing a DataReader or a DataSet:

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

 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 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 and Connection String:

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

4) A connection is required to interact with the database. A Connection object helps


to identify the database server name, user name and password to connect to the
database. The Connection object is used by commands on the database.
5) A Connection object has the responsibility of establishing a connection with the
data store.
6) How to use the Sqlconnection object:
 Instantiate the SqlConnection class.
 Open connection.
 Pass the connection to ADO.NET objects.
 Perform the database operations with ADO.NET object.
 Close the connection.

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.

Format for all 4 Data Providers Connection Strings :

1. Connection String for SQL:

a) Data Source;Initial Catalog;Integrated Security;User Instance;

b) Data Source;Initial Catalog;Integrated Security;User ID; Password;

Sr. Connection String


Description
No. Parameter Name
Identify the server. Could be a local machine, machine domain name, or IP
1 Data Source
Address.
2 Initial Catalog Data base name.
Integrated
3 Set to SSIP to make a connection with the user's window log in.
Security
4 User ID Name of user configured in SQL Server.
5 Password Password matching SQL Server User ID

Example:

Imports System.Data.SqlClient
 King

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
Me.CustomersTableAdapter.Fill(Me.NORTHWNDDataSet.Customers)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
Dim connection As SqlConnection = New SqlConnection()
connection.ConnectionString = "Data Source=King-pc\SQLEXPRESS;Initial
Catalog=northwnd;Integrated Security=True; User Instance=True"
connection.Open()
Dim adp As SqlDataAdapter = New SqlDataAdapter("select * from Customers", connection)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
DataGridView2.DataSource = ds.Tables(0)

End Sub
End Class

2. Connection String for OLEDB:

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

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
Dim con As OleDbConnection = New OleDbConnection()
con.ConnectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\\Nwind.accdb")
Dim da As OleDbDataAdapter = New OleDbDataAdapter("select * from Employees", con)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
DataGridView2.DataSource = ds.Tables(0)
End Sub
End Class

3. Connection String for ODBC:

Data Source;Integrated Security;

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

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles


Button1.Click
Dim connection As OdbcConnection = New OdbcConnection()
connection.ConnectionString = "Dsn=myDSN;UID=;Pwd=;"
Dim SQL As String = "select * from Products"
connection.Open()
Dim cmd As OdbcCommand = New OdbcCommand(SQL, connection)
Dim dr As OdbcDataReader = cmd.ExecuteReader()

Dim ds As DataSet = New DataSet()


Dim dt As DataTable = New DataTable("Shippers")
dt.Load(dr)
ds.Tables.Add(dt)
DataGridView2.AutoGenerateColumns = True
DataGridView2.DataSource = ds.Tables(0)
connection.Close()
dr.Close()
cmd.Dispose()
connection.Dispose()

End Sub
End Class

4. Connection String for OracleClient:

Dsn=myDSN;UID;Pwd;

Example: (Read following note for Oracle Connectivity)

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

NOTE for ORACLE Connectivity:


In addition to basic Oracle client connectivity software, .NET applications require the use
of what is known as a managed data provider (where "managed" refers to code
managed by the .NET framework). The data provider is the layer between the .NET
application code and the Oracle client connectivity software.

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.

Perform the following steps:


1. From Project menu, select Add Reference...

2. Scroll down the list of Component Names and select Oracle.DataAccess. Click OK.

--End--

You might also like