You are on page 1of 114

Chapter 6

ADO.NEt
Interact with middle or database
using ADO.NET

2
Introduction
• Here to understand the role of ADO.NET first we look
into same basic terms like front end application ,front
end tools and back end application, back end tools
• Front end application : the application with which
end user will interact like screen ,form, reports is
known as front end application
• Back end application: the application where we store
the end user data is known as back end application

Compiled By Abrham Y. 3
Cont..
• Front end tool: the software/tool used to
design the front end application
• Ex C#.net , java , Dbase, MS Access, oracle
• Back end application: the software/tool used to
design the back end application
– Ex Dbase, MS Access, oracle sqlsever, Mysql,
Sybase, FoxPro

Compiled By Abrham Y. 4
Layered Approach

• Now we look into approach called layered


approach
1. I Layered Approach
2. II Layered Approach
3. III Layered Approach

Compiled By Abrham Y. 5
I Layered Approach

• In this method both front end and back end


application are designed using the same
tools and can be present only in single
machine

Compiled By Abrham Y. 6
Cont..

Dbase, MS

Access,

oracle

Compiled By Abrham Y. 7
Cont…
• Advantage I Layered Approach
– As front end and back end application are designed using
the same tool no separate translator mediums are required
• Disadvantage I Layered Approach
– Doesn't support centralized database
– Cannot withstand for a large amount of data
• To overcome the disadvantage of I layered we use II layered

Compiled By Abrham Y. 8
II Layered Approach

• In this method both front end and back end


application are designed using two different
tools and can be present in two different
machines

Compiled By Abrham Y. 9
Cont..

Compiled By Abrham Y. 10
Role of Native Drives
• As front end and back end applications are designed
using 2 different tools. So front end tools can not
understand back end tools and back end tools can not
understand front.
• Native drives are used as translators between front end
and back end
• Front end tool naïve driver will take the require form from front
end tool converts into back end tool naïve driver understandable
format and send to front end
• Back end tool naïve driver will take the require form back end
tool and converts into front end tool naïve driver understandable
format and send to front end 11
Compiled By Abrham Y.
Cont..
• Advantage II Layered Approach
– Support centralized database
– Most of the database can withstand for large amount
of data
• Disadvantage II Layered Approach
– In the front end tool if one native driver is designed to interact
with any back end tool and that native driver cannot be used
interact with other backend tool that means every front end
tool should have separate set of native driver to interact with
back end tools l

Compiled By Abrham Y. 12
Cont…
• if one native driver is designed for any back
end tool to interact with some front end tool
that native driver cannot be used to interact
with other front end tools. So every backend
tool need to have one separate set of native
drivers in to interact with each front end tool
like
• To over came this disadvantage owe use III
layered approach

Compiled By Abrham Y. 13
Cont..

Compiled By Abrham Y. 14
Cont..

Compiled By Abrham Y. 15
III Layered Approach

• In this method front end and back end tool


will have only one set of native drivers
• These native drivers do not interact with
each other , rather interact with 3rd
component known as “Middleware”
• Middleware is designed in such a way that it
can understand any front end native drivers
and any back end native driver

Compiled By Abrham Y. 16
Cont..

Compiled By Abrham Y. 17
ADO.NET(ActiveX Data Object for .Net)

18
ADO.Net
• ADO.Net is the native drivers designed by
Microsoft for .Net technology used to
interact with middle wave or database
• With out ADO.Net we can not write database
programming code in .Net
• ADO.Net support connection oriented
architecture and also disconnection oriented
architecture

Compiled By Abrham Y. 19
Cont..
• ADO.Net include .NET framework data
providers for connecting to a
database ,executing commands and
retrieving result

Compiled By Abrham Y. 20
Connection oriented architecture
• In this architecture we have 2 methods supported
• 1st method : in this method first we establish
connection to the database , performs the required
operations like inserting the records, updating ,
deleting the records etc and then close connection

Compiled By Abrham Y. 21
Cont..

Compiled By Abrham Y. 22
Cont..
• 2nd methods: this method is used just to
read the data from the data the database and
using this method we cannot perform the
operation like inserting the records, deleting
the records etc

Compiled By Abrham Y. 23
Cont..

Compiled By Abrham Y. 24
Cont..
• Step1 : makes a connection from client to
database
• Step2: client sends request from required
data to the server
• Step3: client request is processed and
requested data is dumped into separate
memory at server , allocated for the client
this memory is known as network buffer or
result set
Compiled By Abrham Y. 25
Cont..
• Step4: link is created from result set to
client
• Step5: client start reading the data from
result set
• Step 6: after the completion of reading the
data connection to the database can be
closed

Compiled By Abrham Y. 26
Disconnection oriented architecture
• In this method it is not required to establish
prior connection to the database. When
request sent for data connection to the
database is established automatically
• Once requested data is dumped into client
machine memory , no more connection is
maintained to the database

Compiled By Abrham Y. 27
Cont..

Compiled By Abrham Y. 28
Cont..
• Step1:client sends request for required data
from the server , at the time of request only
connection to database is automatically opened
• Step2: requested data is dumped into client
machines memory as process area(PA). Once
data is dumped into process area, no more
connection is maintained to the database and
the connection is closed automatically

Compiled By Abrham Y. 29
Cont…
• Step3: user starts interacting with process
area data
• Step4: after completion of the user
interaction , process area data is updating to
the database while updating the data again
connection is automatically opened and
closed after updating

Compiled By Abrham Y. 30
ADO.Net Architecture

Compiled By Abrham Y. 31
Working with connection
object

32
Introduction

• Connection object is used to establish


connection to the database

Compiled By Abrham Y. 33
Property and methods with
connection object
Property and options Description
ConnectionString Get or sets the connection string value
used to open connection to the database

ConnectionTimeout
DtataSource Get the name of the instance of the
database server to which to connect

PacketSize
State
ServerVersion

Compiled By Abrham Y. 34
Methods with Connection class
Property and options Description
BeingTransaction()
Close() Used to close an opened connection to
the database
CreatCommand()
GetSchema()
Open() Open a database connection with the
property settings specified by the
ConnectionString

Compiled By Abrham Y. 35
Steps to work with connection
object
• Step1: declare and define a connection object
– Ex. OleDbConnection con = new OleDbConnection();
• Sep2 : create connection with database using
connection string
– Connection strings to connect to the various database
using different data provider
– Ex.
con.ConnectionString = @"Provider=MicroSoft.ACE.OLEDB.12.0;Data
Source=C:\database\employ.accdb;Persist Security Info=False;";

Compiled By Abrham Y. 36
Cont..
• Step3 : open connection
– Ex con.open()
– Access data from database
• Sep4: close connection
– Con.close()

Compiled By Abrham Y. 37
Example

• Step1:Design and create the user interface

Compiled By Abrham Y. 38
Con…

Compiled By Abrham Y. 39
Cont…
Step2: write code
1.Write a program to connect to MSAccess
using OledbProvider

Compiled By Abrham Y. 40
using System.Data.OleDb;
namespace database
{
public partial class Form1 : Form
{

OleDbConnection con = new OleDbConnection();


 
public Form1(){ InitializeComponent();}
 
private void Connect_Click(object sender, EventArgs e)
{
con.ConnectionString = @"Provider=MicroSoft.ACE.OLEDB.12.0;Data
Source=C:\database\employ.accdb;Persist Security Info=False;";
con.Open();
MessageBox.Show("Connection Successful");

con.Close();
Compiled By Abrham Y. 41
}}}
Working with Command
object

42
Introduction
• Command object is used to perform the required
operations on the database object (tables, Stored
procedure, view etc) like inserting,
deleting ,updating record, creating , altering,
dropping tables, stored procedures, view etc after
establishing the connection
• Command object will work with connection
oriented architecture

Compiled By Abrham Y. 43
Property and methods with
command object
Property and options Description
CommandText Used to set or get the required
command text value , CommandText
can be any one form SQL Query or
Stored Procedure Name or Table Name

CommandTimeOut
Connection Used to set or get the connection object
name to be used with the command
object

Parameters
Tansaction

Compiled By Abrham Y. 44
Execution methods with
command class
Method Description
ExecutedNonQuery() Will work with action queries
only( create, alter, drop, insert,
update, delete)

ExecuteReader() Will work with action and non-action


queries (select)

ExecuteScalar() Will work with action queries that


contain aggregate function

Compiled By Abrham Y. 45
Steps to work with command
object
• Step1: declare and define a connection object
• Step2: Mention the connation
• Step3:Mention the command type
• Step4: Execute the command object

Compiled By Abrham Y. 46
Example
• Step1:Design and create the user interface

Compiled By Abrham Y. 47
Cont…
Step2: write code
1.Write a program to insert , update and
delete records to MSAccess

Compiled By Abrham Y. 48
Working with DataReader
object

49
Working with DataReader object

Introduction
• DataReader will work with connection
oriented architecture
• DataReader is used only object ie, using
DataReader we can just read the datafrom
the database, and we can not perform any
operation like inserting, updating and delete
records etc
Compiled By Abrham Y. 50
Cont..
• DataReader is sequential only object , i.e can
read the records one by one only
• There is no mechanism to move N th record
directly
• DataReader is forward only object i.e it is
possible to read current record to next record
only and is not possible to read the previous
record from current record
Compiled By Abrham Y. 51
How DataReader will Work(architecture of
DataReade)

Compiled By Abrham Y. 52
Cont..
• Step1:establish connection to the server
• Step2: Client send request to the server
using command object for the required data
• Step3: client request is processed at the
server and the query execution result is
transfer to the separate memory allotted to
the client at server known as result set

Compiled By Abrham Y. 53
Cont..
• Step4: link is created from the header row of
result set to datareader
• Step5: DataReader starts the data using the Read()
method
• Step6: the data read by using the DataReader can
be displayed in the user interface elements or used
for calculation or for any other purpose required
After completion of the total reading of the data either
DataReader closed or connection closed

Compiled By Abrham Y. 54
When result set is destroyed

Result set is destroyed in following situation


• When DataReade closed
• Whew connection closed

Compiled By Abrham Y. 55
How to identify the data using DataReader
• In the above figure if DataReader is reading
the first record then we identify the field
data using the index value or field name like
DR[0] or DR[“EmpID”]
DR[1] or DR[“EName”]
DR[2] or DR[“Designation”]
DR[3] or DR[“DOJ”]
DR[4] or DR[“Salary”]
DR[5] or DR[“Deptno”]
56
Compiled By Abrham Y.
Property of Datareader object

Property and options Description


FieldCont Return the count of fields present in the
result set

HasRow
IsClosed
RecordsAffected

Compiled By Abrham Y. 57
Methods with DataReader class

Method Description
Close() Close the sqlDataReade obhect

GetName(int i)
NextResult()
Read() Advances the DataReader to
the next record. Return true if
there is another record
otherwise return false if EOF
is encountered

Compiled By Abrham Y. 58
Example1
• Step1:Design and create the user interface

Compiled By Abrham Y. 59
Cont…
Step2: write code
1.The application should respond to this click
event next record

Compiled By Abrham Y. 60
Example2
• Step1:Design and create the user interface

Compiled By Abrham Y. 61
Cont…
Step2: write code
1.The application should respond to this
listEmpId selectedIndexChange event

Compiled By Abrham Y. 62
Example3
• Step1:Design and create the user interface

Compiled By Abrham Y. 63
Cont…
Step2: write code
1.The application should respond to this click
event Find record

Compiled By Abrham Y. 64
Working with Dataset
object

65
What is Dataset

• A Dataset is an in-memory object or we can


say that memory representation of the data
• Dataset will work with disconnected
architecture
• Dataset store the data temporarily with in
the application so that data can be used with
in the application

Compiled By Abrham Y. 66
External Architecture of Dataset
• Dataset does not know how to interact with
database so dataset will use DataAdapter object
to interact with database
• DataAdapter will perform following tasks
– Gets the data from database and fillsv into dataset
– Update the dataset data back to database
• DataAdapter will use command object internally
to interact with database

Compiled By Abrham Y. 67
Cont..

Compiled By Abrham Y. 68
Cont…
• DataAdapter will use select command to fetch the
data from the database and to fill into dataset , at
the time of request connection to the database is
automatically established and once data is filled
into the dataset connection is automatically closed
• User interact with dataset data, after completion of
the user interaction dataset data can be updated to
the database using insert, update, and delete
command of the DataAdapter

Compiled By Abrham Y. 69
Internal Architecture of Dataset
• Internally Dataset will store the data in the form of
collection
• Dataset will maintain two collection internally
– Table collection
– Relations collection
• Every table will have three collections again
– Rows collection
– Columns collection
– Constrains collection

Compiled By Abrham Y. 70
Cont..
• Single dataset contain any number of tables
with in the tables collection and any number
of relations with in relation collection
• There is no limits for number of tables and
relations with in the single dataset as long
as memory is available we can store the
number of tables and relations

Compiled By Abrham Y. 71
Cont..

Compiled By Abrham Y. 72
DataSet(DS)

Compiled By Abrham Y. 73
Identification of Table in DataSet

Compiled By Abrham Y. 74
As tables are store in the form of collection each tables
cab be identified using index value or table name like

Compiled By Abrham Y. 75
Identification of column in Dataset
Table
•As every table will contain collection of row each row
can be identified using index value like

Compiled By Abrham Y. 76
Identification of column in Dataset
Table
•As every table will contain collection of columns each column can be identified using
index value like

Compiled By Abrham Y. 77
Identification of cells or cell data
Dataset Table
•A cell or a cell data with in the data set table is identified using matrix notation like

Compiled By Abrham Y. 78
Steps to work with Dataset object

• Step1: Declare and Define DataAdapter object


• Step2 : Declare and Define Dataset
• Step3: Fill the data into Dataset using
Fill()methods of DataAdapter

Compiled By Abrham Y. 79
Properties with Dataset class

Compiled By Abrham Y. 80
Methods with Dataset class

Compiled By Abrham Y. 81
Property with DataAdapter class

Compiled By Abrham Y. 82
Method with DataAdapter class

Compiled By Abrham Y. 83
Example3
• Step1:Design and create the user interface

Compiled By Abrham Y. 84
Cont…
Step2: write code
1.The application should respond to this click
event First, next, previous and last

Compiled By Abrham Y. 85
DataGridView control

86
DataGridView controle
• The DataGridView control provides and
flexible way to display data in a tabular format
• you can use DataGridView control to show
read-only views of a small amount of data, or
you can scale it to show editable views of very
large sets of data
• For example , you can programmatically specify
your own sorting algorithm and you can create
your own types of cells
Compiled By Abrham Y. 87
Adding DataGridView controls
to a window form

Compiled By Abrham Y. 88
Example1
• Step1:Design and create the user interface

Compiled By Abrham Y. 89
Cont…
Step2: write code
1.The application should respond to this click
event show records using DataGridView

Compiled By Abrham Y. 90
Project1

Compiled By Abrham Y. 91
Compiled By Abrham Y. 92
Compiled By Abrham Y. 93
Compiled By Abrham Y. 94
Compiled By Abrham Y. 95
Compiled By Abrham Y. 96
Compiled By Abrham Y. 97
Project2

Compiled By Abrham Y. 98
Compiled By Abrham Y. 99
Compiled By Abrham Y. 100
Compiled By Abrham Y. 101
Compiled By Abrham Y. 102
Compiled By Abrham Y. 103
Compiled By Abrham Y. 104
Project3

Compiled By Abrham Y. 105


Compiled By Abrham Y. 106
Compiled By Abrham Y. 107
Compiled By Abrham Y. 108
Compiled By Abrham Y. 109
Compiled By Abrham Y. 110
Project4

Compiled By Abrham Y. 111


Reading Assignment
Report

112
What is report
• A report is an output tools used to display
the required data or summary information
from the data base to the user in the user
requirement form
• In general in real time reports are very much
vital for any organization , complete top level
management will depend only reports to take
appropriate decision related to business needs

Compiled By Abrham Y. 113


Elements of Report
• In general any report will contain following
element
– Report Header
– Page Header
– Group Header
– Details Section
– Group Footer
– Page Footer
– Report Footer
Compiled By Abrham Y. 114

You might also like