How to use DataReader with data providers? How to retrieving data using a DataReader?. Connection DataReader Database Command Windows Windows Form Form Connected environment... A ty
Trang 1Chapter 2
Access data in a
connected environment (cont.)- Lesson 2: Working with data tiếp theo
in a connected environment
Trang 3 How to use DataReader with data providers?
How to retrieving data using a DataReader?
What is DataReader?
Connection DataReader Database Command Windows Windows Form Form
Connected environment
Trang 4 A type that is good for reading data in the most
efficient manner possible
Read-only, forward-only cursor
Load only a single row into memory at a time
Only one DataReader use at a time
Tie to connection => cannot used other DataReader
Explicitly close DataReader after used
DataReader cannot be used for data binding
Using with stored procedure with a return or output
parameter, must close DataReader before get
parameter value
Features of DataReader
Trang 5 You can’t access anything until calling Read() the first time
How DataReader works?
Trang 6 .Read()
reads a record, and set pointer to the next record
returns true if there are more rows left to be read
dataReader[int i]
dataReader[string colName]
GetValue(int i)
returns an object , a value at the i th column or at the
column colName in the current row
To access column values in native data types:
GetDateTime, GetDouble, GetInt32, GetString,
GetBoolean
Properties and methods of
DataReader
Trang 7Properties and methods of
Closes the DataReader
This allows you to use the Connection for another task
Trang 8 Using DataReader to retrieve data from Command
Trang 9private void ToLabel( DataReader dr ) {
Trang 10private void ToListBox( DataReader dr, ListBox list) {
Trang 11private void ToListView( DataReader dr, ListView lvw ) {
Trang 12 A null value in a relational database is used when the value
in a column is unknown or missing
If you try to retrieve a null value through a DataReader and
assign it to a value type, it causes an InvalidCastException
The NET framework includes the DBNull class for checking
null value, to distinguish between a null reference and a
null database value
If the column value is equal to DBNull.Value, it represents a
null database field
If you want to add a null value to a table in database, use
DBNull.Value
How to check null value
Trang 14Query with “like”
Trang 15 Example: get all students with ten is "tuan":
"select * from sinhvien where ten like '%tuan%' "
Gets all students with ten is input from TextBox
like '%" + txtTen.Text + "%' "
Note: Query with “like”
Trang 16Lesson 2: Summary
Trang 17 Create access database
Table: Student(ID, FirstName, LastName, Phone)
Put data in ListView
Display data to TextBoxes
Insert new record (using parameter query)
Example 1
Trang 18 LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Example 2
Trang 19 LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Example 3
Trang 20 LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Example 4