ebook img

Database Programming with C# PDF

521 Pages·2002·4.24 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Database Programming with C#

Database Programming with C# Database Programming with C# Table of Contents Database Programming with C#......................................................................................................................1 Introduction........................................................................................................................................................4 Who This Book Is For............................................................................................................................4 How This Book Is Organized.................................................................................................................4 Technology Requirements......................................................................................................................4 Example Code.........................................................................................................................................5 Data Source.............................................................................................................................................5 Feedback.................................................................................................................................................5 Chapter 1: A Lightning−Quick Introduction to C#........................................................................................6 Overview.................................................................................................................................................6 Reviewing Programming Concepts........................................................................................................6 A Quick Look at Components of the .NET Framework..................................................................7 JIT: Another Word for Virtual Machine?.......................................................................................10 Assemblies and Namespaces..........................................................................................................10 The .NET Framework Class Library..............................................................................................13 Getting Cozy with the VS .NET Integrated Development Environment..............................................13 All Languages Share the IDE.........................................................................................................14 Two Interface Modes......................................................................................................................14 Built−in Web Browser Functionality.............................................................................................15 Command Window.........................................................................................................................15 Built−in Object Browser................................................................................................................16 Integrated Debugger.......................................................................................................................17 Integrated Help System..................................................................................................................17 Macros............................................................................................................................................18 Upgraded Deployment Tools.........................................................................................................19 Text Editors....................................................................................................................................19 IDE and Tools Modification...........................................................................................................20 Server Explorer...............................................................................................................................20 Data Connections...........................................................................................................................21 Toolbox..........................................................................................................................................21 Task List.........................................................................................................................................22 Summary...............................................................................................................................................22 Chapter 2: Using Databases............................................................................................................................23 Overview...............................................................................................................................................23 What Is a Database?..............................................................................................................................23 Why Use a Database?...........................................................................................................................23 Relational vs. Hierarchical....................................................................................................................24 Hierarchical Databases...................................................................................................................24 Relational Databases......................................................................................................................25 What C# Programmers Need to Know About Relational Databases....................................................25 UserMan Database Schema..................................................................................................................36 Summary...............................................................................................................................................37 i Database Programming with C# Table of Contents Chapter 3A: Presenting ADO.NET: The Connected Layer........................................................................38 Overview...............................................................................................................................................38 Data−Related Namespaces...................................................................................................................40 Data Providers and Drivers...................................................................................................................40 Specifying a Provider or Driver When Connecting.......................................................................42 .NET Data Providers......................................................................................................................42 The Connection Class.....................................................................................................................43 ConnectionString Property.............................................................................................................44 Connection Class Properties...........................................................................................................53 Connection Class Methods.............................................................................................................55 Connection Class Events................................................................................................................58 Opening a Connection....................................................................................................................59 Closing a Connection.....................................................................................................................60 Disposing of a Connection.............................................................................................................60 Comparing Two Connection Objects.............................................................................................60 Manipulating the Connection State................................................................................................61 Pooling Connections.......................................................................................................................63 Handling Provider, Driver, and Data Source Messages.................................................................67 Transactions....................................................................................................................................68 Handling Connection Class Exceptions.........................................................................................76 Using Command Objects......................................................................................................................85 OleDbCommand, OdbcCommand, and SqlCommand...................................................................86 Command Class Properties............................................................................................................89 Command Class Methods...............................................................................................................95 Executing a Command...................................................................................................................96 Handling Command Class Exceptions...........................................................................................98 Using the DataReader Class................................................................................................................100 SqlDataReader, OdbcDataReader, and OleDbDataReader..........................................................101 Declaring and Instantiating a DataReader Object........................................................................101 Reading Rows in a DataReader....................................................................................................102 Closing a DataReader...................................................................................................................102 Checking for Null Values in Columns.........................................................................................103 Handling Multiple Results............................................................................................................103 DataReader Properties..................................................................................................................104 DataReader Methods....................................................................................................................104 Handling DataReader Exceptions................................................................................................110 When to Use the DataReader Class..............................................................................................110 XmlReader....................................................................................................................................110 XmlReader Properties..................................................................................................................111 XmlReader Methods.....................................................................................................................112 Declaring and Instantiating an XmlReader Object.......................................................................115 Reading Rows in an XmlReader..................................................................................................116 Closing an XmlReader.................................................................................................................116 Handling XmlReader Exceptions.................................................................................................116 The DataAdapter Explained................................................................................................................117 DataAdapter Properties................................................................................................................118 DataAdapter Methods...................................................................................................................119 DataAdapter Events......................................................................................................................119 ii Database Programming with C# Table of Contents Chapter 3A: Presenting ADO.NET: The Connected Layer Instantiating a DataAdapter..........................................................................................................120 Setting the Command Properties..................................................................................................122 Handling Fill Operation Errors.....................................................................................................126 Handling Row Updates................................................................................................................127 Using the CommandBuilder Class......................................................................................................129 When to Use the CommandBuilder Class....................................................................................129 Preparing the DataAdapter...........................................................................................................130 Summary.............................................................................................................................................131 Chapter 3B: Presenting ADO.NET: The Disconnected Layer..................................................................133 Using the DataSet Class......................................................................................................................133 Recordset vs. DataSet...................................................................................................................134 DataSet Properties........................................................................................................................135 DataSet Methods..........................................................................................................................138 DataSet Events.............................................................................................................................140 Instantiating a DataSet..................................................................................................................141 Populating Your DataSet Using the DataAdapter........................................................................142 Updating Your Data Source Using the DataAdapter...................................................................144 Clearing Data from a DataSet......................................................................................................146 Copying a DataSet........................................................................................................................146 Merging Data in a DataSet with Other Data................................................................................146 Detecting and Handling Changes to Data in a DataSet................................................................148 Accepting or Rejecting Changes to Data in a DataSet.................................................................150 Using the DataTable Class..................................................................................................................152 DataTable Properties....................................................................................................................152 DataTable Methods......................................................................................................................154 DataTable Events.........................................................................................................................156 Declaring and Instantiating a DataTable......................................................................................157 Building Your Own DataTable....................................................................................................157 Populating a DataTable................................................................................................................159 Clearing Data from a DataTable..................................................................................................159 Copying a DataTable....................................................................................................................159 Searching a DataTable and Retrieving a Filtered Data View......................................................160 Copying Rows in a DataTable......................................................................................................161 Examining the Order of Column and Row Change Events..........................................................163 Handling Column Changes..........................................................................................................163 Handling Row Changes................................................................................................................164 Handling Row Deletions..............................................................................................................166 Using the DataView Class..................................................................................................................167 DataView Properties.....................................................................................................................167 DataView Methods.......................................................................................................................168 DataView Events..........................................................................................................................169 Declaring and Instantiating a DataView......................................................................................170 Searching a DataView..................................................................................................................170 Sorting a DataView......................................................................................................................171 Using the DataRow Class...................................................................................................................172 Declaring and Instantiating a DataRow........................................................................................174 iii Database Programming with C# Table of Contents Chapter 3B: Presenting ADO.NET: The Disconnected Layer Building Your Own DataRow......................................................................................................175 Using the DataColumn Class..............................................................................................................175 Declaring and Instantiating a DataColumn..................................................................................176 Using the DataRelation Class.............................................................................................................177 Declaring and Instantiating a DataRelation..................................................................................178 Looking at Cursors..............................................................................................................................183 Cursor Location............................................................................................................................184 Cursor Types................................................................................................................................184 Examining Data Source Locking........................................................................................................186 Pessimistic Locking......................................................................................................................186 Optimistic Locking.......................................................................................................................188 Using "Classic" ADO and COM Interop............................................................................................194 Summary.............................................................................................................................................195 Chapter 4: Presenting the IDE From a Database Viewpoint....................................................................197 Using the Server Explorer...................................................................................................................197 Handling Data Connections..........................................................................................................197 Handling Servers..........................................................................................................................200 Looking at Database Projects..............................................................................................................206 Creating a Database Project Folder..............................................................................................208 Deleting a Database Project Folder..............................................................................................208 Adding Database Objects to a Database Project..........................................................................209 Designing Databases with Visio for Enterprise Architect..................................................................212 Designing Databases with the Database Designer..............................................................................212 Creating a Database Diagram.......................................................................................................213 Saving the Database Diagram......................................................................................................218 Using the Table Designer....................................................................................................................218 Adding Columns...........................................................................................................................218 Setting the Primary Key...............................................................................................................219 Adding Indexes and Keys.............................................................................................................219 Adding Constraints.......................................................................................................................220 Creating a Relationship................................................................................................................222 Designing Queries with the Query Designer......................................................................................222 Taking a Closer Look at the Query Designer Panes....................................................................223 Verifying the SQL Syntax............................................................................................................227 Executing a Query........................................................................................................................228 Examining the Various Query Types...........................................................................................228 Script Editing Using the SQL Editor..................................................................................................230 Using the Query Editor to Produce SQL Statements...................................................................231 Saving a Script..............................................................................................................................232 Editing and Using Script Templates.............................................................................................232 Running SQL Scripts....................................................................................................................232 Creating Typed Data Sets...................................................................................................................233 Using the XML Designer to Create a Typed Data Set.................................................................233 Using the DataSet Designer to Create a Typed Data Set.............................................................233 Summary.............................................................................................................................................235 iv Database Programming with C# Table of Contents Chapter 5: Exception Handling....................................................................................................................236 Overview.............................................................................................................................................236 Enabling Exception Handling.............................................................................................................237 Using Two or More Exception Handlers in One Procedure...............................................................237 Examining the Exception Class..........................................................................................................238 Handling Exceptions in the Exception Handler..................................................................................239 Filtering Exceptions............................................................................................................................243 Creating Your Own Exception............................................................................................................244 Throwing an Exception.......................................................................................................................245 Handling Data−Related Exceptions....................................................................................................245 CLR Handling of Exceptions..............................................................................................................246 Using the Debug Class........................................................................................................................247 Enabling Debugging.....................................................................................................................247 Disabling Debugging....................................................................................................................248 Using Debug Assertions...............................................................................................................249 Using Debug Error Messages.......................................................................................................250 Debug Class Methods and Properties...........................................................................................251 Summary.............................................................................................................................................251 Chapter 6: Using Stored Procedures, Views, and Triggers.......................................................................252 Overview.............................................................................................................................................252 Optimization Issues.............................................................................................................................252 Troubleshooting Performance Degradation.................................................................................254 Using Stored Procedures.....................................................................................................................255 Why Use a Stored Procedure?......................................................................................................256 Creating and Running a Stored Procedure...................................................................................257 Retrieving a Value Specified with RETURN...............................................................................266 Changing the Name of a Stored Procedure..................................................................................267 Viewing Stored Procedure Dependencies....................................................................................267 Running Oracle Stored Procedures..............................................................................................267 Using Views........................................................................................................................................272 View Restrictions.........................................................................................................................272 Why Use a View?.........................................................................................................................272 Creating a View............................................................................................................................273 Running a View from the IDE.....................................................................................................274 Using a View from Code..............................................................................................................274 Using Triggers....................................................................................................................................277 Why Use a Trigger?.....................................................................................................................277 Creating a Trigger........................................................................................................................278 Viewing Trigger Source...............................................................................................................281 Summary.............................................................................................................................................281 Chapter 7: Hierarchical Databases..............................................................................................................283 Looking at LDAP................................................................................................................................283 Exploring Active Directory..........................................................................................................284 Accessing Active Directory Programmatically..................................................................................284 Examining the System.DirectoryServices Namespace................................................................284 Studying the DirectoryEntry Class...............................................................................................285 v Database Programming with C# Table of Contents Chapter 7: Hierarchical Databases Searching for an Object in Active Directory................................................................................289 Manipulating Object Property Values..........................................................................................293 Accessing Active Directory Using the OLE DB .NET Data Provider...............................................296 Specifying an OLE DB Provider for the Connection...................................................................297 Specifying What Domain to Access with the LDAP Protocol.....................................................297 Specifying What Information to Retrieve from Active Directory...............................................297 Updating an Active Directory Object...........................................................................................298 Retrieving the SID for a User.......................................................................................................298 Accessing Microsoft Exchange Server...............................................................................................299 Using the Microsoft OLE DB Exchange Server Provider...........................................................300 Using the Microsoft OLE DB Provider for Internet Publishing..................................................301 Accessing Exchange Server As a Linked Server from SQL Server.............................................303 Summary.............................................................................................................................................307 Chapter 8: Message Queues..........................................................................................................................308 Overview.............................................................................................................................................308 Connectionless Programming.............................................................................................................308 Taking a Quick Look at the MessageQueue Class.............................................................................309 When to Use a Message Queue..........................................................................................................309 How to Use a Message Queue............................................................................................................311 Private Queues vs. Public Queues................................................................................................311 Creating the Queue Programmatically.........................................................................................311 Displaying or Changing the Properties of Your Message Queue................................................313 Binding to an Existing Message Queue.......................................................................................315 Sending a Message.......................................................................................................................316 Retrieving a Message...................................................................................................................317 Peeking at Messages.....................................................................................................................320 Picking Specific Messages from a Queue....................................................................................321 Retrieving All Messages in a Queue............................................................................................322 Sending and Retrieving Messages Asynchronously....................................................................323 Clearing Messages from the Queue..............................................................................................324 Prioritizing Messages...................................................................................................................326 Locating a Message Queue...........................................................................................................327 Removing a Message Queue........................................................................................................329 Controlling Message Queue Storage Size....................................................................................330 Making Message Queues Transactional.............................................................................................330 Internal Transactions....................................................................................................................330 External Transactions...................................................................................................................330 Creating a Transactional Message Queue....................................................................................331 Starting a Transaction...................................................................................................................331 Ending a Transaction....................................................................................................................332 Using the MessageQueueTransaction Class.................................................................................332 Looking at System−Generated Queues...............................................................................................334 Using Journal Storage..................................................................................................................335 Securing Your Message Queuing.......................................................................................................338 Using Authentication....................................................................................................................338 Using Encryption..........................................................................................................................341 vi Database Programming with C# Table of Contents Chapter 8: Message Queues Using Access Control...................................................................................................................344 Summary.............................................................................................................................................347 Chapter 9: Data Wrappers...........................................................................................................................348 Why Use Data Wrappers?...................................................................................................................348 Looking at Object Oriented Programming.........................................................................................348 Polymorphism..............................................................................................................................349 Inheritance....................................................................................................................................349 Encapsulation...............................................................................................................................351 C# Keywords Related to OOP......................................................................................................352 Wrapping a Database..........................................................................................................................354 Creating the CUser Class.............................................................................................................354 Summary.............................................................................................................................................362 Chapter 10: Data−Bound Controls..............................................................................................................363 Data−Bound Controls vs. Manual Data Hooking...............................................................................363 Different Controls for Different UIs...................................................................................................364 Using Data−Bound Controls with Windows Forms....................................................................364 Using Data−Bound Controls with Web Forms............................................................................375 Summary.............................................................................................................................................386 Chapter 11: UserMan....................................................................................................................................387 Overview.............................................................................................................................................387 Identifying the UserMan Information.................................................................................................387 Discovering the Objects......................................................................................................................387 Database Objects..........................................................................................................................387 Active Directory Object...............................................................................................................397 Other Objects................................................................................................................................399 Creating the Client..............................................................................................................................399 Creating a Windows Client..........................................................................................................399 Creating a Web Client..................................................................................................................400 Tips and Ideas.....................................................................................................................................400 Database Suggestions...................................................................................................................400 General Suggestions.....................................................................................................................401 Summary.............................................................................................................................................402 Appendix A: Using XML with SQL Server 2000........................................................................................403 Overview.............................................................................................................................................403 Using SQLXML 2.0...........................................................................................................................406 Installing SQLXML 2.0...............................................................................................................406 Configuring the ISAPI Extension.................................................................................................407 Testing the ISAPI Extension........................................................................................................412 Restarting the Virtual Directory Application...............................................................................412 Inspecting the SQLXML 2.0 Managed Classes...........................................................................420 Summary.............................................................................................................................................438 Symbols and Numbers..................................................................................................................439 A...................................................................................................................................................439 vii Database Programming with C# Table of Contents Appendix A: Using XML with SQL Server 2000 B...................................................................................................................................................440 C...................................................................................................................................................440 D...................................................................................................................................................444 E....................................................................................................................................................444 F....................................................................................................................................................445 G...................................................................................................................................................445 H...................................................................................................................................................450 I.....................................................................................................................................................450 J....................................................................................................................................................457 K...................................................................................................................................................457 L....................................................................................................................................................459 M..................................................................................................................................................459 N...................................................................................................................................................460 O...................................................................................................................................................460 P....................................................................................................................................................462 Q...................................................................................................................................................462 R...................................................................................................................................................463 S....................................................................................................................................................463 T....................................................................................................................................................465 U...................................................................................................................................................465 V...................................................................................................................................................466 W..................................................................................................................................................466 X...................................................................................................................................................467 Index.................................................................................................................................................................467 Index.................................................................................................................................................................468 Index.................................................................................................................................................................468 Index.................................................................................................................................................................471 Index.................................................................................................................................................................471 Index.................................................................................................................................................................473 Index.................................................................................................................................................................473 Index.................................................................................................................................................................476 Index.................................................................................................................................................................476 Index.................................................................................................................................................................478 Index.................................................................................................................................................................478 viii

Description:
The book is useless, and I am a teacher, so I know what I am talking about. No wonder I was able to get a used copy for that cheap. Every body who buy it is trying to get rid of it. I looked through it once and never took it up again. This cannot help anybody. It like reading something in a newspape
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.