1. Trang chủ
  2. » Giáo án - Bài giảng

Lesson 1 DataSet

29 229 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 29
Dung lượng 264,12 KB

Nội dung

Chapter 3 Binding, linking, update data in a disconnected environment Lesson 1: DataSet  What is DataSet?  Differences between DataSet and DataReader  DataSet structure  Creating DataSet objects  Display data to controls from DataSet Contents Slide 2  DataSet object  is a major component of the ADO.NET architecture  is an in-memory cache of data retrieved from a data source  can include tables, relationships, and both unique and foreign key constraints  DataSet  a relational database in memory  working with data in a disconnected environment What is DataSet? Slide 3  To manipulate the data without an open connection  To perform database modifications  To bind data  To transfer data between tiers have .NET framework (Linux, Unix, Windows…)  To send between computers across network by HTTP protocol in XML format  Power data structure  Event an application without database, we can using dataset instead of arraylist, array… Why DataSet? Slide 4 DataReader DataSet Connected Disconnected Does not support data binding Support data binding Access data in forward-only Access data in any direction Data is read-only Supports data updates No support for sorting and filtering Data can be easily sorted and filtered DataReader - DataSet: main differences Slide 5  Two kinds of DataSet: untyped and typed  Untyped-Dataset: from generic type  Typed-Dataset: from schema (.xsd)  Two ways to create DataSet  By programmatically : for untyped-Dataset (see next)  By using design-time tools (the DataSet Designer and the Data Source Configuration Wizard) : for typed-Dataset  Homework: Module 11 Creating DataSet objects Slide 6 DataSet structure DataSet DataTable Collection DataTable DataRowCollection DataColumnCollection ParentRelations ChildRelations ConstraintCollection DataView DataRelationCollection Slide 7  DataTable represents one table in DataSet  To add a DataTable to DataSet:  dataset.Tables.Add(DataTable tbl)  To access a table in a DataSet:  Using index : dataset.Tables[0]  Using name: dataset.Tables[“authors”]  To access to data-cell  table.Rows[rowindex][colindex]  Properties of DataTable:  Columns property  Rows property  DefaultView  PrimaryKey property DataTable structure DataSet DataTable DataRow DataColumn DataTable DataRow DataColumn Slide 8  DataColumn describes one column of data in a DataTable  Get a column of a table:  table.Columns[index]  table.Columns[“colName”]  Some properties, methods  ColumnName  DataType  AllowDBNull  AutoIncrement  DefaultValue DataColumn structure DataSet DataTable DataRow DataColumn DataTable DataRow DataColumn Slide 9  A DataRow contains the data of a row in the DataTable  Create a DataRow  table.NewRow()  Delete a DataRow  datarow.Delete()  Get a row of a table:  table.Rows[index]  table.Rows[“colName”]  Visit all rows in a table:  foreach (DataRow dr in table.Rows) { …} DataRow structure DataSet DataTable DataRow DataColumn DataTable DataRow DataColumn Slide 10 [...]... DataColumn childColumn); parentDatatable.Constraints.Add(fk); Slide 12 Add a relationship between tables within a DataSet  DataRelation Slide 13 Example 1: Create a dataset with one table // 1a Create a DataSet object DataSet dataset = new DataSet( ); // 1b Add DataTable(s) DataTable tableLop = new DataTable("Lop"); dataset. Tables.Add(tableLop); // 1c Add columns to a Data Table tableLop.Columns.Add("MaLop",...Creating DataSet objects  Populating DataSet manually 1 Construct your own DataSet (schema) a b c d 2 Create DataSet Add DataTable(s) Add DataColumn(s) to DataTable Add constraints to DataTable Fill data into DataTable(s) in DataSet a b Add DataRow to DataTable Processing data in dataset … Slide 11 Add constraints to a DataTable  Primary constraint datatable.PrimaryKey... typeof(int)); // 1d Create a primary key for a DataTable object tableLop.PrimaryKey = new DataColumn[]{ tableLop.Columns["MaLop"] }; // 2a Add rows to a DataTable DataRow dr = tableLop.NewRow(); dr["MaLop"] = "TH99"; dr["TenLop"] = "Tin hoc 99"; dr["SiSo"] = 10 0; tableLop.Rows.Add (dr); Slide 14 Example 2: Create a dataset with tables DataSet dataset = new DataSet( ); DataTable tableLop, tableSV; dataset. Tables.Add(... tableLop.Columns["MaLop"], tableSV.Columns["MaLop"]); tableLop.Constraints.Add(fk); Slide 15 Display data to controls from DataSet  Get table in dataset and process…  foreach( DataRow dr in dataset. Tables["tableName"].Rows ) { // do something with DataRow dr } Example: display tableSV to ListView private void HienLenListView( DataSet ds, ListView lvw ) { lvw.Items.Clear(); foreach ( DataRow dr in ds.Tables["SinhVien"].Rows... dr["HoTen"].ToString() ); li.SubItems.Add( dr["MaLop"].ToString() ); } } Slide 16  You can use the IsNull method of the DataRow class to test for null values from a database Slide 17 Quick check    How do you add a DataTables to a DataSet? How do you retrieve a DataTable from a DataSet? How do you add data to a DataTable? Slide 18 Lab: with DataTable  Lab: Create a DataTable, p.353, with notes:  ... DataAdapter retrieves data into a DataSet from a data source by using the Fill method  Example: OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from danhba"; cmd.Connection = conn; // opened connection DataSet ds = new DataSet( ); OleDbDataAdapter da = new OleDbDataAdapter(cmd); da.Fill(ds, "DanhBa"); // process dataset, see next… Slide 30 Update... represents a set of SQL commands and a database connection that are used to fill the DataSet and update the data source   The DataAdapter serves as a bridge between a DataSet and a data source for retrieving and saving data How to use DataAdapter with data providers? Database Connection Windows Form Command DataAdapter DataSet Slide Disconnected 28 Create DataAdapter object  Create DataAdapter object:... DataTable, p.377, with notes:  Use RejectChanges and AcceptChanges method Slide 19 Chapter 3 Binding, linking, update data in a disconnected environment Lesson 2: DataAdapter Connected & Disconnected environment  Two ways access data using with ADO.NET Connected DataReader Database Connection Windows Form Command DataAdapter DataSet Disconnected Slide 25 Disconnected data access Advantages    Single... OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);  Demo  Update change to database from dataset Slide 33 Update data using DataAdapter (cont.) Demo Module 12 -3  Example: add a new row into DanhBa table OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from danhba where 1= 2"; // chỉ lấy cấu trúc cmd.Connection = conn; OleDbDataAdapter da = new OleDbDataAdapter(cmd);... OleDbDataAdapter(cmd); da.Fill(ds, "DanhBa"); // process dataset, see next… Slide 30 Update data using DataAdapter (p.359)   Using the Update method of the DataAdapter to submit any changes made to the DataSet back to the data source To do that, the DataAdapter must have DataAdapter commands: DeleteCommand, InsertCommand, UpdateCommand   When Update method is called, the DataAdapter analyzes the changes

Ngày đăng: 13/05/2014, 12:19

Xem thêm

w