1
VB.Net 2005 - Chapter 6 1
Chapter 6
Working with Data in a
Connected Environment
VB.Net 2005 - Chapter 6
2
Chapter 6
Lesson 0: Connected & Disconnectd Environment
in ADO.NET
Lesson 1: Creating and Executing Command
Objects
Lesson 2: Working with Parameters in SQL
Commands
Note: Lesson 3: BLOBs and Lesson 4: Bulk Copy: For reading at
home
Lesson 5: Performing Transactions by Using the Transaction Object
2
VB.Net 2005 - Chapter 6
3
Lesson 0: Connected & Disconnectd
Environment in ADO.NET
2 ways using with ADO.NET
Connection Data Adapter
Data
Source
DataSet
Web
Form
Windows
Form
Connection Command
Data
Source
DataReader
Web
Form
Windows
Form
Disconnectd
Connected
VB.Net 2005 - Chapter 6
4
Disconnected Data Access
Single database server
can support many users
reduced server's
resources
Data using more flexible
Data not 'tied' to a
connection
easy to pass
between tiers or
persist to file
Highly suited to Web and
n-tier Apps
Expensive when
open, close
connections
Retrieving large
result sets can be
very slow
Places demand on
client memory and
CPU
Advantages
Disadvantages
3
VB.Net 2005 - Chapter 6
5
System.Data Architecture
DataReader
Database
Program
DataProvider
DataSet
DataAdapter
VB.Net 2005 - Chapter 6
6
Details:
System.Data Architecture
Command Object
Connection Object
Program
Provider
DataSet
DataReader
DataAdapter
Database
Action SQL
Maintain
data
Maintain
data
Get data
Get data
4
VB.Net 2005 - Chapter 6
7
Main Difference:DataSet-DataReader
DataSet
Data structure to store schema and data in a disconnected
fashion
Useful for editing data offline and later update to data
source
DataReader
Like Phone connection.
Doesn’t need to store data in memory
Object to access data in a connected, forward-only, read-
only fashion
When performance is your chief concern, especially with
large amounts of data, use a DataReader class
VB.Net 2005 - Chapter 6
8
Differences DataSet and DataReader
5
VB.Net 2005 - Chapter 6
9
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
2.Creating and Configuring Command
Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
VB.Net 2005 - Chapter 6
10
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
6
VB.Net 2005 - Chapter 6
11
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
To execute SQL statements,stored procedures
Contain the necessary information to execute
SQL statements
VB.Net 2005 - Chapter 6
12
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
Depend on Data Providers
7
VB.Net 2005 - Chapter 6
13
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
Common properties (p.254)
CommandText
SQL string, table name, proc name
CommandType
Text
Proc
Table
Connection
Parameters
VB.Net 2005 - Chapter 6
14
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
Common Command Object Methods (p.255)
ExecuteNonQuery
ExecuteReader
ExecuteScalar
8
VB.Net 2005 - Chapter 6
15
Lesson 1: Creating and Executing
Command Objects
2.Creating and Configuring Command
Objects
Creating a Command Object That Executes a
SQL Statement
Creating a Command Object That Executes a
Stored Procedure
Creating a Command Object That Performs
Catalog Operations
Creating a Command Object That Returns a
Single Value
VB.Net 2005 - Chapter 6
16
Lesson 1: Creating and Executing
Command Objects
2.Creating and Configuring Command
Objects
Creating a Command Object That Executes a
SQL Statement (p.256)
9
VB.Net 2005 - Chapter 6
17
Lesson 1: Creating and Executing
Command Objects
2.Creating and Configuring Command
Objects
Creating a Command Object That Executes a
Stored Procedure (p.257)
VB.Net 2005 - Chapter 6
18
Lesson 1: Creating and Executing
Command Objects
2.Creating and Configuring Command
Objects
Creating a Command Object That Performs
Catalog Operations (p.257)
10
VB.Net 2005 - Chapter 6
19
Lesson 1: Creating and Executing
Command Objects
2.Creating and Configuring Command
Objects
Creating a Command Object That Returns a
Single Value (p.258)
VB.Net 2005 - Chapter 6
20
Lesson 1: Creating and Executing
Command Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
Creating SQL Commands (SQL Statements)
with the Query Designer
Performing Database Operations Using
Command Objects
11
VB.Net 2005 - Chapter 6
21
Lesson 1: Creating and Executing
Command Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
Creating SQL Commands (SQL Statements)
with the Query Designer
We can use the Query Designer to assist in creating
SQL for Command objects
Select database in Server Explorer-> select New
Query from the Data menu.
VB.Net 2005 - Chapter 6
22
Lesson 1: Creating and Executing
Command Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
Performing Database Operations Using
Command Objects (p 260)
12
VB.Net 2005 - Chapter 6
23
How to receive DataReader
Connection
ConnectionConnection
Connectionn
nn
n
Command
CommandCommand
Commandn
nn
n
DataReader
DataReaderDataReader
DataReadern
nn
n
Open
Which data?
Result
VB.Net 2005 - Chapter 6
24
DataReader Class
Store the information obtained by the command
In stateless stream type object
Very efficient
Forward-only cursor
Read-only cursor
Dim dr as OLEDBDataReader = cmd.ExecuteReader
Do while dr.Read()
msgbox dr("City").toString
Loop
dr.Close
Datareader and MS Access
13
VB.Net 2005 - Chapter 6
25
DataReaders
You can’t access anything until you call
Read()the first time
VB.Net 2005 - Chapter 6
26
Core DataReader method/property
Read: Reads, and set pointer to the next record.
Close
IsClosed
HasRows:Returns true if DataReader contains rows
FiledCount: Number of columns
GetName(i): returns the label of the ith column in the
current row
GetString(i) :
returns the value of the i
th
column as the
specified type
14
VB.Net 2005 - Chapter 6
27
DataReader method/property
conSinhVien.Open()
cmdSinhVien = New OleDbCommand("Select ", conSinhVien)
drSinhVien = cmdSinhVien.ExecuteReader
Do While drSinhVien.Read
MessageBox.Show(drSinhVien.GetString(0))
Loop
drSinhVien.Close()
conSinhVien.Close()
VB.Net 2005 - Chapter 6
28
Note: DataReader
ADO.NET does not provide all the server-side cursor
Don’t keep DataReaders open longer than necessary
For flexible updates & client-side manipulation…
Use DataSets and DataAdapters
Only one DataReader use at a time
Tie to Connection=> cannot used other DataReader.
To reuse connection=>call DataReader.Close.
Don’t depend on the garbage collector-> explicitly
close.
Tie the connection ‘ life to DataReader
CommandBehavior.CloseConnection in ExecuteReader.
15
VB.Net 2005 - Chapter 6
29
Note about DataReader
The first row of data is not available until you
call the Read method.
Using with stored procedure uses a return or
output parameter, must close DataReader
before get parameter
DataReader cannot be used for data binding
System.DBNull.value
VB.Net 2005 - Chapter 6
30
Note about DataReader
Use Item collection, to get values:
Using index instead of by key (column name).
Using native type instead of using the Item collection followed by
an explicit cast.
sdr.GetDecimal(1) instead of CDbl(sdr.Item("UnitPrice")).
Concat field’ values: A large of operation
Using StringBuilder instead string concatenation.
While sdr.Read
strProductName = sdr.GetString(0)
sb.Append(strProductName)
sb.Append(sdr.GetDecimal(1).ToString())
sb.Append(vbTab)
sb.Append(sdr.GetInt16(2).ToString())
End While
16
VB.Net 2005 - Chapter 6
31
Lesson 1: Creating and Executing
Command Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
Lab: P265
VB.Net 2005 - Chapter 6
32
Lesson 2: Working with Parameters in
SQL Commands
1.What Is a Parameter and Why Should I
Use Them?
2.Types of Parameters
3.Creating Parameters
4.Adding Parameters to Command
Objects
17
VB.Net 2005 - Chapter 6
33
Lesson 2: Working with Parameters in
SQL Commands
1.What Is a Parameter and Why Should I Use
Them?
parameter can be thought of as a type of variable
use to pass and return values between your
application and a database.
Parameter data types are assigned using the types
defined in the System.Data.SqlDbType enumeration.
pass parameter values to SQL statements when we
want to change the criteria of your queries quickly.
VB.Net 2005 - Chapter 6
34
Lesson 2: Working with Parameters in
SQL Commands
2.Types of Parameters (p 274)
Input parameter. (default)
Output parameter.
InputOutput parameter.
InputOutput parameters are used to both send and
receive data when executing a command.
The type of parameter is designated in the
Direction property of the parameter
With a parameter, we ou can set its Direction
property to Input, Output, InputOutput, or
ReturnValue.
18
VB.Net 2005 - Chapter 6
35
Lesson 2: Working with Parameters in
SQL Commands
3.Creating Parameters (p 274)
Create parameters:instance of the Parameter
class
setting its name and data type
Choose ParameterDirection
VB.Net 2005 - Chapter 6
36
Lesson 2: Working with Parameters in
SQL Commands
4.Adding Parameters to Command
Objects
Command objects have a Parameters property
that represents a collection of parameters
After you create a parameter, you must add it
to the Parameters collection of the Command
object
19
VB.Net 2005 - Chapter 6
37
Lesson 2: Working with Parameters in
SQL Commands
Lab: Lab: Working with Parameters page
275
VB.Net 2005 - Chapter 6
38
Lesson 2: Summary
Update Database with connect mode
Client
Type 1: Using client dynamic Sql
Type 2: Using client SQL with parameters
Server
Using server Query Parameters
Using server Stored Procedured
Example: find student
20
VB.Net 2005 - Chapter 6
39
Your Questions?
VB.Net 2005 - Chapter 6
40
21
VB.Net 2005 - Chapter 6
41
cmd.CommandText = "select *
from sinhvien where ten like
‘%tuan%' "
cmd.CommandText = "select *
from sinhvien where ten like ‘%” &
txt.text & “%' "
VB.Net 2005 - Chapter 6
42
cmd.CommandText = "select *
from sinhvien where ten like
‘%tuan%'"
cmd.CommandText = "select *
from sinhvien where ten like ‘ %
tuan %' "
22
VB.Net 2005 - Chapter 6
43
Notes
Datareader: New?????
Open Connection before ExecuteReader
Execute only one for one command
VB.Net 2005 - Chapter 6
44
Exer 1
1. Create MA access database
Table Student(ID,FirstName,Lastname,Phone)
Using Client dynamic SQL to put data in Listview
Using Client dynamic SQL to detail data to
Textboxes
Exit
23
VB.Net 2005 - Chapter 6
45
Ex. Insert new record
Exit
Save
VB.Net 2005 - Chapter 6
46
Access
Insert into sv(ma,lname,fname,phone) Values
(?,?,?,?)
Cmd.parameters.add(p1)
Cmd.parameters.add(p2)
Cmd.parameters.add(p3)
Cmd.parameters.add(p4)
Insert into sv(ma,lname,fname,phone) Values
(@ma,@lname,@fname,@phone)
insert into sv(ma,[last name],[first
name],phone) values (?,?,?,?)
24
VB.Net 2005 - Chapter 6
47
Exer 2
Copy Exer 1 to new folder
Change the way to get data
Client Dynamic SQL -> Client Parameters SQL
VB.Net 2005 - Chapter 6
48
Exer 3
Copy Exer 2 to new folder
Change the way to get data
Client Parameters SQL ->server Parameters SQL
in querry
25
VB.Net 2005 - Chapter 6
49
Exer 4
Using Student data file
Display in treeview
Display detail
0904981234
VB.Net 2005 - Chapter 6
50
BT1
LOP(MaLop,TenLop)
SINHVIEN(MaSV,Ten,DiaChi,SDT,Lop)