Module 4: Building DataSets

96 288 0
Module 4: Building DataSets

Đ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

Contents Overview 1 Lesson: Working in a Disconnected Environment 2 Lesson: Building DataSets and DataTables 5 Lesson: Binding and Saving a DataSet 18 Lab 4.1: Building, Binding, Opening, and Saving DataSets 25 Lesson: Defining Data Relationships 37 Lesson: Modifying Data in a DataTable 43 Lesson: Sorting and Filtering 54 Review 60 Lab 4.2: Manipulating DataSets 62 Module 4: Building DataSets Information 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. Module 4: Building DataSets iii Instructor Notes This module teaches students how to build and manage DataSets, define data relationships, modify data, and use DataViews. Because practices in this module build on files built during Lesson 1, the starter file for Lesson 2 is the solution file for Lesson 1. Lesson 3 and practices in other lessons also build upon each other. After completing this module, students will be able to: ! Describe the disconnected environment. ! Build a DataSet and a DataTable. ! Bind a DataSet to a DataGrid. ! Open and save a DataSet. ! Define a data relationship. ! Modify data in a DataTable. ! Find and select rows in a DataTable. ! Sort and filter a DataTable by using a DataView. To teach this module, you need the following materials: ! Microsoft® PowerPoint® file 2389B_04.ppt ! Module 4, “Building DataSets” ! Lab 4.1, Building, Binding, Opening, and Saving DataSets ! Lab 4.2, Manipulating DataSets 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 This module 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 Microsoft 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 as instructor-led demonstrations. Presentation: 90 Minutes Lab: 60 Minutes Required materials Preparation tasks Hyperlinked code examples Practices iv Module 4: Building DataSets How to Teach This Module This section contains information that will help you to teach this module. Lesson: Working in a Disconnected Environment This section describes the instructional methods for teaching each topic in this lesson. Discussion Questions: Personalize the following questions to the background of the students in your class. ! Describe some typical business scenarios in which you would build a disconnected application. ! Although many of the same classes are used in the disconnected and connected environments, what are the differences between how you use the classes? Disconnected Applications Module 4: Building DataSets v Lesson: Building DataSets and DataTables This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: This module focuses on defining a DataSet programmatically by using the object model. In the real world, developers are likely to spend more time using XML Schema Definitions (XSD) to define the initial schema, and to use code for applications that need to be more dynamic. Using XSD to define the initial schema is covered in Module 6, “Building DataSets from Existing Data Sources,” in Course 2389B, Programming with Microsoft ADO.NET. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What type of application do you plan to create in which you can use DataSets? ! Why are DataSets and the disconnected environment suited to that type of application? Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some advantages of the DataSet object model? ! How is the DataSet object model similar to or different from relational database structure? Technical Notes: ! Show students how to create a DataSet, a DataTable, and a DataColumn in Microsoft Visual Basic® and Microsoft Visual C#™ by clicking the Visual Basic Example or C# Example buttons on the PowerPoint slide for this topic. ! In the sample for creating DataColumns, point out the use of the GetType statement in Visual Basic and the use of the typeof statement in Visual C#. It is possible to use System.Type.GetType("System.Int32") to do the same thing in both languages if users want to be consistent, but the Microsoft Visual Studio® .NET tools will use the shorthand syntax. ! The System.Data.DbType enumeration can be used to list all of the data types available for data columns and parameters. Discussion Questions: Personalize the following questions to the background of the students in your class. ! When creating Microsoft ADO.NET objects, why might you want to separate the declaration statement from the instantiation statement? When would you want to combine these statements? ! What other exceptions might occur that you will have to control when creating DataTables and DataColumns? What Are DataSets, DataTables, and DataColumns? The DataSet Object Model How to Create a DataSet, a DataTable, and a DataColumn vi Module 4: Building DataSets Transition to Practice Exercise: Now that you have seen several examples of building DataSets and DataTables programmatically, you can now practice creating a DataSet and a DataTable. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: 1. In Visual Studio .NET, start a new Microsoft Windows ® Application project. 2. Drag a DataSet from the Data section of the Toolbox onto the form. 3. In the Property window, rename the DataSet. For the (Name) property, use dsNorthwind. For the DataSetName property, use Northwind. 4. Use the collection builder to add a DataTable to the Tables property of the DataSet. 5. For the (Name) property, use dtProducts. For the TableName property, use Products. 6. Use the collection builder to add three DataColumns to the Columns property of the DataTable. 7. For the (Name) property, use dcProductID. For the ColumnName property, use ProductID. 8. For the DataType property, choose System.Int32. 9. Repeat steps 7 and 8 for the ProductName and UnitPrice fields. 10. Use the code editor to manually write code to create the fourth field. Hint: Use the code that is automatically generated for the UnitPrice field as a guide; you can also copy and paste the code and then edit it. 11. Return to the form view and use the Property window to verify that the designer recognizes the new code that you have added. If not, check that you modified the call to the AddRange method for the DataTable that adds references to the DataColumns. After the practice: ! What lessons did you learn during the practice exercise? ! What did you discover as you created the DataSet, DataTable, and DataColumns? Discussion Questions: Personalize the following questions to the background of the students in your class. ! When would you use the PrimaryKey property to define a primary key versus using the unique constraint? ! Describe an example of a situation where two columns need to be defined as a primary key. How to Create a Primary Ke y Constraint Module 4: Building DataSets vii Transition to Practice Exercise: Using the example I just showed you as reference, you can practice programmatically creating a unique constraint for the Northwind DataSet. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: In Visual Studio .NET, insert code in the Form1_Load event to create a unique constraint for the product name column. Quick solution: Me.dcProductName.Unique = True Recommended solution: Me.dtProducts.Constraints.Add( _ New UniqueConstraint("UC_ProductName", Me.dcProductName)) After the practice: ! Why do you think the recommended solution is preferred over the quick solution? ! What lessons did you learn during the practice exercise? ! What did you discover as you created the unique constraint? ! How will you use unique constraints in your data applications? Technical Notes: Custom expressions can reference other columns in the table. They can also use summary functions such as Count and Sum that apply to all of the values in the specified column. If a DataRelation for a parent table exists, the summary functions can be grouped by parent row by using the Child object; otherwise the entire table groups them. For example: =Sum(Child.UnitPrice) =Sum(UnitPrice) In the first example, the parent groups the UnitPrice values. In the second, the table groups them. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some other examples of situations where an expression column would be used? ! Could an expression column be used for concatenation of column values? Transition to Practice Exercise: Using the syntax printed in the student workbook, you can create a custom expression for the Northwind DataSet. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Using Unique Constraints Creating Custom Expressions viii Module 4: Building DataSets Practice Solution: 1. In Visual Studio .NET, in the Property window, add the new column to the Products table, –or– 2. Write code to add a fifth column with an expression, as shown in the following example: ' Visual Basic Dim dcStockValue As New System.Data.DataColumn( _ "StockValue", GetType(System.Decimal)) dcStockValue.Expression = "UnitPrice*UnitsInStock" dcStockValue.ReadOnly = True After the practice: ! What lessons did you learn during the practice exercise? ! How can you use expression columns in the applications that you are building at your job? Module 4: Building DataSets ix Lesson: Binding and Saving a DataSet This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: Almost all controls have a (DataBindings) property with an (Advanced) subproperty that allows any column to be bound to any control property. This is much more flexible than previous data access models that typically only allowed the Caption or Text properties to be bound. Discussion Questions: Personalize the following question to the background of the students in your class. • What is the advantage of being able to bind a DataSet to a Windows control? Give an example of using this feature in an application. Procedure: Demonstrate the programmatic and graphical procedures for creating a simple data-bound control, or refer students to the procedure in the student workbook. Transition to Practice Exercise: Now that you have seen how to bind a DataSet to a DataGrid both programmatically and by using the graphical tools, choose the method that you would like to use, and turn to the practice exercise at the end of this topic in the student workbook. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: 1. Open the Visual Studio .NET development environment and the project that includes the Northwind DataSet. 2. Add five TextBox controls and a DataGrid control to the form. 3. In the Property window, set the DataBindings for the TextBox controls. Bind the Text property of each box to the five columns in the Products table in the Northwind DataSet. Do not bind to the dtProducts variable. If you do, you will only see the first row. 4. Set the DataSource for the DataGrid to the Northwind DataSet. 5. Set the DataMember of the DataGrid to the DataTable. Notice that the TextBox controls display the same information as the currently selected row in the DataGrid. 6. Notice that you cannot have two rows with the same ProductName, but that you can have two records with the same ProductID. After the practice: ! How many of you used the graphical tools to bind the DataSet to the DataGrid, and how many of you did it programmatically? ! What are the differences between binding a DataSet to a simple Windows control and binding a DataSet to a DataGrid? How to Bind Data to a Windows Control How to Bind a DataSet to a DataGrid Caution x Module 4: Building DataSets Transition to Practice Exercise: The purpose of this practice is to provide experience saving and opening a DataSet by using WriteXml and ReadXml. Northwind Traders needs an application that allows the easy editing of product information and persists that data to disk. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: The solution for this practice is located at <install folder<\Practices\Mod04_1\Lesson3\PersistingDataSets\ How to Save and Open a DataSet [...]... that you created? ! Did any of you sort and filter the Products DataTable by using the DataView? What kinds of filters did you create? xvi Module 4: Building DataSets Review: Building DataSets This section provides the answers to review questions at the end of this module 1 What is the difference between a DataTable and a DataView? DataTable objects are used to represent the tables in a DataSet A DataTable... GetChildRows method of the DataRow object Module 4: Building DataSets 1 Overview ! Working in a Disconnected Environment ! Building DataSets and DataTables ! Binding and Saving a DataSet ! Defining Data Relationships ! Modifying Data in a DataTable ! Sorting and Filtering *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction This module presents the concepts and... rows from tables in a SQL Server data source It is returned by the ExecuteReader method of the XxxCommand class, typically as a result of a SELECT SQL statement Module 4: Building DataSets 5 Lesson: Building DataSets and DataTables ! What Are DataSets, DataTables, and DataColumns? ! The DataSet Object Model ! How to Create a DataSet, a DataTable, and a DataColumn ! How to Create a Primary Key Constraint... DataSets and XML DataSets represent data in a relational view regardless of its source However, data in a DataSet can be represented in XML format The integration of DataSets with XML enables you to define the structure of a DataSet schema For more information, see Module 5, “Reading and Writing XML With ADO.NET,” in Course 2389B, Programming with Microsoft ADO.NET Module 4: Building DataSets 7 The... primary key ! Use unique constraints ! Create custom expressions 6 Module 4: Building DataSets What Are DataSets, DataTables, and DataColumns? DataSet DataTable DataTable Connection Stored Procedure Database Server Data Store *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction In ADO.NET, DataSets, DataTables, and DataColumns enable you to represent data... format DataSets can be saved as a combination of XSD (for the structure) and XML (for the data) You might need to convert other data sources into a schema that the DataSet object recognizes by using an intermediate XmlDataDocument object BizTalk and electronic data interchange Microsoft BizTalk™ Server uses XML-Data Reduced (XDR), an early form of XSD, to send and receive messages 4 Module 4: Building DataSets. .. End Try 11 12 Practice Module 4: Building DataSets Northwind Traders needs to build an application that includes data related to its products You will use the Visual Studio NET development environment graphical tools to build a Microsoft Windows® Application solution The solution for this practice is at the following location: \Practices\Mod04_1\Lesson2\CreateDataSets\ 1 Create a new... constructor or the Add method and practice creating constraints and a DataRelation object Instruct students to turn to the practice exercise at the end of this topic in the student workbook xii Module 4: Building DataSets Practice Solution: In Visual Studio NET, open the file that contains the DSCatProd DataSet, and define the following relationships for the DSCatProd DataSet on the Form1_Load event 1... DataSet to a DataGrid ! Open and save a DataSet ! Define a data relationship ! Modify data in a DataTable ! Find and select rows in a DataTable ! Sort and filter a DataTable by using a DataView 2 Module 4: Building DataSets Lesson: Working in a Disconnected Environment ! Typically disconnected scenarios ! NET Framework classes used in scenarios *****************************ILLEGAL FOR NON-TRAINER USE******************************... completing this lesson, you will be able to: ! Describe some typical disconnected data access applications ! Describe the Microsoft® ADO.NET classes that are used in disconnected applications Module 4: Building DataSets 3 Disconnected Applications SQL Server 2000 Categories SQL Server 6.5 Products Customers SqlDataAdapter DataSet Categories Products Orders OleDbDataAdapter Employees XML XML File File . Design Time xvi Module 4: Building DataSets Review: Building DataSets This section provides the answers to review questions at the end of this module. 1. What. examples Practices iv Module 4: Building DataSets How to Teach This Module This section contains information that will help you to teach this module. Lesson:

Ngày đăng: 22/10/2013, 16:15

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan