Tài liệu ASP.NET DATABASE PROGRAMMING - Pdf 85

WEEKENDCRASHCOURSE
WEEKENDCRASHCOURSE

BUTLER
CAUDILL
ASP.NET
DATABASE PROGRAMMING
JASON
BUTLER
TONY
CAUDILL
CD-ROM
with assessment
software, sample
code, and more
30 Sessions That
Will Have You
Creating Dynamic,
Data-Driven Web
Applications in
Only 15 Hours
ASP.NET DATABASE PROGRAMMING
HOUR
15
15
he big day is Monday. The day you get to show off what you
know about ASP.NET database programming. The problem is, you’re not
really up to speed. Maybe it’s been a while since you worked with
Active Server Pages. Or maybe you’re new to Microsoft .NET or database
programming. In any event, we’ve got a solution for you — ASP.NET
Database Programming Weekend Crash Course. Open the book Friday

128 MB RAM. See the About the
CD Appendix for details and
complete system requirements.
Category:
Programming/Web Development
WEEKEND CRASH COURSE
WEEKEND CRASH COURSE
T

ISBN 0-7645-4830-1
,!7IA7G4-feidah!:P;m;o;t;T
FRIDAY
Evening: 4 Sessions, 2 Hours
• Introducing ASP.NET
• Setting Up .NET
• Designing a Database
• SQL, A Primer
SATURDAY
Morning: 6 Sessions, 3 Hours
• Databases, A Primer on
SQL Server 2000
• XML, A Primer
• Developing ASP.NET Pages
• Using HTML Controls
• Using ASP.NET Controls
• User Controls
SATURDAY, continued
Afternoon: 6 Sessions, 3 Hours
• Validating User Input
• Maintaining State in

ASP.NET
• Migration From ADO to
ADO.Net
WEEKEND
CRASH
COURSE
HOURS
*85555-AIABIi
For more information on
Hungry Minds, go to
www.hungryminds.com
$24.99 US
$37.99 CAN
£19.99 UK incl. VAT
4830-1 cover 10/25/01 2:13 PM Page 1
ASP.NET
Database Programming
Weekend Crash Course

Jason Butler and Tony Caudill
Cleveland, OH • Indianapolis, IN • New York, NY
014830-1 FM.F 11/7/01 9:00 AM Page i
ASP.NET Database Programming Weekend
Crash Course™
Published by
Hungry Minds, Inc.
909 Third Avenue
New York, NY 10022
www.hungryminds.com
Copyright © 2002 Hungry Minds, Inc. All rights

Distributor, Inc. for Micronesia; by Chips
Computadoras S.A. de C.V. for Mexico; by Editorial
Norma de Panama S.A. for Panama; by American
Bookshops for Finland.
For general information on Hungry Minds’ products
and services please contact our Customer Care
department within the U.S. at 800-762-2974, out-
side the U.S. at 317-572-3993 or fax 317-572-4002.
For sales inquiries and reseller information, includ-
ing discounts, premium and bulk quantity sales,
and foreign-language translations, please contact
our Customer Care department at 800-434-3422, fax
317-572-4002 or write to Hungry Minds, Inc., Attn:
Customer Care Department, 10475 Crosspoint
Boulevard, Indianapolis, IN 46256.
For information on licensing foreign or domestic
rights, please contact our Sub-Rights Customer Care
department at 212-884-5000.
For information on using Hungry Minds’ products
and services in the classroom or for ordering exam-
ination copies, please contact our Educational Sales
department at 800-434-2086 or fax 317-572-4005.
For press review copies, author interviews, or other
publicity information, please contact our Public
Relations department at 317-572-3168 or fax
317-572-4168.
For authorization to photocopy items for corporate,
personal, or educational use, please contact
Copyright Clearance Center, 222 Rosewood Drive,
Danvers, MA 01923, or fax 978-750-4470.

me with tremendous support and happiness throughout my life. To my mother,
Marian, for inspiring me to be that best person that I possibly can. To Donna,
without whose support, encouragement, and patience this book would never
have been completed. To my father, Al, for always providing much needed
advice and support. And to my stepfather, Steve, for being who you didn't have
to be. I would also like to send my prayers to all of the families impacted by
the tragic events of September 11, 2001.
- Jason
I would like to dedicate this book to my family and friends, who have
supported me and given me tremendous joy over the past year: Marie and
Bradley Caudill. I also would like to offer my prayers for the families impacted
by the tremendous tragedies of September 11 and my support for the
policemen, firefighters, and communities of New York and Washington. And to
the US military leadership, such as my father, Sy, and his wife Sue, who like so
many soldiers before them will face a long, challenging, and difficult road to
building a foundation for us all in our search for peace.
-Tony
014830-1 FM.F 11/7/01 9:00 AM Page iii
Acquisitions Editor
Sharon Cox
Project Editor
Sharon Nash
Development Editor
Michael Koch
Technical Editors
Todd Meister
Peter MacIntyre
Copy Editor
Maarten Reilingh
Editorial Assistant

15 hours stretching from Friday afternoon to Sunday evening. At the end of each sec-
tion of the book, you’ll get a chance to test your knowledge before continuing. Good luck!
Who Should Read This Book
This book is for people who want to learn to write ASP.NET applications in order to access
and manipulate data in a Web environment. This book assumes that you have a basic
understanding of Web development, including experience with Visual Basic or Visual Basic
Scripting in developing ASP-based applications. The book’s focus is on ASP.NET and ADO.NET
as a suite of components used in data-driven Web application development. It includes a
CD-ROM with ASP.NET editor software and sample code mentioned in the text.
Organization and Presentation
We’ve organized the book into 30 sessions, each requiring approximately 30 minutes. We
divide the sessions as follows:
¼
Friday evening. Sessions 1 through 4. Reading time: 2 hours.
¼
Saturday morning. Sessions 5 through 10. Reading time: 3 hours
¼
Saturday afternoon. Sessions 11 through 16. Reading time: 3 hours.
¼
Saturday evening. Sessions 17 through 20. Reading time: 2 hours.
¼
Sunday morning. Sessions 21 through 26. Reading time: 3 hours.
¼
Sunday afternoon. Sessions 27 through 30. Reading time: 2 hours.
At the end of each session, we present questions designed to check your progress.
The text is sprinkled with icons designed to catch your attention.
Preface
014830-1 FM.F 11/7/01 9:00 AM Page v
Forewordvi
The “minutes to go” icons mark your progress in the session.

Tony and Jason: We would both like to thank PricewaterhouseCoopers LLP and Hungry
Minds for providing the financial and motivational support to accomplish this task while
supporting an extensive consulting practice.
Health and Peace . . .
CD-ROM
Note
Tip
014830-1 FM.F 11/7/01 9:00 AM Page vi
Contents at a Glance
Preface....................................................................................................................................v
Introduction ........................................................................................................................xvii
FRIDAY
......................................................................................................................
2
Part I—Friday Evening
..................................................................................................
4
Session 1–Introducing ASP.NET ..................................................................................................5
Session 2–Setting Up .NET ......................................................................................................13
Session 3–Designing a Database ...............................................................................................21
Session 4–Building a Database .................................................................................................29
SATURDAY
.................................................................................................................
44
Part II—Saturday Morning .....................................................................................................46
Session 5–Using SQL: A Primer .................................................................................................47
Session 6–XML: A Primer .........................................................................................................55
Session 7–Developing ASP.NET Pages ........................................................................................61
Session 8–Using HTML Controls ................................................................................................69
Session 9–Using Web Controls ..................................................................................................79

Appendix C–ADO.NET Class Descriptions ................................................................................319
Appendix D–Coding Differences in ASP and ASP.NET ...............................................................355
Index ..................................................................................................................................369
Hungry Minds, Inc. End User License Agreement .....................................................................387
014830-1 FM.F 11/7/01 9:00 AM Page vii
014830-1 FM.F 11/7/01 9:00 AM Page viii
Contents
Preface .....................................................................................................................v
Introduction..........................................................................................................xvii
FRIDAY
...................................................................................................................
2
Part I—Friday Evening
.......................................................................................
4
Session 1–Introducing ASP.NET ..................................................................................5
Internet Standards
.............................................................................................
5
The Evolution of ASP
..........................................................................................
6
The Benefits of ASP.NET
......................................................................................
8
What Is .NET?
....................................................................................................
9
.NET Experiences ........................................................................................................9
.NET Clients .............................................................................................................10

29
Creating SQL Server Tables
.................................................................................
32
Creating a View
................................................................................................
37
Creating a Stored Procedure
...............................................................................
37
Creating a Trigger
.............................................................................................
38
SATURDAY
...........................................................................................................
44
Part II—Saturday Morning
..............................................................................
46
Session 5–Using SQL: A Primer ................................................................................47
INSERT Statements
...........................................................................................
47
DELETE Statements
...........................................................................................
49
UPDATE Statements
..........................................................................................
50
SELECT Statements

Session 8–Using HTML Controls ...............................................................................69
Introducing HTML Controls
.................................................................................
70
Using HTML controls .................................................................................................70
How HTML controls work ...........................................................................................74
Intrinsic HTML controls .............................................................................................74
HTML Control Events
.........................................................................................
74
The Page_OnLoad event .............................................................................................75
Custom event handlers ..............................................................................................76
Session 9–Using Web Controls .................................................................................79
Intrinsic Controls
.............................................................................................
80
Using intrinsic controls .............................................................................................80
Handling intrinsic Web control events .........................................................................83
List Controls
....................................................................................................
84
Rich Controls
...................................................................................................
85
Session 10–Introducing User Controls ......................................................................89
Creating a User Control
......................................................................................
90
Adding User Control Properties
...........................................................................

115
Advantages ........................................................................................................115
Disadvantages ....................................................................................................115
How to make it happen ........................................................................................115
Out of Process State Management
......................................................................
118
Session Management with SQL Server ........................................................................118
Advantages ........................................................................................................118
Disadvantages ....................................................................................................118
How to make it happen ........................................................................................119
Session Management with ASP.NET State Server ..........................................................120
Advantages ........................................................................................................120
Disadvantages ....................................................................................................120
How to make it happen ........................................................................................121
Session 13–Authentication and Authorization .........................................................123
Introducing the Key Security Mechanisms
..........................................................
123
Web.config and Security
...................................................................................
124
Special identities ....................................................................................................127
Using request types to limit access ...........................................................................128
New Tricks for Forms-based Authentication
.........................................................
128
014830-1 FM.F 11/7/01 9:00 AM Page x
Using the Passport Authentication Provider
........................................................

...............................................................
150
Transmission formats ..............................................................................................150
Connected versus disconnected datasets ....................................................................151
COM marshaling versus text-based data transmission ...................................................151
Variant versus strongly typed data ............................................................................151
Data schema ...........................................................................................................152
ADO.NET Managed Provider Versus SQL Managed Provider
......................................
152
Why ADO.NET?
...............................................................................................
152
Session 16–Navigating the ADO.NET Object Model ...................................................155
Part IV—Saturday Evening ...............................................................168
Session 17–Opening a Connection ..........................................................................169
Creating a Connection
.....................................................................................
170
Opening a Connection
......................................................................................
171
Using Transactions
..........................................................................................
174
Session 18–Executing Commands ...........................................................................177
Building a Command
.......................................................................................
178
Connection property ...............................................................................................179

GetOrdinal method ..................................................................................................194
GetName method ....................................................................................................194
Close method .........................................................................................................194
Session 20–Introducing DataSets, Part I .................................................................197
Constructing a DataAdapter Object
.....................................................................
199
SelectCommand property .........................................................................................200
UpdateCommand, DeleteCommand, and InsertCommand properties ...............................201
Fill method ............................................................................................................201
Update method .......................................................................................................203
Dispose method ......................................................................................................203
Using DataSet Objects
......................................................................................
203
DataSetName property .............................................................................................203
CaseSensitive property ............................................................................................204
SUNDAY
.............................................................................................................
208
Part V—Sunday Morning
...............................................................................
210
Session 21–Introducing DataSets, Part II ................................................................211
Constructing a DataSet
.....................................................................................
211
Tables property .......................................................................................................212
TablesCollection Object
....................................................................................

226
Binding to XML
..............................................................................................
227
TreeView Control
............................................................................................
228
Implement the TreeView server control ......................................................................229
Session 23–Using the DataGrid Control with Bound Data ..........................................233
DataGrid Control Basics
....................................................................................
233
Binding a set of data to a DataGrid control ................................................................233
Formatting the output of a DataGrid control ..............................................................235
Master/Detail Relationships with the DataGrid Control
..........................................
236
Populating the Master control ..................................................................................238
Filtering the detail listing ........................................................................................239
Session 24–Beating the CRUD out of the DataGrid Control .......................................243
Updating Your Data
.........................................................................................
243
014830-1 FM.F 11/7/01 9:00 AM Page xii
Handling the OnEditCommand Event
..................................................................
246
Handling the OnCancelCommand Event
...............................................................
247

267
OLEDBError Object Methods
..............................................................................
267
OLEDBException Properties
...............................................................................
268
Writing Errors to the Event Log
.........................................................................
269
Part VI—Sunday Afternoon
...........................................................................
276
Session 27–SOAP It Up! ........................................................................................277
Introducing SOAP
...........................................................................................
277
Accessing Remote Data with SOAP
.....................................................................
278
SOAP Discovery (DISCO)
...................................................................................
281
Web Service Description Language (WSDL)
..........................................................
281
Using SOAP with ASP.NET
.................................................................................
282
Session 28–Web Services ......................................................................................283

........................................................................
300
Running ADO under ASP.NET
............................................................................
302
Early Binding ADO COM Objects in ASP.NET
..........................................................
304
Appendix A–Answers to Part Reviews .....................................................................309
Friday Evening Review Answers
........................................................................
309
Saturday Morning Review Answers
.....................................................................
310
Saturday Afternoon Review Answers
..................................................................
311
Saturday Evening Review Answers
.....................................................................
312
Sunday Morning Review Answers
.......................................................................
312
Sunday Afternoon Review Answers
....................................................................
313
Contents xiii
014830-1 FM.F 11/7/01 9:00 AM Page xiii
Contentsxiv

How you did it with VBScript in ASP .........................................................................356
How you do it with VB .NET in ASP.NET .....................................................................357
How you do it with C# in ASP.NET .............................................................................358
Variable Declarations
.......................................................................................
358
How you did it with VBScript in ASP .........................................................................358
How you do it with VB .NET in ASP.NET .....................................................................359
How you do it with C# in ASP.NET .............................................................................359
Statements
....................................................................................................
359
How you did it with VBScript in ASP .........................................................................359
How you do it with VB .NET in ASP.NET .....................................................................359
How you do it with C# in ASP.NET .............................................................................359
Comments
.....................................................................................................
359
How you did it with VBScript in ASP .........................................................................359
How you do it with VB .NET in ASP.NET .....................................................................360
How you do it with C# in ASP.NET .............................................................................360
Indexed Property Access
..................................................................................
360
How you did it with VBScript in ASP .........................................................................360
How you do it with VB .NET in ASP.NET .....................................................................360
How you do it with C# in ASP.NET .............................................................................360
Using Arrays
..................................................................................................
360

How you did it with VBScript in ASP .........................................................................364
How you do it with VB .NET in ASP.NET .....................................................................364
How you do it with C# in ASP.NET .............................................................................365
String Concatenation
.......................................................................................
365
How you did it with VBScript in ASP .........................................................................365
How you do it with VB .NET in ASP.NET .....................................................................365
How you do it with C# in ASP.NET .............................................................................365
Error Handling
...............................................................................................
365
How you did it with VBScript in ASP .........................................................................365
How you do it with VB .NET in ASP.NET .....................................................................366
How you do it with C# in ASP.NET .............................................................................366
Conversion of Variable Types
............................................................................
366
How you did it with VBScript in ASP .........................................................................366
How you do it with VB .NET in ASP.NET .....................................................................366
How you do it with C# in ASP.NET .............................................................................366
Index....................................................................................................................369
Hungry Minds, Inc. End User License Agreement.......................................................387
Contents xv
014830-1 FM.F 11/7/01 9:00 AM Page xv
014830-1 FM.F 11/7/01 9:00 AM Page xvi
W
ith the release of the .NET Framework, Microsoft is taking the most significant risk
in its history. Microsoft has spent billions of dollars, representing over 80 percent of
its R&D budget, on designing and constructing this fundamental shift in its develop-

order to provide full cross-platform portability. While at the time of this writing there were
no major announcements in this area, it is anticipated that ports to Linux and other operat-
ing systems is a key way Microsoft will be able to recoup its investment. In fact, Microsoft
has a migration kit, the Java User Migration Path or JUMP, which contains a set of tools
that will enable Java developers to take advantage of the .NET platform. The stated goal of
these tools is to provide a path for Visual J++ and other Java developers to preserve their
existing Java language projects and migrate those projects to the .NET platform. Once you
begin experimenting with C# you will clearly see how realistic it is for this type of approach
to affect the Java community.
The Microsoft .NET Architecture
The Microsoft .NET Architecture is split into three essential areas:
¼
The .NET platform, which includes the .NET infrastructure and tools to build and
operate a new generation of Web services and applications. The core of the .NET
platform is the .NET Framework, which includes C#, VB .NET, ASP.NET, and ADO.NET.
¼
.NET products and services, which include Microsoft Windows, MSN.NET, personal
subscription services, Microsoft Office .NET, Microsoft Visual Studio .NET, and
Microsoft bCentral for .NET.
¼
Third-party .NET services, which are services created by a vast range of partners
and developers who now have the opportunity to produce corporate and vertical
services built on the .NET platform.
The .NET platform contains all of the building blocks for creating .NET products and ser-
vices and integrating third-party .NET solutions. Microsoft is using components of the .NET
platform to extend the platform itself and to build additional .NET products. For example, as
a developer you will be very impressed or possibly amazed that the entire ASP.NET platform
is actually built on C#, which is a new .NET language! Additionally, large portions of the
Visual Studio .NET code base are built on a combination of C++, C#, and VB .NET.
One of the most common themes heard throughout the development community concerns

you? Web services enable you to outsource the generic portions of application development
that today are commonly developed over and over each time a new application is built. Some
people have compared it to building with Legos. From a relatively generic set of components,
in a very short period you can build a complex, robust product that is great fun to use!
We hope this brief introduction provides you a foundation for jumping into our book on
ASP.NET and gives you a perspective on how it is but one small yet important component of
the overall .NET solution.
Introduction xix
014830-1 FM.F 11/7/01 9:00 AM Page xix
014830-1 FM.F 11/7/01 9:00 AM Page xx
ASP.NET
Database Programming
Weekend Crash Course

014830-1 FM.F 11/7/01 9:00 AM Page 1
024830-1 DPO1.F 11/7/01 9:00 AM Page 2
Part I — Friday Evening
Session 1
Introducing ASP.NET
Session 2
Setting Up .NET
Session 3
Designing a Database
Session 4
Building a Database
024830-1 DPO1.F 11/7/01 9:01 AM Page 3
PART
Friday
Evening
I


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status