Sunteți pe pagina 1din 23

Software Programming

ADO.NET

Contents
Introduction ADO.NET
The Connection Object
The Command Object
Reading Data with the DataReader
Working with Disconnected Data - The DataSet and
DataAdapter
Adding Parameters to Commands
Using Stored Procedures

Introduction ADO.NET
ADO:- Active Data Objects.
The primary data access technology to connect into a
database from .NET is ADO.NET.
ADO.NET use for (01) connect to a database (02)
manipulate the data.
ADO.NET has support for XML(Extensible Markup Language)
data representation.

Introduction ADO.NET
ADO.NET was built for Online or Connected mode and
offline or disconnected mode.
connected Method
When you are making use of the connected layer, your code base will
explicitly connect to and disconnect from the underlying data store.
When you are using ADO.NET in this manner, you interact with the
data store using connection objects, command objects, and data
reader objects.

Introduction ADO.NET
disconnected Method
The disconnected layer, allows you to obtain a set of DataTable objects
(contained within a DataSet) that functions as a client-side copy of the
external data.
When you obtain a DataSet using a related data adapter object, the
connection is automatically opened and closed on your behalf.
This approach helps quickly free up connections for other callers.
The client receives a DataSet, it is able manipulate the contents without
cost of network .
If the client wishes to submit the changes back to the data store, the data
adapter (in conjunction with a set of SQL statements) is used once again to
update the data source, at which point the connection is closed immediately.

ADO.NET Architecture or Diagram view

Data Providers
The data provider is used to interact between the
database and your program.
Function of the Data Provider:
Connecting to the database
Retrieving data from the database
Updating the data in the database
Deleting the data in the database,
Other functions .

ADO.NET supports multiple data providers, each of which is


optimized to interact with a specific DBMS.

Data Providers cont..


ADO.NETs main three Data Providers are
Microsoft SQL Server Data Provider
OLEDB Data Provider
ODBC Data Provider .

Data Providers
Calling base class of Data provider in your program.
using System.Data.OleDb;
using System.Data.ODBC;
using System.Data.SqlClient;

Data Providers
Four key classes are used as a part of the data provider (xxx
mean Data provider name)
xxxConnection

Used to connect to the database.


(SqlConnection)

xxxCommand

Used to execute a command against the


database.
(SqlCommand)

xxxDataReader

A special class used to get a set of data


from the database that you can view. This
can be viewed only sequentially, and the
data cant be changed. Retrieval of query
results from the Data Source
(SqlDataReader)

xxxDataAdapter

Used to get a set of data from the


database that you can then manipulate.
You use this to make the changes to the
database as well
(SqlDataAdapter)

OLEDB Data Provider


OleDb Data provider is using for Access Database.
OleDbs classess are
OleDbConnection
OleDbCommand
OleDbDataReader
OleDbDataAdapter

Creating a Connection to the Database


Database is Access, so we are going to use OleDb data
provider.
To open a database, first thing you must connect to
database.
You create a connection by using the 'xxxConnection' class.

Creating a Connection to the Database


Using this class, you create a connection object:

As you can see, conn is created in the same way that other
objects are created.

Creating a Connection to the Database


Connection sting:

Creating an OledbCommand Object


OleDbCommand object allows you to specify what type of
interaction you want to perform with a database.
For example, you can do select, insert, modify, and delete
commands on rows of data in a database table.
I am going to select data from database.

Open Database connection.


Now we are going to open a connection to Database using
conn object.

Executing Command Object


How you execute a command depends on the result you expect
from the command.
You will consider executing a command generally in three ways:
ExecuteReader
ExecuteNonQuery
ExecuteScalar

You use ExecuteReader if you plan to retrieve data from the


database.
If you plan to make changes to the database but dont expect
values to be returned, you can use ExecuteNonQuery.
Finally, if you want to get just a single value from the database, you
can use ExecuteScalar.

Retrieving Data with a DataReader


A special class has been created to enable you to easily and
efficiently read data from a database.This is the
DataReader.
The DataReader are that it can only read data (it cant
write)
It does only a forward read. This means you can go through
the data only once.

After execution
After it is executed, myDataReader will contain the results of the
command that you assigned.
This result will most likely be a set of records from the database.
You can now loop through these records by calling the Read method
of the DataReader.
Each time you call the Read method, the next record is read.
You can access the values in each record by getting their values with
a Getxxx method.
To use most of these methods, you must know the data type of the
information you are retrieving.
If you dont know the type, you will want to use the GetValue
method.

After execution

Read Data from DataReader

Final Coding

Summary
Connection
Command
Data reader
Get the value

S-ar putea să vă placă și