VB.Net 2005 - Chapter 6 3 2 ways using with ADO.NET Connection Data Adapter Data Web Form Windows Form Connection Command Data Web Form Windows Form Disconnectd Connected VB.Net 2005 -
Trang 2VB.Net 2005 - Chapter 6 3
2 ways using with ADO.NET
Connection Data Adapter
Data
Web Form
Windows Form
Connection Command Data
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
Retrieving large result sets can be very slow
Places demand on client memory and CPU
Trang 4Like 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
Trang 5VB.Net 2005 - Chapter 6 9
1.What Are Command Objects?
2.Creating and Configuring Command
Objects
3.Creating SQL Commands (SQL
Statements) with the Query Designer
Lesson 1: Creating and Executing
Command Objects
1.What Are Command Objects?
Trang 6VB.Net 2005 - Chapter 6 11
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
Trang 71.What Are Command Objects?
Common Command Object Methods (p.255)
ExecuteNonQuery
ExecuteReader
ExecuteScalar
Trang 10Statements) with the Query Designer
Creating SQL Commands (SQL Statements)
with the Query Designer
Performing Database Operations Using
Command Objects
Trang 11VB.Net 2005 - Chapter 6 21
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
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)
Trang 12Store the information obtained by the command
In stateless stream type object
Trang 13VB.Net 2005 - Chapter 6 25
You can’t access anything until you call
Read()the first time
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
Trang 14ADO.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
Trang 15VB.Net 2005 - Chapter 6 29
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
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.
Trang 17VB.Net 2005 - Chapter 6 33
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
Lesson 2: Working with Parameters in
InputOutput parameters are used to both send and
receive data when executing a command
Trang 18Command 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
Trang 19Type 1: Using client dynamic Sql
Type 2: Using client SQL with parameters
Server
Trang 20VB.Net 2005 - Chapter 6 39
VB.Net 2005 - Chapter 6 40
Trang 22VB.Net 2005 - Chapter 6 43
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
Trang 23VB.Net 2005 - Chapter 6 45
Exit Save
Trang 24VB.Net 2005 - Chapter 6 47
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