Ddcauado.net Session05

23
Slide 1 of 23 Ver. 1.0 Developing Data-Centric Applications Using ADO.NET Session 5 In this session, you will learn to: Working in a disconnected environment Objectives

Transcript of Ddcauado.net Session05

Page 1: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 1/23

Slide 1 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

In this session, you will learn to:

Working in a disconnected environment

Objectives

Page 2: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 2/23

Slide 2 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

In a disconnected environment, data is stored in datasets

and manipulations are performed in the datasets.

 After the data has been manipulated in the dataset, the

changes are updated to the database.

 A dataset is a disconnected, cached set of records that areretrieved from a database. 

The dataset acts like a virtual database containing tables,

rows, and columns.

The two main types of datasets are:

Typed datasetUntyped dataset

Let us discuss each of these types in detail.

Working with Datasets and Datatables

Page 3: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 3/23

Slide 3 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Typed Dataset:

 A typed dataset is derived from the DataSet class and has an

associated XML schema, which is created at the time of 

creation of the dataset.

The XML schema contains information about the dataset

structure such as the tables, columns, and rows.

The XML Schema Definition (XSD) language is used to define

the elements and attributes of XML documents.

The structure of a typed dataset is decided at the time of its

creation.

When a typed dataset is created, the data commands aregenerated automatically by using the column names from the

data source.

Working with Datasets and Datatables (Contd.)

Page 4: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 4/23

Slide 4 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Untyped Dataset:

 An untyped dataset does not have any associated XML

schema.

In an untyped dataset, the tables and columns are represented

as collections.

Because an XML schema is not created for an untyped

dataset, the structure of an untyped dataset is not known

during compilation.

Untyped datasets find their use in cases where the structure of 

the schema is not decided during compilation or the data being

used does not have a definite structure.

Working with Datasets and Datatables (Contd.)

Page 5: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 5/23

Slide 5 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A dataset has its own object model, as shown in the

following figure.

Working with Datasets and Datatables (Contd.)

Page 6: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 6/23

Slide 6 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A dataset is created with the help of a DataSet object.

The DataSet object is present in a DataSet class and is

defined in the System.Data namespace.

The DataSet object contains a collection of DataTable 

objects, each containing one or more tables. A DataTable object contains one or more columns, each

represented by a DataColumn object.

To add a column to a DataTable, create a DataColumn 

object and call the Add() method on the Columns 

collection, which enables you to access the columns in adatatable.

Working with Datasets and Datatables (Contd.)

Page 7: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 7/23Slide 7 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

The following list describes some parameters that can be

specified for a column:

The name of the column

The data type of the column

Whether the column is read onlyWhether the column permits null values

Whether the value of the column must be different in each row

Whether the column is an auto-increment column

Whether the column is an expression column

Working with Datasets and Datatables (Contd.)

Page 8: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 8/23Slide 8 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Consider the following code snippet used for addingcolumns in a DataTable:

DataSet ds = new DataSet();

DataTable dt = ds.Tables.Add();

dt.Columns.Add(“Store Id”, typeof(string)); dt.Columns.Add(“Store Name”, typeof(string)); 

dt.Columns.Add(“Address”, typeof(string)); 

Creating a DataSet 

object Creating a DataTable 

object

 Adding columns in aDataTable

Working with Datasets and Datatables (Contd.)

Page 9: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 9/23Slide 9 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A DataTable object also has a Rows collection that allows

rows to be accessed in a dataset.

The various methods performed on a row by using theDataRow object are:

Add()InsertAt()

Find()

Select()

Remove()

RemoveAt()Delete() Removes a row provisionally from a table 

 Appends the row to the end of the table

Inserts the row at a specified position

 Accesses a row in a table by its primary key value

Finds rows that match a specified condition

Removes the specified DataRow object

Removes the row at a specified position

Working with Datasets and Datatables (Contd.)

Page 10: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 10/23Slide 10 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Which method of the DataRow object accesses a row in a

table by its primary key value?

1. Find()

2. Select()

3. InsertAt()

4. Add()

Just a minute

 Answer:

1. Find()

Page 11: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 11/23Slide 11 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

In ADO.NET, you can navigate through multiple tables to

validate and summarize the data by using theDataRelation object.

By using primary key and foreign key constraints that use aDataRelation object, you can create the relationship

between multiple tables.

The primary key is a unique index and ensures the

uniqueness of data stored in that table for that particular 

row. 

The foreign key is a constraint on a table that can referenceone or more columns in that table.

The table that has a primary key constraint is known as the

Parent table, and the table that has a foreign key constraint

is known as the Child table.

Working with Datasets and Datatables (Contd.)

Page 12: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 12/23Slide 12 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A DataSet.Relations property gets the collection of 

relations that link tables and allow navigation from Parent

tables to Child tables. 

The Rule enumeration indicates the action that occurs

when a foreign key constraint is enforced. 

The various Rule enumeration values are:

Cascade

None

SetDefault

SetNull 

Deletes or updates the child DataRow object when the parent

DataRow object is deleted or its unique key is changed

Throws an exception if the parent DataRow object is deleted or 

its unique key is changed

Sets the foreign key column(s) value to the default value of theDataColumn object(s), if the parent DataRow object is deleted

or its unique key is changedSets the foreign key column(s) value to DbNull, if the parent

DataRow object is deleted or its unique key is changed

Working with Datasets and Datatables (Contd.)

Page 13: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 13/23Slide 13 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Sometimes, the data available in one DataSet can be

merged with another DataSet. Or, a copy of the

DataTable objects can be created so that the user can edit

or modify data, which can then be merged back to the

original dataset.

The Merge() method is used to combine data from multiple

DataSet, DataTable, and DataRow objects.

During merging of data within datasets, theMissingSchemaAction enumeration specifies the action

to be taken when the data added to the dataset and therequired DataTable or DataColumn is missing.

Working with Datasets and Datatables (Contd.)

Page 14: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 14/23Slide 14 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

The various values of MissingSchemaAction 

enumeration are:

Add

AddWithPrimaryKey

Error

Ignore 

 Adds the DataTable and DataColumn

objects to complete the schema

 Adds the DataTable, DataColumn,

and PrimaryKey objects to complete

the schemaThrows an exception if the DataColumn 

does not exist in the DataSet that is

being updated

Ignores data that resides in

DataColumns that are not in the

DataSet being updated

Working with Datasets and Datatables (Contd.)

Page 15: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 15/23Slide 15 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A dataview provides a dynamic view of data stored in a

datatable. 

If any data is modified in the datatable, the dataview

associated with the datatable will also show the modified

data.

Let us understand the object model of a dataview.

Working with Dataviews

Page 16: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 16/23Slide 16 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

The following figure shows the object model of a dataview.

Working with Dataviews (Contd.)

DataSet Object

DataTable Object

DataView Objects

Alternative

Sort/Filter Criteria

Additional Views

DataView Object

Default Sort/Filter Criteria

DefaultView

Page 17: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 17/23Slide 17 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A DataView object creates a fixed customized view of a

given DataTable object.

You can create a DataView object to display the data

based on a criterion and another DataView object to

display the data based on a different criterion.

The following figure shows how customized data is

displayed through dataviews.

Windows

Application

DataView1

DataView2

DataView3

Filter3

Filter2

Filter1

DataTable

Working with Dataviews (Contd.)

Page 18: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 18/23Slide 18 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

 A dataview provides a sorted or filtered view of data in a

datatable.

Sorting in a DataTable by using a DataView object is

done by using the Sort property.

Filtering a DataTable by using the DataView object isdone by using the RowFilter and RowStateFilter 

properties.

Working with Dataviews (Contd.)

Page 19: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 19/23Slide 19 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Which property of the DataView object returns a subset of 

rows on the basis of the column values?

1. FindRows

2. RowFilter

3. RowStateFilter

4. Find

Just a minute

 Answer:

2. RowFilter

Page 20: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 20/23Slide 20 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

Problem Statement:

Jane, a member of the HR team at Tebisco, needs to store the

details of employees who have recently joined the

organization. You need to create an application for Jane that

will enable her to add and save the details of new employees,

and if required, delete records of employees who are no longer working in the organization. The employee code should get

automatically generated based on the format in the table. The

employee details will be stored in the empdetails table of the

HR database.

Demo: Manipulating Data in a Disconnected Environment

Page 21: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 21/23Slide 21 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

In this session, you learned that:

 A dataset, which is a part of disconnected environment, is a

disconnected, cached set of records that are retrieved from a

database. 

The two main types of datasets are:

Typed datasets

Untyped datasets

 A typed dataset is derived from the DataSet class and has an

associated XML schema, which is created at the time of 

creation of a dataset.

 An untyped dataset does not have any associated XMLschema. As a result, the structure of an untyped dataset is not

known during compilation.

Summary

Page 22: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 22/23Slide 22 of 23Ver. 1.0

Developing Data-Centric Applications Using ADO.NET

Session 5

The DataSet object contains a collection of DataTable 

objects, each containing one or more tables.

 A DataTable object contains one or more columns, each

represented by a DataColumn object.

 A DataTable object also has a Rows collection, which allows

rows to be accessed in a dataset. A DataTable objectcontains one or more rows, each represented by a DataRow 

object.

The DataRelation object is used to navigate through

multiple tables to validate and summarize the data.

The primary key and foreign key constraints in aDataRelation object create the relationship between the

tables in a schema.

Summary (Contd.)

Page 23: Ddcauado.net Session05

7/29/2019 Ddcauado.net Session05

http://slidepdf.com/reader/full/ddcauadonet-session05 23/23

Developing Data-Centric Applications Using ADO.NET

S i 5

The Merge() method is used to combine data from multiple

DataSet, DataTable, and DataRow objects.

 A dataview, which is a part of the disconnected environment,

enables you to create a dynamic view of data stored in a

datatable.

 A DataTable can have multiple DataView objects assignedto it, allowing the data to be viewed in different ways without

having to re-read from the database.

Sorting in a DataTable by using the DataView object is done

by using the Sort property.

Filtering in DataTable using DataView object is done byusing the RowFilter and RowStateFilter properties.

Summary (Contd.)