Unit 4 Windows Programming
Unit 4 Windows Programming
(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
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
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
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 -
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.
RN Name Fees
Amol 1000
Dalaji 2000
Chetan 3000
roll number
Unit 6
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 Form1()
InitializeComponent( )
Net [email protected] A
Application.Exit( );
The SQL SELECT statement returns a result set of records from one or more tables.
Fees 2000
CHNet
[email protected]
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;
public Form1()
InitializeComponent( );
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]
B.Sc.(CS) TY
else
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
namespace updateData
public Forml()
InitializeComponent( :
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 ="
"
d.ExccuteNonQuery( );
MessagcBox.Show("Record is updated");
con.Closc( ):
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( );
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