.NET Data Access Architecture Guide.pdf
(
414 KB
)
Pobierz
.NET Data Access
Architecture Guide
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, place or event 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, ActiveX, Microsoft Press, Visual Basic, Visual Studio, and Windows are
either registered trademarks or trademarks of Microsoft Corporation in the United
States and/or other countries.
© 2003 Microsoft Corporation. All rights reserved.
Version 1.0
The names of actual companies and products mentioned herein may be the
trademarks of their respective owners.
Contents
.NET Data Access Architecture Guide
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Who Should Read This Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What You Must Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
What’s New . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Introducing ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
.NET Data Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Stored Procedures vs. Direct SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Properties vs. Constructor Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Managing Database Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Using Connection Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Storing Connection Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Connection Usage Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
.NET Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Generating Errors from Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Retrieving Multiple Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Retrieving a Single Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Retrieving a Single Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Connecting Through Firewalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Choosing a Network Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Handling BLOBs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Where to Store BLOB Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Performing Database Updates with DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Update Usage Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Initializing DataAdapters for Update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Managing Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Correctly Updating Null Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Using Strongly Typed DataSet Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
When to Use Strongly Typed DataSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Generating DataSet Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Working with Null Data Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Choosing a Transaction Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Using Manual Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Using Automatic Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
iv
Contents
Data Paging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Comparing the Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Using the Fill Method of SqlDataAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Using ADO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Using a Manual Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
How to Enable Object Construction for a .NET Class . . . . . . . . . . . . . . . . . . . . . . . . 62
How to Use a SqlDataAdapter To Retrieve Multiple Rows . . . . . . . . . . . . . . . . . . . . 64
How to Use a SqlDataReader to Retrieve Multiple Rows . . . . . . . . . . . . . . . . . . . . . 64
How to Use an XmlReader to Retrieve Multiple Rows . . . . . . . . . . . . . . . . . . . . . . . 66
How to Use Stored Procedure Output Parameters to Retrieve a Single Row . . . . . . . 67
How to Use a SqlDataReader to Retrieve a Single Row . . . . . . . . . . . . . . . . . . . . . . 68
How to Use ExecuteScalar to Retrieve a Single Item . . . . . . . . . . . . . . . . . . . . . . . . 69
How to Use a Stored Procedure Output or Return Parameter
to Retrieve a Single Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
How to Use a SqlDataReader to Retrieve a Single Item . . . . . . . . . . . . . . . . . . . . . . 72
How to Code ADO.NET Manual Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
How to Perform Transactions with Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
How to Code a Transactional .NET Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Collaborators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
.NET Data Access
Architecture Guide
Introduction
If you are designing a data access layer for a .NET-based application, you should use
Microsoft® ADO.NET as the data access model. ADO.NET is feature rich and
supports the data access requirements of loosely coupled, multitier Web applications
and Web services. As with other feature-rich object models, ADO.NET offers a
number of ways to solve a particular problem.
The .NET Data Access Architecture Guide provides information to help you choose
the most appropriate data access approach. It does this by describing a wide range
of common data access scenarios, providing performance tips, and prescribing best
practices. This guide also provides answers to frequently asked questions, such as:
Where is the best place to store database connection strings? How should I
implement connection pooling? How should I work with transactions? How should
I implement paging to allow users to scroll through large numbers of records?
This guide focuses on the use of ADO.NET to access Microsoft SQL Server™ 2000 by
using the SQL Server .NET data provider, one of the two providers shipped with
ADO.NET. Where appropriate, this guide highlights any differences that you need
to be aware of when you use the OLE DB .NET data provider to access other OLE
DB–aware data sources.
For a concrete implementation of a data access component developed using the
guidelines and best practices discussed in this document, see the Data Access
Application Block. The Data Access Application Block includes the source code for
the implementation, and you can use that code directly in your .NET-based
applications.
The .NET Data Access Architecture Guide includes the following sections:
Introducing ADO.NET
Managing Database Connections
Error Handling
Performance
Connecting Through Firewalls
Handling BLOBs
Plik z chomika:
tombula
Inne pliki z tego folderu:
Improving .NET Application Performance and Scalability.pdf
(8148 KB)
Andrew Troelsen - COM and .NET Interoperability.pdf
(6514 KB)
Operating .NET-based Applications.pdf
(2888 KB)
The Spring.NET Framework Reference Manual.pdf
(3822 KB)
.NET Data Access Architecture Guide.pdf
(414 KB)
Inne foldery tego chomika:
Ada
Agile
AI - Robotyka
AJAX
Android
Zgłoś jeśli
naruszono regulamin