Chapter 7
Create, Add, Delete, and Edit Data
in a Disconnected Environment
Objectives in chapter
Create a DataSet graphically.
Create a DataSet programmatically.
Add a DataTable to a DataSet.
Add a relationship between tables within a DataSet.
Navigate a relationship between tables.
Merge DataSet contents.
Copy DataSet contents.
Create a typed DataSet.
Create DataTables.
Manage data within a DataTable.
Create and use DataViews.
Represent data in a DataSet by using XML. (NO)
Use the OleDbDataAdapter object to access an ADO Recordset or Record.
Generate DataAdapter commands automatically by using the Command-Builder
object.
Before we go…
Con.connectionString=“….”;
/// con.Open(); -> do not need
cmd.Connectiion=con;
cmd.CommandType="Select ", conSinhVien)
cmd.CommandText=“Select * from …”;
DataAdapter da=new DataAdapter();
Da.SelectCommand=cmd;
Dataset Ds=new Dataset();
Da.Fill(Ds);
DataGrid1.DataSource=ds.tables[0];
Chapter content
Lesson 1:Creating DataSet Objects
Lesson 2: Creating DataTable Objects.
Lesson 3: Creating DataAdapter Objects
Lesson 4: Working with Data in
DataTable Objects
Lesson 6: Creating and Using DataView
Objects
Lesson 1:
Creating
DataSet
Objects
DataSet Objects
Creating DataSet Objects
Programmatically
Lesson 1:
Creating
DataSet
Objects
DataSet Objects
temporarily store the data
Like array, in memory
Dataset structure
Objective: Using in-memory database
DataSet Structure
DataTable Structure
DataColumn
DataRow
Build manual dataset
DataView
Example
Why DataSet?
Perform database modifications
always
disconnected
To transfer data between tiers have .NET framework
(Linux, Unix, Windows…)
To send between computers across network by HTTP
protocol in XML format
To manipulate the data without an open connection
To relate data from multiple sources
To bind data to a Windows form
Power data structure
Event an application without database, we can using dataset
… instead of arraylist, array…
Populating DataSets
Manual
From existing databae, using
DataAdapter
Read from XML data files
Build from XSD files
But before filling dataset…
… we need to know dataset structure
DataSet Components
DataTable
DataColumn
DataRow
Constraint
DataRelation
DataView
DataSet Object Model
DataSet
DataTable Collection
DataTable
DataRowCollection
DataColumnCollection
ParentRelations
ChildRelations
ConstraintCollection
DataView
DataRelationCollection
See next page
1.reside in the
System.Data
namespace
2.doesn’t care
where it came
from
Lesson 6Lesson 4
DataSet class tree model
Constraint
Constraint
s
s
Constraint
Constraint
Columns
Columns
Column
Column
DataSet
DataSet
Tables
Tables
Table
Table
Object
Collection
Relations
Relations
Relation
Relation
Rows
Rows
Row
Row
DataTable
DataSet
DataTable
DataRow
DataColumn
DataTable
DataRow
DataColumn
Represents one table in DataSet
Are accessed using the DataSet's
Tables property
Collection of DataTables
Using index : ds.tables(0)
Using name: ds.tables(“authors”)
Each DataTable has:
A Columns property
(Add,remove,removeat,Item)
A Rows property
(Add,remove,removeat,Item)
Access to data-cell
Table.Rows(index).Item(index)
Dim DT1 As New DataTable("TableName")
DataTable Core Pro./Methods
TableName property
A PrimaryKey property
array of datacolumn
Select(filter): datarow()
End of sql
NewRow
DefaultView
Example: Create table and dataset
Ds=new DataSet();
ds.DataSetName = "BookAuthors";
Tauthors =new DataTable("Author");
Ds.Tables.add (Authors);
DataTable Accessing example
myDataTable.Rows(5)
returns a DataRow object that is the 6
th
row of
myDataTable
myDataRow(2)
returns an object representing the data stored in the
3rd column of myDataRow
myDataTable.Rows(5)(2)
returns an object representing the data stored in the
3rd column of the 6
th
row of the myDataTable
DataColumn Class
Describes one column of data in a
DataTable
DataColumn objects describe
table's schema
DataAdapters can generate the
required DataColumns
Similar to column in datatable
Doesn’t have Value property
Core Pro. / methods
ColumnName
DataType
Ex. Gettype(integer)
AllowDBNull
AutoIncrement
DefaultValue
Unique
ReadOnly
DataSet
DataTable
DataRow
DataColumn
DataTable
DataRow
DataColumn
Example
DataColumn Cid =
authors.Columns.Add("ID",gettype(integer));
Cid.AutoIncrement = true;
DataColumn Cname =
authors.Columns.Add("Name", gettype(String));
authors.PrimaryKey = new DataColumn[] {Cid};
DataRow Class
A DataRow object provides access
to one row of data in a DataTable
A DataTable's DataRow objects
contain all the data in the
DataTable
Create
Table.NewRow method
Can access DataRow values by
providing:
Column name
Column number
DataColumn object
Core Pro./methods
Item(index) : get cell value
IsNull
Delete
DataSet
DataTable
DataRow
DataColumn
DataTable
DataRow
DataColumn
Populating DataSet-Manual
1.
Construct your own DataSet (schema)
1.
Create Dataset
2.
Add DataTable(s)
3.
Add DataColumn(s) to DataTable
4.
Define datatable schema
2.
Fill Data into DataTable(s) in DataSet
1.
Add DataRow to DataTable
2.
Processing data in dataset …
Dim ds As New DataSet , r As DataRow
With ds.Tables.Add("SV")
.Columns.Add("id")
.Columns.Add("Name")
End With
With ds.Tables("SV")
r = .NewRow()
r("id") = "aa"
r("name") = "bb"
.Rows.Add(r)
End With
DataView
An custom view of a DataTable
Sorting, filtering, searching, editing, and
navigating the data from a DataSet
Using same data memory from Datatable
Each DataTable have DefaultView
DataView1 DataView2
SV1 Nguyen thi A
SV2 Nguyen thi C
SV3 Nguyen thi B
DataTable
row1
row2
row3
row2
row3
row1
row1
row3
row2
Dataview
Changes DataView affect DataTable automatically
We can get Dataview direct from DataTable:
DataTable1.DefaultView , but only 1 view
Using DataView, we can have multiple view
d1 =New DataView(ds.Tables[0]);
d1.Sort = "Last name“;
d2 =New DataView(ds.Tables[0]);
d2.Sort = "first name“;
DataGrid2.DataSource = d1;
DataGrid3.DataSource = d2;
Lesson 1:
Creating
DataSet
Objects
two kinds : typed, and untyped.
Un-Typed Dataset: from generic type
Typed Dataset:from schema (.xsd)
DataSet Objects: 2 ways to creata
Declare a new DataSet object
programmatically for untype Dataset
Use design-time tools: for typed-Dataset
Lesson 1:
Creating
DataSet
Objects
DataSet Objects:
Choosing typed or Un-typed Dataset?
Demo1: Creating a typed DataSet with
the DataSet Designer
Lesson 1:
Creating
DataSet
Objects
Demo2: Creating a Typed DataSet with
the Data Source Configuration Wizard
Questions?
Lesson 1:
Creating
DataSet
Objects
Exercise 3: Configuring Untyped
DataSet Objects
Questions?