This module teaches students about the design of data-centric applications including connected and disconnected environments, Microsoft ADO.NET architecture, and ADO.NET and XML.. A conn
Trang 1Contents
Overview 1
Lesson: Design of Data-Centric Applications 2
Review 28
Lab 1.1: Data-Centric Applications and
ADO.NET 30
Module 1: Data-Centric Applications and
ADO.NET
Trang 2Information in this document, including URL and other Internet Web site references, is subject to change without notice Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, places or events is intended or should be inferred Complying with all applicable copyright laws is the responsibility of the user Without limiting the rights under copyright, no part
of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted
in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property
2001-2002 Microsoft Corporation All rights reserved
Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk, IntelliSense, JScript, MSDN, SQL Server, Visual Basic, Visual C#, Visual C++, Visual J#, Visual Studio, and Windows Media are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A and/or other countries
The names of actual companies and products mentioned herein may be the trademarks of their respective owners
Trang 3Instructor Notes
This course contains code samples in two programming languages: Microsoft®
Visual Basic® and Microsoft Visual C#™ The use of these two programming languages will accommodate students who are currently Visual Basic programmers and might be considering working with Visual C#, and students who are C, Microsoft Visual C++®, Java, and Visual C# programmers who do not have Visual Basic experience The lab exercises have solutions in both languages Most examples are also in both programming languages, except when the differences between the languages are minor
Access the code examples by clicking the hyperlinks at the bottom of the Microsoft PowerPoint® slides during your presentation The examples are displayed in Microsoft Internet Explorer The examples for each module are contained in a single htm file for that module You can navigate to different examples within the example files To avoid running multiple instances of Internet Explorer, close each instance after you have finished with a code example
This module teaches students about the design of data-centric applications (including connected and disconnected environments), Microsoft ADO.NET architecture, and ADO.NET and XML
After completing this module, students will be able to:
! Give examples of storage options
! Diagram the architecture of data-centric applications
! Choose a connected, disconnected, or mixed environment based on application requirements
! Use the System.Data namespaces in applications
! Diagram the ADO.NET object model
! Analyze typical business scenarios
! Describe the use of XML in ADO.NET
To teach this module, you need the following materials:
! Microsoft PowerPoint file 2389B_01.ppt
! Module 1, “Data-Centric Applications and ADO.NET”
! Lab 1, Data-Centric Applications and ADO.NET
To prepare for this module:
! Read all of the materials for this module
! Complete the practices and labs
! Read the latest NET Development news at http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28000519
Trang 4iv Module 1: Data-Centric Applications and ADO.NET
This module contains multimedia animations To start an animation, click the movie projector button on the PowerPoint slide for the multimedia When the multimedia window opens, click the Start button
This course contains code examples that are linked to text hyperlinks at the bottom of PowerPoint slides These examples enable you to teach from code examples that are too long to display on a slide All of the code examples for this module are contained in one htm file Students can copy the code directly from the browser or from the source, and paste it into a development
environment
To display a code sample, click a text hyperlink on a slide Each link opens an instance of Internet Explorer and displays the code associated with the link At the end of each example is a link that displays a table of contents of all examples in this module After you have finished teaching the code for an example, close the instance of the browser to conserve resources
If time constraints occur, one or more practices in this module can be presented
Trang 5How to Teach This Module
This section contains information that will help you to teach this module
Lesson: Design of Data-Centric Applications
This section describes the instructional methods for teaching each topic in this lesson This lesson should not take more than 25 minutes
Discussion Questions: Personalize the following question to the background of
the students in your class
• Which data storage models do you need to access in your company?
Technical Notes: The key word is constantly connected
Be careful not to get drawn into a philosophical discussion about what is a connected or disconnected environment
Discussion Questions: Personalize the following questions to the background
of the students in your class
! What are the advantages of the connected environment?
! What are the disadvantages of using a connected environment?
Technical Notes:
! The key word is constantly connected
! Emphasize that most real-world environments use a combination of connected and disconnected solutions
Discussion Questions: Personalize the following question to the background of
the students in your class
• Do your applications use a connected or disconnected environment?
Trang 6vi Module 1: Data-Centric Applications and ADO.NET
Technical Notes:
! Emphasize that most real-world environments use a combination of different tiered solutions
! Balance is a good key word here Improving one aspect of a solution tends
to worsen another Difficult design decisions must be made based on what is best for a particular situation
! Remember KISS: “Keep It Simple, Simon”
Discussion Questions: Personalize the following questions to the background
of the students in your class
! What are some problems with 2-tier applications? How scalable is this type
of application?
! What kinds of security are available with the different application models?
Transition to Practice Exercise: Instruct students to turn to the practice
exercise at the end of this topic in the student workbook
The purpose of this practice is to relate the topics we have discussed to applications that you have built or will build in your work environment
Data Access
Application Models
Trang 7Lesson: ADO.NET Architecture
This section describes the instructional methods for teaching each topic in this lesson This lesson should not take more than 20 minutes
Technical Notes: In some ways you can think of ADO.NET as a marketing
term that covers the classes in the System.Data namespace
Technical Notes: The students should already know how to reference
assemblies and import namespaces, but some will not, so be prepared to explain the fundamentals of namespaces Appendix D, “Microsoft NET Framework
Overview,” in Course 2389B, Programming with Microsoft ADO.NET, covers
the fundamentals of Microsoft NET, including namespaces
Transition to Practice Exercise: Instruct students to turn to the practice
exercise at the end of this topic in the student workbook
The purpose of this practice is to provide experience referencing an assembly and importing a namespace
Technical Notes:
The slide does not show all ADO classes (for example, Record and Stream) Many students will not have used the ADO Recordset in a disconnected scenario
You might want to spend a few minutes explaining ADO.NET functionality and limitations while discussing the benefits for DataSets
Transition to Practice Exercise: Instruct students to turn to the practice
exercise at the end of this topic in the student workbook
The purpose of this practice is to discuss different ways of using ADO.NET and relate the classroom discussions to applications you may be building or working with
Technical Notes: The ADO.NET object model consists of two major parts: the
DataSet classes and the NET data provider classes
The DataSet class has a Tables property, which gets a collection of DataTable objects in the DataSet, and a Relations property, which gets a collection of the DataRelation objects in the DataSet
Trang 8viii Module 1: Data-Centric Applications and ADO.NET
Technical Notes: Stress to the students that the connection is open during the
processing of the DataReader, and therefore resources are being used on the server
Discussion Questions: Personalize the following questions to the background
of the students in your class
Why is using the SQL Server NET Data Provider more efficient than using the OLE DB NET Data Provider for SQL Server?
What is the purpose of the XxxDataReader class?
Technical Notes: Stress to the students that the connection is open during the
retrieval of the dataset, but not during the processing of the data The processing can include reading the rows, sorting, filtering, and editing the data Therefore, resources are not used on the server while this processing occurs Later, a new connection is made and the changes are sent to the data source
Discussion Questions: Personalize the following question to the background of
the students in your class
What class is used in the disconnected environment that is not used in the connected environment?
Using ADO.NET Classes
in a Connected Scenario
Using ADO.NET Classes
in a Disconnected
Scenario
Trang 9Lesson: ADO.NET and XML
This section describes the instructional methods for teaching each topic in this lesson This lesson should not take more than 15 minutes ADO.NET and XML
is discussed in detail in Module 5: “Reading and Writing XML with
ADO.NET,” in Course 2389B, Programming with Microsoft ADO.NET
Technical Notes: Do not spend long on this topic, because you will need plenty
of time for the multimedia and demonstration Explain that ADO.NET uses XML extensively, and that the course covers XML features in detail later This multimedia animation describes how a developer can use ADO.NET to create a distributed solution to a business problem The Visual Studio NET solution includes two projects The first is an XML Web service and the second
is a Microsoft Windows® application that uses the Web service
Trang 10x Module 1: Data-Centric Applications and ADO.NET
Review: Data-Centric Applications and ADO.NET
This section provides the answers to review questions at the end of this module
1 What are the characteristics of a connected architecture? Describe some scenarios where a connected architecture is appropriate
A connected architecture is one where an application connects directly
to a data source, to query and modify the stored data
A connected architecture is appropriate for in-house applications that access a data source over a local area network Another example is an ASP.NET Web application that queries a database to generate a read- only report of the data
2 What are the characteristics of a disconnected architecture? Describe a scenario where a disconnected architecture is appropriate
A disconnected architecture is one where an application does not connect directly to a data source ADO.NET provides extensive support for building disconnected applications, to meet the needs of modern-day distributed systems
A disconnected architecture is appropriate for mobile workers At the start of the day, a private copy of customer and product data can be downloaded from the database During the day, the worker uses and modifies this copy of the data At the end of the day, the worker posts the data changes back to the database XML Web services have an important role to play in this style of application
3 What are the features and advantages of the XML Web service architecture?
Business services are made available to disconnected users XML Web services can retrieve private copies of data from a data source to enable users to work with the data remotely XML Web services also allow the user to post data updates back to the data source when required
Trang 114 How does ADO.NET increase the interoperability and scalability of disconnected systems?
XML makes interoperability possible Data from a data source can be expressed in XML format, which makes it easier to exchange the data between application tiers and across organizational boundaries XML is tightly integrated into the design and philosophy of ADO.NET
Scalability is achieved by enabling data to be cached locally in memory caches This reduces the number of active connections and database locks that are required, which means that more users can be supported at the same time
in-5 What is a DataSet, and why is it important in ADO.NET?
The DataSet class is the most fundamental concept in the design of the ADO.NET disconnected architecture A DataSet is an in-memory cache
of data tables, relationships, and constraints You can populate a DataSet from a SQL query, an XML document, or by creating tables, relations, and constraints programmatically by using code
DataSets usually contain a subset of the data in a central database You can manipulate a DataSet in a disconnected application You can also write out the contents of a DataSet in XML format, and pass it to other applications and services
6 Which NET data providers are included in the NET Framework?
The NET Framework includes two data providers: the SQL Server NET Data Provider, and the OLE DB NET Data Provider
The SQL Server NET Data Provider gives optimized access to SQL Server 2000 and SQL Server 7.0 databases
The OLE DB NET Data Provider gives access to SQL Server 6.5 (and earlier), Oracle, Sybase, and other databases
Trang 13Overview
! Design of Data-Centric Applications
! ADO.NET Architecture
! ADO.NET and XML
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
This module describes the design of data-centric applications, the Microsoft®
ADO.NET architecture, and the integration between ADO.NET and XML After completing this module, you will be able to:
! Give examples of storage options
! Diagram the architecture of data-centric applications
! Choose a connected, disconnected, or mixed environment based on application requirements
! Use the System.Data namespaces in applications
! Diagram the ADO.NET object model
! Analyze typical business scenarios
! Explain how to use ADO.NET with XML
Introduction
Objectives
Trang 142 Module 1: Data-Centric Applications and ADO.NET
Lesson: Design of Data-Centric Applications
! Data Storage
! What Is a Connected Environment?
! What Is a Disconnected Environment?
! Data Access Application Models
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
This lesson describes the design of data-centric application architecture and data storage options
After completing this lesson, you will be able to:
! Give examples of common types of data storage
! Choose between a connected and disconnected application environment
! Diagram how data access application models have evolved
Introduction
Lesson objectives
Trang 15Data Storage
ADO.NET supports the following types of data storage:
! Unstructured
! Structured, non-hierarchical data
" Comma Separated Value (CSV) files, Microsoft Excel spreadsheets, Microsoft Exchange files, Active Directory files, and others
! Hierarchical
" XML documents and others
! Relational database
" SQL Server, Oracle, Access, and others
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
Data storage is a method of storing specific items that together constitute a unit
of information Individual data items themselves are of little use; they become value resources only when put into context with other data items
The following table describes different methods of data storage
Unstructured Data has no logical order Simple memos
Structured, hierarchical
non-Data is separated into units, but the units are organized strictly by their order
Comma Separated Value (CSV) files or tab-separated files, Microsoft Excel spreadsheets, Microsoft Exchange files, Microsoft Active Directory™ files, indexed sequential access method (ISAM) files
Hierarchical Data is organized in a tree
structure, with nodes that contain other nodes
XML data documents
Relational database
Data is organized in tables, with columns containing a specific type
of data and rows containing a single record Tables can be related over columns with identical data
Microsoft SQL Server™
and Microsoft Access databases, Oracle databases
Object-oriented database
Data is organized as objects Objectivity/DB ADO.NET can support all of the data formats in the preceding table
Definition of data
storage
Types of data storage
ADO.NET support
Trang 164 Module 1: Data-Centric Applications and ADO.NET
What Is a Connected Environment?
! A connected environment is one in which users are constantly connected to a data source
! Advantages:
" Environment is easier to secure
" Concurrency is more easily controlled
" Data is more likely to be current than in other scenarios
! Disadvantages:
" Must have a constant network connection
" Scalability
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
For much of the history of computers, the only environment available was the connected environment
A connected environment is one in which a user or an application is constantly connected to a data source
A connected scenario offers the following advantages:
! A secure environment is easier to maintain
! Concurrency is easier to control
! Data is more likely to be current than in other scenarios
A connected scenario has the following disadvantages:
! It must have a constant network connection
! Scalability The following are examples of connected environments:
! A factory that requires a real-time connection to monitor production output and storage
! A brokerage house that requires a constant connection to stock quotes
Trang 17What Is a Disconnected Environment?
! In a disconnected environment, a subset of data from a central data store can be copied and modified independently, and the changes merged back into the central data store
! Advantages
connect to a data source at any time to process requests
performance of applications
! Disadvantages
" Data is not always up to date
" Change conflicts can occur and must be resolved
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
With the advent of the Internet, disconnected work scenarios have become commonplace, and with the increasing use of handheld devices, disconnected scenarios are becoming nearly universal Laptop, notebook, and other portable computers allow you to use applications when you are disconnected from servers or databases
In many situations, people do not work entirely in a connected or disconnected environment, but rather in an environment that combines the two approaches
A disconnected environment is one in which a user or an application is not constantly connected to a source of data Mobile users who work with laptop computers are the primary users in disconnected environments Users can take a subset of data with them on a disconnected computer, and then merge changes back into the central data store
A disconnected environment provides the following advantages:
! You can work at any time that is convenient for you, and can connect to a data source at any time to process requests
! Other users can use the connection
! A disconnected environment improves the scalability and performance of applications
A disconnected environment has the following disadvantages:
! Data is not always up to date
! Change conflicts can occur and must be resolved
A farmer has a Microsoft Windows® CE device running SQL Server CE that he uses to keep track of livestock when animals are born
Trang 186 Module 1: Data-Centric Applications and ADO.NET
Data Access Application Models
Evolution of data access
1-Tier (monolithic)
Client logic Business logic Data storage
3-Tier
“Thin” Client
N-Tier 2-Tier
“Fat” Client
Internet
XML Web service
XML Web service
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
Data access models have evolved with the evolution of computers, from highly localized to highly distributed As the number of users and the amount of data increased, data access models evolved from a single user on a single application
to multiple users on the Internet The latest development in this evolution is the XML Web service model
Within a data access model, a tier is a logical level or layer at which the logical components of an application reside, not a physical tier Tiers can reside on one
or more computers, or physical tiers The number of tiers refers to the number
of levels, not the number of physical computers, into which services are divided These levels typically include the following:
! Client tier, also known as the presentation or user services layer This tier contains the user interface
! Business logic tier, which contains the logic that interacts with the source of data This “middle” tier contains the part of the application that interacts with the data; for example, creating a connection to the source of the data The business logic tier is often physically implemented in all tiers; for example, as stored procedures in the data store, as classes within an application server, or as code within the client application
! Data services tier, which contains the data that the business logic uses in the applications
! Interoperability tier, which contains the logic that allows interaction between applications on different operating systems, or different types of data For example, XML Web services can be hosted on any operating system
Introduction
Definition of tier
Trang 19The major advantage of adding tiers is the ability to scale applications Each additional tier allows you to add more users and isolate a level of application logic Isolating the logic enables you to make changes to a specific area of an application without requiring changes to the other tiers
For example, in a 1-tier application, a change to any level of logic requires that the entire application be recompiled and redistributed
The following table compares the different types of data access models
1-tier, or Monolithic This model typically involves
a single user and all three layers in a single computer
For example, an old-style Microsoft Access database with a single user
Because everything is in one place, all components are easily accessible
Program update requires source code to be modified, recompiled, andredistributed for every user This model provides
no real ability to scale
2-tier – client/server The user layer and business
logic layer reside in one tier, data services on another This model typically involves two
or more computers For example, a business personnel database Often the business logic is split between the two tiers: some logic in the client application, and some as stored
procedures in the data tier
Provides some separation
Software distribution and maintenance problems
3-tier Each service is in a separate
layer Business logic moves into a new “Middle tier.”
Good separation of functions The client layer
is a “thin client” that contains only the client logic, or presentation layer
More complex to manage
Security is not as scalable/flexible as n-tier
N-tier An enterprise-level personnel
database where several clients access a single application server New tiers can be added as new logical needs occur
Allows different applications on different operating systems to interact with both the user and the data
Security issues
Remote procedure calls (RPC) cannot pass through firewalls
N-tier with Web interface Services are distributed
between the Internet and intranet, with additional tier and additional servers dedicated to the network
Zero client deployment costs The only updates are
to Web/application servers
HTTP can go through firewalls
Security issues
Benefits of tiers
Evolution of access
models
Trang 208 Module 1: Data-Centric Applications and ADO.NET
As a general guideline, keep in mind that as the number of tiers increases, the scalability and complexity of the data access model increase
Group discussion Ask a student to come to the front of the class, think of a
database application that he or she has developed in the past, and then use the whiteboard to:
1 Draw a diagram showing the data access application model the database application used
2 Draw the data storage formats the student needed to access
Did the architecture use a connected or disconnected environment?
Trang 21Lesson: ADO.NET Architecture
! What Is ADO.NET?
! What Are the Data-Related Namespaces?
! Evolution of ADO to ADO.NET
! The ADO.NET Object Model
! Using ADO.NET Classes in a Connected Scenario
! Using ADO.NET Classes in a Disconnected Scenario
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
This lesson introduces ADO.NET and establishes its place in the Microsoft NET Framework
After completing this lesson, you will be able to:
! Explain how to use ADO.NET
! Discuss how ADO.NET is divided into namespaces
! Diagram the ADO.NET object model
Introduction
Lesson objectives
Trang 2210 Module 1: Data-Centric Applications and ADO.NET
What Is ADO.NET?
ADO.NET is a set of classes for working with data
It provides:
enumerations that manage data access from within the NET Framework
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
ADO.NET is the next step in the evolution of Microsoft ActiveX® Data Objects (ADO) It does not share the same programming model, but shares much of the ADO functionality
ADO.NET is a set of classes for working with data
As application development has evolved, new applications have become loosely coupled based on the Web application model An increasing number of
applications use XML to encode data to be passed over network connections ADO.NET provides a programming model that incorporates features of both XML and ADO.NET within the NET Framework
ADO.NET provides the following advantages over other data access models and components:
! Interoperability ADO.NET uses XML as the format for transmitting data from a data source to a local in-memory copy of the data
! Maintainability When an increasing number of users work with an application, the increased use can strain resources By using n-tier applications, you can spread application logic across additional tiers ADO.NET architecture uses local in-memory caches to hold copies of data, making it easy for additional tiers to trade information
! Programmability The ADO.NET programming model uses strongly typed data Strongly typed data makes code more concise and easier to write because Microsoft Visual Studio® NET provides statement completion
! Performance ADO.NET helps you to avoid costly data type conversions because of its use of strongly typed data
! Scalability The ADO.NET programming model encourages programmers
to conserve system resources for applications that run over the Web
Because data is held locally in in-memory caches, there is no need to retain database locks or maintain active database connections for extended periods
Introduction
Definition
Business use case
Benefits
Trang 23What Are the Data-Related Namespaces?
! The data-related namespaces include:
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
The NET Framework divides functionality into logical namespaces, and ADO.NET is no exception ADO.NET is implemented primarily in the System.Data namespace hierarchy, which physically resides in the
System.Data.dll assembly Some parts of ADO.NET are part of the System.Xml namespace hierarchy, for example the XmlDataDocument class
The following table describes the data-related namespaces
Namespace Description
System.Data Core of ADO.NET Includes classes that make up the
disconnected part of the ADO.NET architecture For example, the DataSet classes
System.Data.Common Utility classes and interfaces that are inherited and
implemented by NET data providers
System.Data.SqlClient SQL Server NET Data Provider
System.Data.OleDb OLE DB NET Data Provider
System.Data.SqlTypes Classes and structures for native SQL Server data
types A safer, faster alternative to other data types System.Xml Classes, interfaces, and enumerations that provide
standards-based support for processing XML For example, the XmlDataDocument class
Introduction
The data-related
namespaces
Trang 2412 Module 1: Data-Centric Applications and ADO.NET
! Reference the System.Data assembly, import the System.Data namespace, and declare an object variable
1 Start the Visual Studio NET development environment
2 On the File menu, point to New, and then click Project
3 In the New Project dialog box, select the following options, and then click
4 In the Solution Explorer, right-click the References folder and choose Add
Reference
5 Select the System.Data.dll assembly, click Select, and then click OK
Referencing the System.Data assembly is done by default in Windows
Application projects, so the previous two steps are not usually necessary
6 Open the code editor, and insert the following code before the existing code:
' Visual Basic Imports System.Data Imports System.Data.SqlClient // Visual C#
7 Insert the following code in the Form1_Load event Notice that you do not need to specify the namespace for the DataSet and SqlConnection classes:
' Visual Basic Dim dsNorthwind As DataSet Dim cnNorthwind As SqlConnection // Visual C#
Trang 25Evolution of ADO to ADO.NET
XxxDataReader XxxDataAdapter
***************************** ILLEGAL FOR NON - TRAINER USE ******************************
Many changes have been made between ADO and ADO.NET Most of these changes are a response to studies of how developers use (and misuse) ADO Other changes have made ADO.NET more flexible, more powerful, and more scalable than ADO
Because not all NET data providers provide transactional functionality, ADO.NET moves that functionality into a separate class This also means that the new ADO.NET connection object is more lightweight than the previous ADO connection object
The ADO Recordset was a huge object in ADO It provided the ability to support multiple types of cursors, from a fast, lightweight “firehose” cursor, to
a disconnected client-side cursor that supported change tracking, optimistic locking, and automatic batch updates of a central database However, all of this functionality was difficult (or impossible) to customize
ADO.NET breaks the functionality of the old ADO Recordset into multiple classes, thereby allowing a focused approach to developing code The Data Reader is the equivalent of a “firehose” cursor The DataSet is a disconnected data cache with tracking and control binding functionality The DataAdapter provides the ability to completely customize how the central data store is updated with the changes to a DataSet
See Appendix B, “ADO and ADO.NET Comparison,” for more information about the reasons for the changes between ADO and ADO.NET
Introduction
Divide and conquer
Reference