B.Sc.
(CS) TY
Unir VI- Dutabase Connectivity
Unit VI
Database Connectivity
* Introduction ADO.Net-
ADO
stands for ActiveX Data Obiects. ADO.NET is a database technology
ewoTK o or ne
used to connect application system and database server ADO.NET is a part
.NET Framework. ADO.NET consists
ofa set of classes used to handle dala
acces
The
following figure shows the ADO.NET objects at a
glance
Connection
Command
Application Data Source
DataReader
DataSet DataAdapter
Figure- ADO.Net Architecture
System.Data namespace is the core of ADO.NET and it contains classes used by all data
providers.
1) Data Providers
A key component of an ADO.NEJ is Data l'rovidcr. The Data Providcr classcs arc meant
to work with different kinds of data sources They are used to perform all
data-management
operations on specific databases.
The Net Framework includes nainly three Data PrOviders for ADO.NET.
They are the
Microsoft SQL Server Data PrOvider, OLEDB Data Provider and ODBC Data
Provider. SOL
Sérver uses the SqIConnection object, OLEDB uses the OleDbConnection
Object and ODBC
uses Odbc Connection Object respectively.
CHNet [email protected]
Latur
B.Sc:(CS) T
Connection
Source.
Data
connection to the
to it properly,
log on
2) Connection
physical and to
Connection Object provides the data
source
The recognize
i n f o r m a t i o n to
the necesary
string.
object needs a
connection
information is provided through
this
executed
to be
3) Command- statement or stored procedure
pcrform SQL can be
uses to methods that
The Command Object number of Execute
provides a
Data Source The command object
at the fashions.
to perform the SQL. queries
in a variety of ExecuteScalar (
used ADO.NET command object are
methods of
Different exccute fetches only a single object.
ExecutcNonQuery ().
ExecuteScalar )
XecueReader (). rows and loads to DataReader.
ExecuteReader ) fetches result set with multiple
for insert, update and delete.
ExecuteNonqucry () Cxecutes SQL statements
4) DataRcader-
retrieval of query
The DataReader Object is a stream-based, forward-only, read-only
Iesults from the Data Source, which do not update the data. DataReader requires a ive
connection with the database.
51 DataAdapter-
DataAdapter Ohject populates a Datasct Object with results from a Dala Source. It is a
special class whose purpose is to bridge the gap between the disconnected Dataset objccts and
the physical data source.
6) DataSct -
DataSct class provides mechanisms for managing data when it is disconnected from the
data source It is completcly independent fromthe Data Source, DataSct provides much greater
flexibility when dealing with related Result Sets.
*Advantages of ADO.Net -
ADO.Nct olfers several advantages; some of them are listed below,
1)Scalability -
ADO.NET provides
full support for disconnected data
access., In this type of data access,
most of the time the data we are
working with is not connected to a data sourc
CH.Net
[email protected]
Maintcunolbility Uge N-tie+ application Iogrc ace
rhdsls NdoeRE Coooecuoeotg, So/te
ta
maintai n seveka useA tequestS
ad cuh nma ties to me
eSSZornalerox Cocsit La
B.Sc(CS)TY EotammibilitySttongly ty ped datu &egsy to wtrle Code
because mictosot Ufsucd Stu Unit Vi- Datubuse
Connectivity
2) Data Source dio-0et feameuootk PEOuides
Independence- Stademernd opioMS
The fundamental object that holds data in AD0.NET
is object of the class Data Set.
an
This class is present in the
SOurceand
namespace.System.Data. DataSet is completely indepcndent of data
a
no way DataSct controlled by the data source as it
is
happens in case of RecordSet.
3)Interoperability
As ADO.NET transmits the data using the format of XML which is not
ADO.NET or windows platform. dependent on
4) Performance
Less processing time is needed for data conversion in the
case of ADO.NET, as data
lransmission occurs via XML. Tlhis iesults in better per formance of the busincss application that
is developed with the help of ADO.NET.
5) Firewall-
AS in ADO.NET transmission is via XML
format, there fore il can pass th1rough fiiewalls.
6) XML
ADO.NET programs take
can
advantagc of the flexibility and broad acccptance of
Extensible Markup I.anguage (XMI.). XML is the
format for transmitting datasets across the
network, so any component that can read the XML format
can process data.
Developing a Simple ADO.NET Based Application -
Consider college.mdb database is created in MS-Access with Student
table,
Ficld Data Type
RN Auto Number
Name lext
F'ees Number
RN Name Fees
Amol 1000
Dalaji 2000
Chetan 3000
Table -Student (Design and Data Sheet
CH.Net View)
[email protected]Professtoral xerox Cocs
Unit VI-DatabaseConnectivity
B.Sc.(CS) 7 table depending
upon
Student
record from
will remove
an application that
rogram-Develop
roll number
Unit 6
Simple ADO.NET Application
RN
Delete
Property Table-
Control Property
Name Text
Labell RN
textBox txt_rn
button btn_delete Delete
button2 btn exit Exit
using System.Data.OleDb;
namespace simpleADO
public partial class Forml: Form
public Form1()
InitializeComponent( )
private void btn_delete_Click(object sender, EventArgs e)
OleDbConnection con = new OleDbConnection ("Provider = Microsoft.Jet.oledb.4.0;
Net [email protected] A
Professtoal xerox Cocsit Latu
B.Sc.(CS) TY
Unit VI- Datubase Connectivity
Data Source d:llcollege.mdb")
con.Open)0
string query = "delete from student where m= " txt m.Text;
OleDbCommand cmd new OleDbCommand(query, con),
cmd.Execute NonQuery( ):
MessageBox.Show( "Record is deleted");
con.Close)
private void btn_exit_Click(object sender, EventArgs e)
Application.Exit( );
Retrieving & Updating Data from Tables
a) Retrieving Data from Table
The SQL SELECT statement returns a result set of records from one or more tables.
retrieving data lrom database.
Program - Write a program for demonstration of accessing or
Simple ADO.NET Application
RN
2
Name- Balaj Search
Fees 2000
CHNet
[email protected]
Professional xerox Cocsit atu
Unit VI- Database
B.Sc.(CS) TY
Connectivity
Property Table -
Control Property
Name Text
Labell RN
Labell Name
Labell Fees
textBox1 txt_rn
textBox2 tXt_name
textBox3 Ixtfees
buttonl btnsearch Search
using System.Data.OleDb;
namespace database Demo
public partial class Forml: Form
public Form1()
InitializeComponent( );
private void btn _search_Click(object sender, EventArgs e)
OleDbConnection con =
new OleDbConnection ("Provider
Microsoft.Jet.oledb.4.0;
=
Data Source =
d:|lcollege.mdb"):
con.Open()
string query= "select *
from student where rn =" +
txt_rn.Text;
OleDhCommand cmd= new OieDbCommand(query, con);
OleDbDataReader dr cmd.ExecuteReader( );
=
if(dr.Read(O)
txt_name.Text = dr["name"].ToString();
txt_fecs.Text=dr["fees"].ToString();
CH.Net
[email protected]
rotessu07al xer0XCocsb Latur
Unit VI- Dutubuse Connectivity
B.Sc.(CS) TY
else
MessageBox.Show("Record not found");
txt_clear( )
con.Close( ):
b) Table Updating-
An SQL UPDATE statement changes the data of one or more records in a table.
Program Write a program for demonstration of modifying or updating data from database.
Updating Data
RN
Name
Fees
*****
Update Ext
Property Table-
Control Property
Name Text
Labell RN
Label2 Name
Fees
Label3
[email protected]
CH.Net
Prot ondl xerox Cocsit Latur
Unit VI-Database Connectivit
B.Sc(CS) T textBox INt rn
t N t name
textBox2
textBox3 txt fees
buttonl btn_update Update
Exit
button2 btn_exit
using System. Data.OleDb,
namespace updateData
public partial class Forml: Form
public Forml()
InitializeComponent( :
privale void btn_update_Click(object sender, EventArgs e)
OlcDbConnection con =
new
OleDbConnection("Provider Microsoft.Jet.oledb.4.0;
=
Data Souree =
d:\\college.mdb")
con.Open()
string query= "update student set name =" "+ txt name.Text "
+ '
fees= + Ixt fecs.Text +"' where rn ="
"
+ txt rn.Text +" "
OlcDbCommand cmd new
OleDbCommand(query, con);
=
d.ExccuteNonQuery( );
MessagcBox.Show("Record is updated");
con.Closc( ):
private void btn_cxit
Click(object sender, EventArgs e)
Application.Exit()
C#.Net
[email protected]
Unit VI-Databuse Connectivity
B.Sc.(CS) TY
Disconnected Data Access through Dataset Objects-
The ADO.NET Framework supports two models of Data Access Architeclure, connected
and disconnected modes.
A connected mode of in ADO.Net is in
unde
operation one
which the connection to the
yng database is alive throughout the lifetime of the
operation. Meanwhile, a disconnected
s operation is one in which ADO.Net retrieves data from the underlying database, stores
the data retrieved
etrieved temporarily in the memory, and then closes the connection to the database.
0.Net provides a new solution by introduce a new component called Dataset. The
h e central component in the ADO.NET Disconnected Data Access Architecture. It
Old multiple tables at the same time. DataSets only hold data and do not interact with a
Data Source.
onnection Oriented Data Acces, when you read data lrom a
Datakeader
dalabase by using a
object, an open
connection must be maintained between your application and
Data Source.
Unlike the DataReader, the DataSet is not connected the
through a Connection object when dircctly to a Data Source
t
you populate it.
is the
c
DataAdapter that manages connections between Data Source and Dataset by fill
data irom Data Source to the Dataset and giving a disconnected
DataAdapter acts as a bridge behavior to the Dataset. The
between thhe DataSet and Data Source.
Program Write a
program for demonstration of disconnected data
dataset object.
access through
Databasejconnecuyity
Disconnected Mode
RN Name Fees
Ano 1000
Balaj 2000
**
Chetan 3000
C#.Net [email protected] 9
Unit VI- Database Connectivity
B.Sc.(CS) 7Y
using System.Data.OleDb:
namespace disconnectedAccess
1Forml: Form
public partial class
public F'ormI()
InitializeComponent( );
private void l'orml_Ioad(object sender, EventArgs e)
OlelDbConnection con = new OleDbConnection("Provider = Microsoft.Jet.oledb.4.0;
Data Source =
d:\\eollege.ndb");
con.Open( ):
stringquery= "select * from student";
OleDbDataAdapter da =
new
OleDbDataAdapter(query, con);
DataSct ds =
new DataSet( );
da.Fill(ds. "student");
dataGrid Vicw1.DataSource ds. T'ables|"student"];
con.Close( ):
CH.Net
[email protected] 10