Tài liệu Module 2: Connecting to Data Sources pptx

82 393 0
Tài liệu Module 2: Connecting to Data Sources pptx

Đ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: Choosing a .NET Data Provider 2 Lesson: Defining a Connection 8 Lesson: Managing a Connection 16 Lesson: Handling Connection Exceptions 25 Lesson: Connection Pooling 37 Review: Connecting to Data Sources 48 Lab 2.1: Connecting to Data Sources 50 Module 2: Connecting to Data Sources 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 2: Connecting to Data Sources iii Instructor Notes This module explains the concepts and procedures necessary to create and manage a Microsoft ® ADO.NET connection to Microsoft SQL Server ™ or other data sources. After completing this module, students will be able to: ! Choose a .NET data provider. ! Connect to SQL Server. ! Connect to OLE DB data sources. ! Manage a connection. ! Implement and control connection pooling. To teach this module, you need the following materials: ! Microsoft PowerPoint ® file 2389B_02.ppt ! Module 2, “Connecting to Data Sources” ! Lab 2.1, “Connecting to Data Sources” 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?contenti d=28000519 This module contains multimedia animation(s). 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 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 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 there 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: 60 Minutes Lab: 90 Minutes Required materials Preparation tasks Multimedia Hyperlinked code examples Practices iv Module 2: Connecting to Data Sources How to Teach This Module This section contains information that will help you to teach this module. Lesson: Choosing a .NET Data Provider This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: The .NET data providers are the part of ADO.NET that communicates with specific data sources. Technical Notes: Briefly describe each class and its functionality. Explain that each of the four major classes will be discussed at greater length throughout the course. Technical Notes: The ODBC .NET Data Provider gives better performance than using the OLE DB .NET Data Provider with the OLE DB for ODBC provider. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What .NET data provider would you use to connect to a SQL Server 6.5 database? ! What .NET data provider would you use to connect to an Access database? ! Why should you not use the OLE DB .NET Data Provider to connect to SQL Server 2000? What Are .NET Data Providers? The .NET Data Provider Classes Which .NET Data Provider Should You Use? Module 2: Connecting to Data Sources v Lesson: Defining a Connection This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: Be careful not to get drawn into a detailed discussion about security. This section is about how to connect to secure databases by using ADO.NET. It is not designed to be a discussion of the merits of different security options. Discussion Questions: Personalize the following questions to the background of the students in your class. ! When would you use Microsoft Windows ® Authentication and when would you use Mixed Mode? ! What are the disadvantages of using Mixed Mode security? Discussion Questions: Personalize the following question to the background of the students in your class. • Out of the list of parameters for a connection string, which parameters are required to establish a connection? Technical Notes: Use the graphical tools to generate connection strings. Because the graphical tools write code anyway, they are now the easiest and best way to set connection strings. Transition to Practice Exercise: The purpose of this practice is to practice recognizing valid connection strings and to practice correcting broken connection strings. Be sure to go over examples before letting students do the practice. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Practice Solution: The following are answers to the practice exercises, with changes to the original code in bold. Exercise 1 The name of the provider was wrong, and the database path is specified by using the Data Source parameter, not the Initial Catalog. Use OLE DB .NET Data Provider Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\MyDB\MyDB.mdb; Exercise 2 We should be using Windows Authentication, not SQL standard security. Use SQL Server .NET Data Provider Data Source=ProdServ01;Initial Catalog=Pubs;Integrated Security=SSPI; Database Security What Is a Connection Strin g ? How to Set a Connection Strin g vi Module 2: Connecting to Data Sources Exercise 3 The names of the SQL Server and database should be switched. Use SQL Server .NET Data Provider Data Source=ProdServ01;Initial Catalog=Pubs;User ID=JohnK;Password=JohnK; Exercise 4 The SQL Server .NET Data Provider will not work with SQL Server 6.5. Use the OLE DB .NET Data Provider instead. Use OLE DB .NET Data Provider Provider=SQLOLEDB;Data Source=ProdServ01;Initial Catalog=Pubs;Integrated Security=True; Exercise 5 This code is correct, so no changes are required. Use SQL Server .NET Data Provider Data Source=ProdServ02;Initial Catalog=Northwind;Integrated Security=SSPI; Exercise 6 You should be using Windows Authentication, not SQL standard security. Use SQL Server .NET Data Provider DataSource=ProdServ02;Initial Catalog=Pubs;Integrated Security=SSPI; Module 2: Connecting to Data Sources vii Exercise 7 The connection timeout is measured in seconds, not minutes. Use SQL Server .NET Data Provider Data Source=ProdServ01;Initial Catalog=Pubs;Integrated Security=True;Connection Timeout=60; Exercise 8 This code is correct because 15 seconds is the default timeout, so no changes are required. Use SQL Server .NET Data Provider Data Source=ProdServ01;Initial Catalog=Pubs;Integrated Security=True; Exercise 9 Add an additional option to save the password in the connection string. Use SQL Server .NET Data Provider Data Source=ProdServ02;Initial Catalog=Pubs;User ID=JohnK;Password=JohnK;Persist Security Info=True; viii Module 2: Connecting to Data Sources Lesson: Managing a Connection This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: Make sure that you clearly explain the difference between closing, disposing, and setting a connection object to Nothing or null. Closing and disposing affect server resources. Setting the connection object to Nothing or null affects client resources. Technical Notes: If you are not familiar with Microsoft Visual C# ™ , make sure that you know how to manually add code to handle a non-default event. The default event can be handled by double-clicking the object on the graphical designer. For example, to automatically write code to the handle a form’s Load event, double-click the form’s caption bar. Transition to Practice Exercise: The purpose of this practice is to gain experience handling connection events. This practice uses the StatChange event, but the process for handling events would be similar for the other connection events. 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 in <install folder>\Practices\Mod02\Lesson3\CS\HandlingStateChange\ After the Practice: • Discuss answers for the questions that are at the end of the practice. Opening and Closing a Connection Handling Connection Events Module 2: Connecting to Data Sources ix Lesson: Handling Connection Exceptions This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: The students should already be familiar with this topic, so do not spend too much time describing structured exception handing in general terms. Always use ADO.NET objects in any examples that you demonstrate. Examples: The Visual Basic example showing connection exception handling can be demonstrated by commenting out the line of code that instantiates the connection object, which will cause a NullReferenceException, and by altering the connection string to cause another exception. Transition to Practice Exercise: The purpose of this practice is to help you become more familiar with exception handling by reviewing the exceptions that can occur when using specific methods within a class. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. After the Practice: ! What are some of the exceptions that can occur when calling the Open method? ! What are some of the exceptions that can occur when calling the ChangeDatabase method? Technical Notes: ! The key point here is that when a SqlException occurs, the Errors collection could contain more than one error, and therefore must be looped through. Usually there will only be one error, so the Errors collection could be ignored and only the SqlException properties read. However, this would not be the best practice. ! SqlExceptions are raised when serious problems occur on the computer running SQL Server. Transition to Practice Exercise: The purpose of this practice is to: 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 in <install folder>\Practices\Mod02\Lesson4\xx\CatchingSqlExceptions\ where xx is either VB or CS. What Is Structured Exception Handling? How to Handle SqlExceptions x Module 2: Connecting to Data Sources Technical Notes: ! InfoMessage events are raised when minor or potential problems occur on the SQL Server. The details are accessed through the SqlError class, similar to SqlExceptions, but these objects should be treated as informational messages rather than actual errors. ! Stress the usefulness of using the Print T-SQL statement for debugging. Discussion Questions: Personalize the following question to the background of the students in your class. • How could you use the InfoMessage event in your applications? Transition to Practice Exercise: The purpose of this practice is to practice using the InfoMessage event in an error-handling routine. 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 in <install folder>\Practices\Mod02\Lesson4\xx\HandlingInfoMessage\ where xx is either VB or CS. How to Handle the InfoMessage Event [...]... Profiler? xii Module 2: Connecting to Data Sources Review: Connecting to Data Sources This section provides the answers to review questions at the end of this module 1 What is a NET data provider? The NET data providers are a core component within the ADO.NET architecture that enables communication between a data source and a component, an XML Web service, or an application A data provider allows you to connect... necessary to create and manage a Microsoft® ADO.NET connection to Microsoft SQL Server™ or other data sources Objectives After completing this module, you will be able to: ! Choose a NET data provider ! Connect to SQL Server ! Connect to OLE DB data sources ! Manage a connection ! Handle common connection exceptions ! Implement and control connection pooling 2 Module 2: Connecting to Data Sources Lesson:... SELECT SQL statement XxxDataAdapter Uses XxxCommand objects to populate a DataSet, and resolves updates with the data source For example, the SqlDataAdapter class can manage the interaction between a DataSet and the underlying data in a SQL Server data source 6 Module 2: Connecting to Data Sources Which NET Data Provider Should You Use? ! SQL Server NET Data Provider " ! OLE DB NET Data Provider " ! SQL... or ODBC layer OLE DB NET Data Provider To use the OLE DB NET Data Provider, you need to include the System .Data. OleDb namespace in your applications Module 2: Connecting to Data Sources Data provider classes 5 ADO.NET exposes a common object model for NET data providers The following table describes the four core classes that make up a NET data provider In the SQL Server NET Data Provider, the class... manipulate data, and update the data source This lesson explains the various types of data providers, and enables you to choose the appropriate provider for your application Lesson objectives After completing this lesson, you will be able to: ! Describe the different NET data providers ! Choose a NET data provider Module 2: Connecting to Data Sources 3 What Are NET Data Providers? ! Definition " ! A NET data. .. Engine (MSDE), because MSDE is based on the SQL Server engine Module 2: Connecting to Data Sources OLE DB NET Data Provider 7 The OLE DB NET Data Provider uses native OLE DB and COM interoperability to connect to and communicate with a data source Therefore, you must use an OLE DB provider to use the OLE DB NET Data Provider To use the OLE DB NET Data Provider, you must indicate the provider type in the... Choosing a NET Data Provider ! What Are NET Data Providers? ! The NET Data Provider Classes ! Which NET Data Provider Should You Use? *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction When connecting to a data source, you must first choose a NET data provider The data provider includes classes that enable you to connect to the data source, read data efficiently,... service, or an application A data provider allows you to connect to a data source, retrieve and manipulate data, and update the data source Types of NET data providers The following NET data providers are included with the release of the Microsoft NET Framework: ! SQL Server NET Data Provider ! OLE DB NET Data Provider Other NET data providers will be made available for other data sources Microsoft will... XxxDataReader – for example, SqlDataReader ! XxxDataAdapter – for example, SqlDataAdapter ! XxxPermission – for example, SqlClientPermission *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction ADO.NET uses the NET data providers to connect to a data source, retrieve data, manipulate data, and update the data source The NET data providers are designed to. .. following provider available as a World Wide Web release download: • Open Database Connectivity (ODBC) NET Data Provider Each of these data providers includes implementations of the generic ADO.NET classes so that you can programmatically communicate with different data sources in a similar way 4 Module 2: Connecting to Data Sources The NET Data Provider Classes ! XxxConnection – for example, SqlConnection . Connection Pooling 37 Review: Connecting to Data Sources 48 Lab 2.1: Connecting to Data Sources 50 Module 2: Connecting to Data Sources Information. Monitoring SQL Server Activity xii Module 2: Connecting to Data Sources Review: Connecting to Data Sources This section provides the answers to

Ngày đăng: 24/01/2014, 10:20

Từ khóa liên quan

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

Tài liệu liên quan