7/30/2019 Topik 5.3 WorkwithADO.net
1/37
F5227VISUAL BASIC .NET PROGRAMMING
Topic 5.3 :
Work with Data using ADO.NET
7/30/2019 Topik 5.3 WorkwithADO.net
2/37
Course Learning Outcome (CLO) Upon completion of this course, students should be able to :
1. Create a simple VB.NETbased application based on the
Windows Application template.
2. Describe on essential terminology including memory,data types and graphical user interface.
3. Apply object oriented programming techniques to
create classes, add methods and add properties.
4. create a simple VB.NET based Web forms applicationthat uses an XML Web Service and manipulate data in
database by using Microsoft ADO.NET.
7/30/2019 Topik 5.3 WorkwithADO.net
3/37
Course Learning Outcome:
Topic 5.0
1. create a simple VB.NET based Web forms application
that uses an XML Web Service and manipulate data in
database by using Microsoft ADO.NET
7/30/2019 Topik 5.3 WorkwithADO.net
4/37
Topic 5.0
Topic 5.1 : Understand with Web Forms
Topic 5.2 Create a web application using
VB.NET
Topic 5.3 : Work with Data using ADO.NET
7/30/2019 Topik 5.3 WorkwithADO.net
5/37
Introduction
ADO.NET(ActiveX Data Objects .NET) is the
primary data access API (Application
Programming Interface) for the .NET
Framework.
ADO.NET is a data access technology from
Microsoft .Net Framework , which provides
communication between relational and non-relational systems through a common set of
components
http://vb.net-informations.com/ado.net/ado.net-architecture.htmhttp://vb.net-informations.com/framework/what_is_net_framework.htmhttp://vb.net-informations.com/framework/what_is_net_framework.htmhttp://vb.net-informations.com/ado.net/ado.net-architecture.htm7/30/2019 Topik 5.3 WorkwithADO.net
6/37
ADO.NET
ADO.NET provides the classes that you use as
you develop database applications with Visual
Basic .NET as well as other .
7/30/2019 Topik 5.3 WorkwithADO.net
7/37
ADO.NET consist of a set of Objects that
expose data access services to the .NET
environment. ADO.NET is built for
disconnected architecture , so it enables truly
disconnected Data Access and Data
Manipulation through its Dataset Object,
which is completely independent from theData Source.
7/30/2019 Topik 5.3 WorkwithADO.net
8/37
ADO.NET Architecture
7/30/2019 Topik 5.3 WorkwithADO.net
9/37
How ADO.NET works?
7/30/2019 Topik 5.3 WorkwithADO.net
10/37
ADO.NET uses two types of objects to access
the data in a database:
data providerobjects, which include data
adapters, commands, and connections.
datasets, which can contain one or more data
tables
7/30/2019 Topik 5.3 WorkwithADO.net
11/37
Data Providers
The .Net Framework includes mainly three
Data Providers for ADO.NET and they are:
Microsoft SQL Server Data Provider
OLEDB Data Provider
ODBC Data Provider.
SQL Server uses the SqlConnection object ,
OLEDB uses the OleDbConnection Object and
ODBC uses OdbcConnection Object
respectively.
7/30/2019 Topik 5.3 WorkwithADO.net
12/37
Data Providers
The ADO.NET classes that are responsible for
working directly with a database are provided
by the .NET data providers.
These data providers include the classes you
use to create data adapters, commands, and
connections.
7/30/2019 Topik 5.3 WorkwithADO.net
13/37
Data Providers
The four Objects from the.Net Framework provide the
functionality of Data
Providers in the ADO.NET.The Connection Object
provides physical
connection to the Data
Source.
The Command Object
uses to perform SQL
statement or stored
procedure to be
executed at the Data
Source
The DataReader Object is
a stream-based , forward-
only, read-only retrieval
of query results from the
Data Source, which do
not update the data.
DataAdapter
Object , which
populate a
Dataset Object
with results from
a Data Source .
7/30/2019 Topik 5.3 WorkwithADO.net
14/37
Data Adapter
To retrieve data from a database and store it
in a data table, a data adapterobject issues a
Select statement thats stored in a command
object.
Next, the command object uses a connection
object to connect to the database and retrieve
the data.
Then, the data is passed back to the data
adapter, which stores the data in the dataset.
7/30/2019 Topik 5.3 WorkwithADO.net
15/37
Command Object
To update the data in a database based on the
data in a data table, the data adapter object
issues an Insert, Update, or Delete statement
thats stored in a command object.
Then, the command object uses a connection
to connect to the database and update the
data.
7/30/2019 Topik 5.3 WorkwithADO.net
16/37
Data Provider
The data provider remains connected to the
database only long enough to retrieve or
update the specified data.
Then, it disconnects from the database and
the application works with the data via the
dataset object.
This is referred to as a disconnected data
architecture.
7/30/2019 Topik 5.3 WorkwithADO.net
17/37
Disconnected data
One of the advantages of using a disconnected
data architecture is improved system
performance due to the use of fewer system
resources for maintaining connections.
Another advantage is that it makes ADO.NET
compatible with ASP.NET web applications,
which are inherently disconnected.
7/30/2019 Topik 5.3 WorkwithADO.net
18/37
Dataset
A dataset stores data from the database so
that it can be accessed by the application.
The .NET data provider objects retrieve data
from and update data in the database.
7/30/2019 Topik 5.3 WorkwithADO.net
19/37
DataSet
DataSet provides a disconnected
representation of result sets from the Data
Source, and it is completely independent from
the Data Source.
DataSet provides much greater flexibility
when dealing with related Result Sets.
7/30/2019 Topik 5.3 WorkwithADO.net
20/37
DataSet
DataSet consists of a collection ofDataTable
objects that you can relate to each other with
DataRelation objects.
The DataTable contains a collection of
DataRow and DataCoulumn Object which
contains Data.
The DataAdapter Object provides a bridge
between the DataSet and the Data Source.
7/30/2019 Topik 5.3 WorkwithADO.net
21/37
DataSetThe DataTable contains a
collection of DataRow andDataColumn Object which
contains Data.
7/30/2019 Topik 5.3 WorkwithADO.net
22/37
Scenario
The data in a dataset is independent of the
database that the data was retrieved from.
In fact, the connection to the database is
typically closed after the data is retrieved from
the database.
Then, the connection is opened again when
its needed.
7/30/2019 Topik 5.3 WorkwithADO.net
23/37
Because of that, the application must work
with the copy of the data thats stored in the
dataset.
The architecture thats used to implement this
type of data processing is referred to as a
disconnected data architecture.
7/30/2019 Topik 5.3 WorkwithADO.net
24/37
ADO.NET Connection String
Connection String is a normal String
representation which contains Database
connection information to establish the
connection between Datbase and theApplication.
The Connection String includes parameters
such as the name of the driver, Server nameand Database name , as well as security
information such as user name and password.
7/30/2019 Topik 5.3 WorkwithADO.net
25/37
ADO.NET Connection String
Data providers use a connection string
containing a collection of parameters to
establish the connection with the database.
The .NET Framework provides mainly three
data providers: Microsoft SQL Server, OLEDB
and ODBC.
7/30/2019 Topik 5.3 WorkwithADO.net
26/37
All of the ADO.NET objects are implemented
by classes in the System.Data namespace of
the .NET Framework.
7/30/2019 Topik 5.3 WorkwithADO.net
27/37
Microsoft SQL Server Connection
String
Syntax:
connectionString ="Data Source = ServerName;
Initial Catalog = Databasename; User ID =
UserName; Password=Password"
7/30/2019 Topik 5.3 WorkwithADO.net
28/37
SQL Server Connection String
The SqlConnection Object is Handling the part
of physical communication between the
application and the SQL Server Database.
An instance of the SqlConnection class in .NET
Framework is supported the Data Provider for
SQL Server Database.
7/30/2019 Topik 5.3 WorkwithADO.net
29/37
SQL ConnectionImports System.Data.SqlClient
Public Class Form1Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim connectionString As String
Dim cnn As SqlConnection
connectionString = "Data Source=ServerName;Initial
Catalog=DatabaseName;User ID=UserName;Password=Password"
cnn = New SqlConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
7/30/2019 Topik 5.3 WorkwithADO.net
30/37
OLEDB Data Provider Connection
String
Syntax:
connectionString = "Provider =
Microsoft.Jet.OLEDB.4.0; Data Source =
yourdatabasename.mdb;"
7/30/2019 Topik 5.3 WorkwithADO.net
31/37
OLEDB ConnectionImports System.Data.OleDb
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim cnn As OleDbConnection connetionString =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"
cnn = New OleDbConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
7/30/2019 Topik 5.3 WorkwithADO.net
32/37
ODBC Connection String
Syntax:
connectionString = "Driver = {Microsoft Access
Driver (*.mdb)}; DBQ =
yourdatabasename.mdb;"
7/30/2019 Topik 5.3 WorkwithADO.net
33/37
ODBC ConnectionImports System.Data.Odbc
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVale As System.EventArgs) Handles Button1.Click
Dim connetionString As String
Dim cnn As OdbcConnection
connetionString = "Driver={Microsoft Access Driver
(*.mdb)};DBQ=yourdatabasename.mdb;"
cnn = New OdbcConnection(connetionString)
Try
cnn.Open()
MsgBox("Connection Open ! ")
cnn.Close()Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End Sub
End Class
7/30/2019 Topik 5.3 WorkwithADO.net
34/37
Two ways to create ADO.NET
objects
First, you can use the components in the Data
tab of the Toolbox to create ADO.NET objects
by dragging and dropping them onto a form.
The second technique for creating ADO.NET
objects is to write the code
7/30/2019 Topik 5.3 WorkwithADO.net
35/37
Creating ADO.NET
7/30/2019 Topik 5.3 WorkwithADO.net
36/37
.NET Providers
7/30/2019 Topik 5.3 WorkwithADO.net
37/37
The SqlConnection class