Thông tin tài liệu
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 [...]... 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 Slide 12 Example: DBNull.Value try { while ( dr.Read() ) { label1.Text = dr[0].ToString(); label2.Text = dr.GetString(1); label3.Text = dr.GetString (2) ; if (dr["GVCN"] == DBNull.Value)... it.SubItems.Add( dr["malop"].ToString() ); } } finally { if (dr != null) } dr.Close(); } Slide 11 How to check null value 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,... Slide 16 Lesson 2: Summary Slide 17 Example 1 Create access database Table: Student(ID, FirstName, LastName, Phone) Put data in ListView Display data to TextBoxes Insert new record (using parameter query) Slide 18 Example 2 LOP(MaLop, TenLop) SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop) Slide 19 Example 3 LOP(MaLop, TenLop) SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop) Slide 20 Example 4 LOP(MaLop,...Example: Using DataReader to display on listview 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()... (dr["GVCN"] == DBNull.Value) label3.Text =""; else label3.Text = dr["GVCN"].ToString(); } } finally { if (dr != null) dr.Close(); } Slide 13 Query with “like” Slide 15 Note: Query with “like” 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 '%"... Slide 19 Example 3 LOP(MaLop, TenLop) SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop) Slide 20 Example 4 LOP(MaLop, TenLop) SINHVIEN(MaSV, Ten, DiaChi, SDT, Lop) Slide 21 Exercise Module 9 - Exercise 2 Module 10 - Exercise 4 Module 9 - Exercise 3 Module 9 - Exercise 4 Slide 22
Ngày đăng: 13/05/2014, 12:19
Xem thêm: Lesson 2 Working with data in a connected environment, Lesson 2 Working with data in a connected environment