database programming with c# - springer978-1-4302-1098-6/1.pdf · including assembler, rpg, visual...

15
Database Programming with C# CARSTEN THOMSEN APress Media, LLC

Upload: lethu

Post on 09-May-2019

252 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Database Programming with C#

CARSTEN THOMSEN

APress Media, LLC

Page 2: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Database Programming with C#

All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.

ISBN 978-1-59059-010-2 ISBN 978-1-4302-1098-6 (eBook) DOI 10.1007/978-1-4302-1098-6

Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

Technical Reviewer: Douglas Milnes

Editorial Directors: Dan Appleman, Peter Blackburn, Gary Cornell, Jason Gilmore, Karen Watterson, John Zukowski

Managing Editor: Grace Wong

Copy Editors: Nicole LeClerc, Ami Knox

Production Editor: Tory McLearn

Compositor: Impressions Book and Journal Services, Inc.

Indexer: Valerie Haynes Perry

Cover Designer: Tom Debolski

Marketing Manager: Stephanie Rodriguez

In the United States, phone 1-800-SPRINGER, email orders@springer-ny. com, or visit http://www.springer-ny.com.

Outside the United States, fax +49 6221 345229, email orders@springer. de, or visit http://www.springer.de.

For information on translations, please contact Apress directly at 2560 9th Street, Suite 219, Berkeley, CA 94710.

Email info@apress. com or visit http: I /www. apress. com.

The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.

Copyright © 2002 by Carsten ThomsenOriginally published by Apress in 2002

Page 3: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Contents at a Glance

Foreword .............................................................. . ix

About the Author ..................................................... . xi

About the Technical Reviewer .................................... .. xiii

Acknowledgments ..................................................... . xv

Introduction ........................................................ . xvii

Part One

Chapter 1

Part Two

Chapter 2

Chapter 3A

Chapter 3B

Chapter 4

Chapter 5

Chapter 6

Chapter 7

Chapter 8

Chapter 9

Chapter 10

Getting Started ................................. . 1

A Lightning-Quick Introduction to C# ........... . 3

Database Programming ......................... . 29

Using Databases ................................... . 31

Presenting ADO.NET: The Connected Layer ....... 55

Presenting ADO.NET: The Disconnected Layer ... 193

Presenting the IDE from a Database Viewpoint 281

Exception Handling .............................. . 343

Using Stored Procedures, Views, and Triggers 367

Hierarchical Databases ......................... . 413

Message Queues ................................... . 449

Data Wrappers .................................... . 505

Data-Bound Controls ............................. . 525

iii

Page 4: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Contents at a Glance

Part Three Example Applications ........................ . 563

Chapter 11 UserMan ........................................... . 565

Appendix A Using XML with SQL Server 2000 ................ . 587

Index ................................................................ . 637

iv

Page 5: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Contents

Foreword ............................................................ . ix

About the Author ................................................ . xi

About the Technical Reviewer .............................. . xiii

Acknowledgments ................................................. . xv

Introduction .................................................... . xvii

Part One Getting Started .............................. . I

Chapter 1 A Lightning-Quick Introduction to C# .. . 3

Reviewing Programming Concepts .................................... . 4 Getting Cozy with the VS .NET Integrated

Development Environment ....................................... . 14

Summary .............................................................. . 28

Part Two Database Programming .................... .. 29

Chapter 2 Using Databases ................................ . 31

What Is a Database? ............................................... . 31

Why Use a Database? ............................................... . 32

Relational vs. Hierarchical ..................................... .. 33

What C# Programmers Need to Know About Relational Databases .......................................... . 35

UserMan Database Schema ........................................... . 51

Summary .............................................................. . 53

v

Page 6: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Contents

Chapter 3A Presenting ADO.NET: The Connected Layer ......................... . 55

Data-Related Namespaces ........................................... . 58 Data Providers and Drivers ....................................... . 59 Using Command Objects ............................................ . 123 Using the DataReader Class ...................................... . 146 The DataAdapter Explained ....................................... . 170 Using the CommandBuilder Class ................................. . 188 Summary ............................................................. . 191

Chapter 38 Presenting ADO.NET: The Disconnected Layer ................... . 193

Using the DataSet Class .......................................... . 193

Using the DataTable Class ....................................... . 218 Using the DataView Class ........................................ . 238

Using the DataRow Class .......................................... . 245 Using the DataColumn Class ...................................... . 249 Using the DataRelation Class .................................... . 252 Looking at Cursors ................................................ . 261 Examining Data Source Locking ................................... . 265 Using "Classic" ADO and COM Interop ........................... . 276 Summary ............................................................. . 279

Chapter 4 Presenting the IDE from a Database Viewpoint ................ . 281

Using the Server Explorer ....................................... . 281 Looking at Database Projects .................................... . 296 Designing Databases with Visio for Enterprise Architect .... . 305 Designing Databases with the Database Designer .............. . 306 Using the Table Designer ........................................ . 314 Designing Queries with the Query Designer .................... . 321 Script Editing Using the SQL Editor ........................... . 334 Creating Typed Data Sets ........................................ . 337 Summary ............................................................. . 341

vi

Page 7: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Chapter 5 Exception Handling 0 0 0 0 0 0 0 0 0 .. 0 0 ........ 0 0 0 0 0 0343

Enabling Exception Handling 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o345 Using Two or More Exception Handlers

in One Procedure 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o345 Examining the Exception Class 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0346 Handling Exceptions in the Exception Handler 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0348 Filtering Exceptions 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0354 Creating Your Own Exception 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o356 Throwing an Exception 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0356 Handling Data-Related Exceptions 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0357 CLR Handling of Exceptions 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0359 Using the Debug Class 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .359 Summary 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0366

Chapter 6 Using Stored Procedures, Views, and Triggers 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0367

Optimization Issues 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0368 Using Stored Procedures 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .373 Using Views 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0397 Using Triggers 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .405 Summary 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 o412

Chapter 7 Hierarchical Databases 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.413

Looking at LDAP 0 0 0 0 0 o o o 0 0 o o 0 0 o 0 0 0 0 0 o 0 o 0 0 0 0 0 0 0 0 o o 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .413 Accessing Active Directory Programmatically 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .415 Accessing Active Directory Using

the OLE DB . NET Data Provider 0 0 0 0 0 0 0 0 0 0 0 o 0 0 0 0 0 0 0 0 0 0 0 o o 0 0 0 0 0 0 0429 Accessing Microsoft Exchange Server 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .436 Summary 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0448

Chapter 8 Message Queues 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.449

Connectionless Programming 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .450 Taking a Quick Look at the MessageQueue Class 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .450 When to Use a Message Queue 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0451 How to Use a Message Queue 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .453

Contents

vii

Page 8: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Contents

Making Message Queues Transactional ........................... .480

Looking at System-Generated Queues ............................. .485

Securing Your Message Queuing ................................... .490

Summary ............................................................. . 504

Chapter 9 Data Wrappers .................................. . 505

Why Use Data Wrappers? ........................................... . 505

Looking at Object Oriented Programming ........................ . 506

Wrapping a Database .............................................. . 513

Summary ............................................................. . 524

Chapter 10 Data- Bound Controls ........................ . 525

Data-Bound Controls vs. Manual Data Hooking .................. . 525

Different Controls for Different Uis .......................... . 526

Summary ............................................................. . 561

Part Three Example Applications ................ . 563

Chapter 11 UserMan ......................................... . 565

Identifying the UserMan Information ........................... . 565

Discovering the Objects .......................................... . 566

Creating the Client .............................................. . 582

Tips and Ideas .................................................... . 583

Summary ............................................................. . 586

Appendix A Using XML with SQL Server 2000 ........ . 587

Using SQLXML 2.0 .................................................. . 591

Summary ............................................................. . 635

Index ............................................................... . 637

viii

Page 9: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Foreword

No MATTER WHAT APPLICATION you're building, there's no escape from dealing with data and, subsequently, with databases. While new technologies can make our lives easier, they're built with one thing in mind: to keep data at our fingertips.

The same goes for .NET, the new generation of Microsoft technology that promises to make data interchange easier and more flexible, even across dis­parate systems .. NET comes with a whole set of data-handling tools that will give developers more choices when they build applications.

Carsten's book is a wonderful introduction to all these choices, some of which even I did not give a second thought to before reading the book. Because problems in real-life applications demand different solutions, it is important for every developer to understand these different solutions. Not only will this make developers more productive, but it will also make them more marketable.

Moreover, I believe, by covering topics such as hierarchical databases, Message Queuing, and SQLXML, Carsten shows his expertise in the area of data handling within the .NET Framework, which goes beyond the ADO.NET coverage you see in most other books. Being a Microsoft MVP, Carsten often interacts with people to solve their day-to-day problems, and the same conduct continues in his book. So, when I was asked to review this book and write the foreword, I was more than happy to oblige.

I hope this book opens your mind to the new possibilities within .NET. I'm sure you'll appreciate Carsten's efforts to make learning data access with .NET easy.

Manohar Kamath Microsoft MVP and .netBooks Editor, (http: I /www. dot net books. com) March2002

ix

Page 10: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

About the Author

Carsten Thomsen is a Microsoft MVP, recognition he received in August 1999. He lives in his native Denmark and currently holds the MCSE and MCSD certifications. Carsten has been programming in Visual Basic for almost 10 years, and he specializes in data access. With the advent ofVB .NET and, more important, VS .NET, Carsten now focuses on data access using C# and VB .NET.

Carsten and his girlfriend Mia live in Esbjerg, Denmark. They have a 2-year­old daughter, Caroline, and are currently expecting a second child. Nicole, 7, is Carsten's oldest daughter and she lives with her mother in Dublin, Ireland. Carsten enjoys traveling to Dublin several times a year to spend time with Nicole.

xi

Page 11: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

About the Technical Reviewer

Douglas Milnes is a freelance computer consultant who has been running his own consultancy and application develop­ment business since 1984. He lives in the heart of the United Kingdom's "Silicon Valley" near Reading, Berkshire, England. He has consulted for major companies such as IBM and Oracle and is a lead consultant with both DeeSoft (http: I /www. dee soft. net) and Boost Data

(http: I lwww. boost. net/Douglas). Douglas started with computing at the beginning of the microcomputer

boom and opened a pioneering retail business in the days before the IBM PC was launched. After disposing of that business, he worked for a range of com­panies implementing PC and UNIX systems. His specialty was accounting applications, which led him into stock control applications and his first "real" database exposure.

Douglas owns up to a wide experience in the computing world, admitting to being one of those programmers who carries a screwdriver with him. He has worked with DGE, DEC, IBM, and ICL minis and mainframes, but he's spent most of the last few years around PCs, linking to larger machines where necessary. Like any programmer who's been around for a time, he works on desktop computers these days that vastly outpower the multiuser minicomputers he learned about databases on.

Douglas has extensive experience with several programming languages, including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up with the times and being at the leading edge of technology, he has been involved with .NET since the beta pre-releases were available.

With his two children, Daniel, 12, and Rebecca, 15, and his beautiful wife, Douglas counts his blessings and enjoys a varied life. Douglas can be contacted by e-mail at Douglas@DeeSoft. net.

xiii

Page 12: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Acknowledgments

This book, which is based on my first book Database Programming with VB .NET,

was somewhat easier for me to write than the first one. Having been through all the processes once before, I knew what to expect and how to plan accordingly. Obviously, a lot of the material in this book is taken from the first book, but a number of changes have been made, especially to correct the typos and more grave errors, and I've managed to add a lot more content to the book.

I couldn't have written this without Technical Reviewer Douglas Milnes, who proved to be extremely valuable for content of this book. Douglas found some of the mistakes that I made, but he also brought a number of excellent ideas to my attention, on how to arrange the book and on "sticking in" extra or just missing sections. My editor this time around was Peter Blackburn. I can't really say that we started off on the right foot (which was also the case with Douglas) but we sure ash*** made up for it along the way. Peter has an "annoying" habit of con­tacting me day and night on MSN Messenger, so we were always in close contact about the book, the whole process, and just about everything else. What I am getting at here, is that it truly has been an excellent partnership, not just with Peter and Douglas, but also with the rest of the crew: Grace, Nicole, Alexa, Ami, and Tory.

Carsten,[email protected] Esbjerg, Denmark March2002

XV

Page 13: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Introduction

THIS BOOK IS ALL ABOUT ACCESSING DATABASES of variOUS kinds, SUCh as Active Directory, SQL Server 2000, Exchange Server 2000, and Message Queuing. My intention with this book is to give you insight into how ADO.NET works and how you can use the classes from ADO.NET to access a database; use stored procedures, views, and triggers; get information from Active Directory and Exchange Server; and use Message Queuing in your applications. Having said that, my goal was to make the book as easy to read as possible, and although there are passages that aren't quite as easy to read as I'd like them to be, I think I've managed to accomplish what I set out to do.

Who This Book Is For

This book is targeted at intermediate users, meaning users who already know a little about Visual Studio .NET and perhaps previous versions ofVisual Basic. Basic knowledge of Object Oriented Programming (OOP), ADO, and database design is also assumed. Parts of the book are at a beginner level and other parts are at a more advanced level. The beginner-level material appears where I feel it is appropriate to make sure you really understand what is being explained. The many listings and tables make this a good reference book, but it's also a book intended for reading cover to cover. It will take you through all the data access aspects ofVisual Studio .NET with example code inC#. This includes how to cre­ate the various database items, such as databases, tables, constraints, database projects, stored procedures, views, triggers and so on. For the most part, I show you how to do this from within the VS .NET IDE as well as programmatically where possible. You'll build on the same example code from the beginning of the book until you wrap it up in the very last chapter. The example application, UserMan, is a complete user management system that includes SQL Server, Active Directory, and Message Queuing.

xvii

Page 14: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Introduction

xviii

How This Book Is Organized

This book is organized in four parts:

Part One is a general introduction to Visual Studio .NET and the .NET Framework.

Part Two is the juicy part, where you take a look at how to connect to rela­tional and hierarchical databases. You will also learn how to wrap your database access in classes and how to master exception handling. Part Two starts with a look at how to design a relational database, and you gradually learn the building blocks for the User Man example application.

Part Three is where you finish the User Man example application.

Appendix A covers how to use the SQLXML plug-in for manipulating SQL Server 2000 data using XML from managed code or using HTTP.

Technology Requirements

From the example code you can connect to SQL Server using any of these three .NET Data Providers: SQL Server .NET Data Provider, OLE DB .NET Data Provider, or ODBC .NET Data Provider. The example code also includes how to connect to and manipulate data in MySQL 3.23.45 or later, Oracle 8i or later, Microsoft Access 2000 or later, and SQL Server. For connecting to SQL Server 2000, you can find coverage of the SQLXML plug-in for manipulating SQL Server data using XML from managed code or using HTTP. Exchange Server 2000 connection and data manipulation is also covered.

Because I'm using some ofthe Enterprise functionality ofVisual Studio .NET, you'll need either of the two Enterprise editions to follow all the exercises. How­ever, the Professional edition will do for most of the example code, and it will certainly do if you just want to see how everything is done while you learn ADO.NET. This means that the only thing extra you get from the Enterprise editions in terms of database access is an extra set of database tools to be used from within the IDE.

Page 15: Database Programming with C# - Springer978-1-4302-1098-6/1.pdf · including Assembler, RPG, Visual Basic and, of course, C#. Tending to keep up Tending to keep up with the times and

Example Code

All the example code for this book can be found on the Apress Web site (http: I lwww. a press. com) in the Downloads section, or on the User Man Web site (http: I lwww. userman. dk).

Data Source

The data source for the example code in this book is running on SQL Server 2000. However, the example code also includes how to connect to and manipulate data in MySQL 3.23.45 or later, Oracle 8i or later, Microsoft Access 2000 or later, and SQL Server.

Feedback

I can be reached at carstent@dotnetservices. biz and I'll gladly answer any e-mail concerning this book. Now, I don't need any unnecessary grief, but I'll try to respond to any queries you might have regarding this book.

I have set up a Web site for the User Man example application, where you can post and retrieve ideas on how to take it further. The Web site address is http: I lwww. userman. dk. Please check it out.

Introduction

xix