Chapter 2
Access data in a connected environment (cont.)- tiếp
theo
Lesson 2: Working with data in a connected
environment
DataReader object
How to get data from DataReader object to display on controls
How to check null value
Contents
Slide 2
What is DataReader?
A category of object used to sequentially read data from a data source
The return results of Command SQL statements (no constructor)
How to use DataReader with data providers?
How to retrieving data using a DataReader?
What is DataReader?
Slide 3
Connection
Connection
DataReader
DataReader
Database
Database
Windows
Form
Windows
Form
Command
Command
Connected environment
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
Slide 4
You can’t access anything until calling Read() the first time
How DataReader works?
Slide 5
.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
Slide 6
Properties and methods of DataReader (cont.)
.GetName(int i)
returns the label of the i
th
column in the current row
.FieldCount
returns number of columns
.IsClosed
returns true if DataReader is closed
.Close():
Closes the DataReader
This allows you to use the Connection for another task
Slide 7
Using DataReader to retrieve data from Command object to do something
Slide 8
Get data from DataReader object
// Setting for Connection and Command object
//
try {
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//do something with reader
}
}
finally {
if (reader != null) reader.Close();
}
private void ToLabel( DataReader dr ) {
try {
while ( dr.Read() )
{
label1.Text = dr[0].ToString();
label2.Text = dr.GetString(1);
label3.Text = dr.GetString(2);
}
}
&nally
{
if (dr != null) dr.Close();
}
}
Example: Using DataReader to display on label, textbox,
Slide 9
private void ToListBox( DataReader dr, ListBox list) {
list.Items.Clear();
try {
while(dr.Read())
{
list.Items.Add( dr[1].ToString() );
}
}
&nally {
if (dr != null) dr.Close();
}
}
Example: Using DataReader to display on listbox, combobox,
Slide 10
private void ToListView( DataReader dr, ListView lvw ) {
listView1.Items.Clear();
try {
while (dr.Read()) {
ListViewItem it = listView1.Items.Add( dr["masv"].ToString() );
it.SubItems.Add( dr["tensv"].ToString() );
if (dr.GetBoolean(2) == true)
it.SubItems.Add("Nam");
else
it.SubItems.Add("Nữ");
it.SubItems.Add( dr.GetDateTime(3).ToShortDateString() );
it.SubItems.Add( dr["malop"].ToString() );
}
}
&nally { if (dr != null) dr.Close(); }
}
Example: Using DataReader to display on listview
Slide 11
A null value in a rela5onal 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
InvalidCastExcep5on
The .NET framework includes the DBNull class for checking null value, to dis5nguish between a null
reference and a null database value
If the column value is equal to DBNull.Value, it represents a null database &eld
If you want to add a null value to a table in database, use DBNull.Value
How to check null value
Slide 12
try {
while ( dr.Read() )
{
label1.Text = dr[0].ToString();
label2.Text = dr.GetString(1);
label3.Text = dr.GetString(2);
if (dr["GVCN"] == DBNull.Value)
label3.Text ="";
else
label3.Text = dr["GVCN"].ToString();
}
}
&nally { if (dr != null) dr.Close();
}
Example: DBNull.Value
Slide 13
Query with “like”
Slide 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
cmd.CommandText = "select * from sinhvien where ten like '%" + txtTen.Text + "%' "
Note: Query with “like”
Slide 16
Lesson 2: Summary
Slide 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
Slide 18
LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Slide 19
Example 2
LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Example 3
Slide 20
LOP(MaLop, TenLop)
SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop)
Example 4
Slide 21
Module 9 - Exercise 2
Module 10 - Exercise 4
Module 9 - Exercise 3
Module 9 - Exercise 4
Exercise
Slide 22