View
214
Download
0
Category
Preview:
Citation preview
8/8/2019 Data Access Using ADODotNet
1/22
Data Access using ADO.Net
Lecture No:30
8/8/2019 Data Access Using ADODotNet
2/22
Introducing ADO.Net
ADO.Net is an object oriented framework
that allows you to interact with database
systems ADO.NET is an object-oriented set of
libraries that allows you to interact with
data sources. Commonly, the data source
is a database, but it could also be a text
file, an Excel spreadsheet, or an XML file.
8/8/2019 Data Access Using ADODotNet
3/22
Traditional architecure
We usually interact with database systemsthrough SQL queries or stored procedures. Intraditional data access components, you made aconnection to the database system and then
interacted with it through SQL queries using theconnection.The application stays connected tothe DB system even when it is not using DBservices. This commonly wastesvaluable andexpensive database resources, as most of thetime applications only query and view thepersistentdata.
8/8/2019 Data Access Using ADODotNet
4/22
ADO.NET The best thing aboutADO.Net is that it is extremely
flexible and efficient.
ADO.Net also introduces the concept of a disconnecteddataarchitecture.
ADO.Net solves this problem by managing a local buffer
of persistent data called a data set. Your application automatically connects to the database
server when it needs to run a query and thendisconnects immediately after getting the result back andstoring it in the dataset.
This design ofADO.Net is called a disconnected dataarchitecture and is very much similar to theconnectionless services of HTTP on the internet.
It should be noted that ADO.Net also providesconnection oriented traditional data access services.
8/8/2019 Data Access Using ADODotNet
5/22
Traditional Data Access
Architecture
Connect to DB
Pass query1 and access data from DB
After some time Pass query2 and accessfrom DB
After some time Pass query3 and access
fromDB
Disconnect DB
8/8/2019 Data Access Using ADODotNet
6/22
ADO.Net Disconnected Data
Access
Architecture
Connect to DB
Pass query1 and access data from DB
Disconnect
DB
After some time, Connect to DB, Passquery2 and access from DB, DisconnectDB
After some time, Connect to DB, Passquery2 and access from DB, DisconnectDB
8/8/2019 Data Access Using ADODotNet
7/22
ADO.Net Disconnected Data
Access
Architecture
Another important aspect of disconnected
architecture is that it maintains a local repository
of data in the dataset object. The dataset object
stores the tables, their relationship and theirdifferent constraints. The user can perform
operations like update, insert and delete on this
dataset locally, and the changes made to the
dataset are applied to the actual database as abatch when needed. This greatly reduces
network traffic and results in better performance
8/8/2019 Data Access Using ADODotNet
8/22
Advantages and Disadvantages of
ADO.NET Advantages:
It is disconnected data architecture. Here data isretrieved and cached on your local machine.The biggest advantage here is reduced burdenon the database server and helps ourapplication to scale well.
Disadvantages:
A cursor is a database element that controls
record navigation, the ability to update data, andthe visibility of changes made to the database byother users.ADO.NET does not have aninherent cursor object
8/8/2019 Data Access Using ADODotNet
9/22
Discriminate ADO and ADO.Net
As in classic ADO we had client and serverside cursors they are no more present in
ADO.NET.
Note it's a disconnected model so they are nomore applicable.
Locking is not supported due to disconnectedmodel.
All data persist in XML as compared to classicADO where data persisted in Binary formatalso.
8/8/2019 Data Access Using ADODotNet
10/22
8/8/2019 Data Access Using ADODotNet
11/22
The Namespace in which .NEThave the
data functionality classes
System.data This contains the basic objects used for accessingand storing relational data, such as DataSet,DataTable, andDataRelation. Each of these is independent of the type of datasource and the way we connect to it.
System.Data.OleDB It contains the objects that we use toconnect to a data source via an OLE-DB provider, such asOleDbConnection, OleDbCommand, etc.
System.Data.SqlClient: This contains the objects that we use to
connect to a data source via the TabularData Stream (TDS)interface of Microsoft SQL Server (only).
System.XML This Contains the basic objects required to create,read, store, write, and manipulate XML documents according toW3C recommendations.
8/8/2019 Data Access Using ADODotNet
12/22
Different components of ADO.Net
All generic classes for data access are contained in
theSystem.Data namespace. Class Description
DataSet The DataSet is a local buffer of tables or acollection of disconnected record sets.
DataTable ADataTable is used to contain data in
tabular form using rows and columns. DataRow Represents a single record or row in a
DataTable.
DataColumn Represents a column or field of aDataTable.
DataRelation Represents the relationship betweendifferent tables in a data set.
Constraint Represents the constraints or limitationsthat apply to a particular field or column.
8/8/2019 Data Access Using ADODotNet
13/22
ADO.NET Data Providers ADO.NET Data Providers are class libraries that allow a common way to interact with specific
data sources or protocols.
Provider Name Data Source Description
ODBCData Provider OdbcData Sources with an ODBC interface.
Normally older data bases
OleDb Data Provider Sources that expose an OleDb interface, i.e.Access
or Excel
Oracle Data Provider ForOracle Databases
SQL Data Provider For interacting with Microsoft SQL Server
Borland Data Provider Generic access to many databases such as Interbase,
SQL Server, IBM DB2, and Oracle.
8/8/2019 Data Access Using ADODotNet
14/22
The Managed providers that are
currently available withADO.
NET SQL server managed provider
OLE DB managed provider.
ODBC managed provider. managed provider forORACLE.
8/8/2019 Data Access Using ADODotNet
15/22
ADO.NET CLASSES
ADO.Net also contains some database specific classes. This meansthat different database system providers may provide classes (ordrivers) optimized for their particular database system.
Microsoft itself has provided the specialized and optimized classesfor their SQL server database system. The name of these classes
start with 'Sql and are contained in the System.D
ata.Sql
Clientnamespace.
Similarly, Oracle has also provides its classes (drivers) optimized forthe Oracle DB System.
Microsoft has also provided the general classes which can connectyour application to any OLE supported database server. The nameof these classes start with 'OleDb' and these are contained in theSystem.Data.OleDb namespace.
In fact, you can use OleDb classes to connect to SQL server orOracle database; using the database specific classes generallyprovides optimized performance.
8/8/2019 Data Access Using ADODotNet
16/22
ADO.NET CLASSES
SqlConnection, OleDbConnectionRepresents a connection to the databasesystem.
SqlCommand, OleDbCommand RepresentsSQL query.
SqlDataAdapter, OleDbDataAdapter A classthat connects to the database system, fetchesthe record and fills the dataset.
SqlDataReader, OleDbDataReader A streamthat reads data from the database in aconnected design.
SqlParameter, OleDbParameter Represents aparameter to a stored procedure
8/8/2019 Data Access Using ADODotNet
17/22
Connection object
Command object
DataReader object DataSet object
DataAdapter object
ADO.NET Objects
8/8/2019 Data Access Using ADODotNet
18/22
The SqlConnection Object
To interact with a database, you must
have a connection to it. The connection
helps identify the database server, the
database name, user name, password,
and other parameters that are required for
connecting to the data base. A connection
object is used by command objects sothey will know which database to execute
the command on.
8/8/2019 Data Access Using ADODotNet
19/22
The SqlCommand Object
he process of interacting with a database meansthat you must specify the actions you want tooccur. This is done with a commandobject. You use a command object to send SQL
statements to the database. A command objectuses a connection object to figure out whichdatabase to communicate with. You can use acommand object alone, to execute a commanddirectly, or assign a reference to a command
object to an SqlDataAdapter, which holds a setof commands that work on a group of data asdescribed below.
8/8/2019 Data Access Using ADODotNet
20/22
The SqlDataReader Object
Many data operations require that you only get astream of data for reading. The data readerobject allows you to obtain the results of a
SELEC
T statement from a command object.
Forperformance reasons, the data returned from adata reader is a fast forward-only stream ofdata. This means that you can only pull the datafrom the stream in a sequential manner. This is
good for speed, but if you need to manipulatedata, then a DataSet is a better object to workwith.
8/8/2019 Data Access Using ADODotNet
21/22
The DataSet Object
DataSet objects are in-memory representationsof data. They contain multiple Datatable objects,which contain columns and rows, just like normaldatabase tables. You can even define relations
between tables to create parent-childrelationships. The DataSet is specificallydesigned to help manage data in memory and tosupport disconnected operations on data, whensuch a scenario make sense. The DataSet is an
object that is used by all of the Data Providers,which is why it does not have a Data Providerspecific prefix.
8/8/2019 Data Access Using ADODotNet
22/22
The SqlDataAdapter Object Sometimes the data you work with is primarily read-only and yourarely need to make changes to the underlying data source. Some
situations also call for caching data in memory to minimize thenumber of database calls for data that does not change.
The data adapter makes it easy for you to accomplish these thingsby helping to manage data in a disconnected mode.
The data adapter fills a DataSet object when reading the data andwrites in a single batch when persisting changes back to thedatabase.
A data adapter contains a reference to the connection object andopens and closes the connection automatically when reading fromor writing to the database.
Additionally, the data adapter contains command object references
for SELECT, INSERT, UPDATE, and DELETE operations on thedata. You will have a data adapter defined for each table in aDataSet and it will take care of all communication with the databasefor you. All you need to do is tell the data adapter when to load fromor write to the database.
Recommended