© 2004 Your company Standard print manual template Title page 1 Use this page to introduce the product by <AUTHOR> This is "Title Page 1" - you may use this page to introduce your product, show title, author, copyright, company logos, etc. This page intentionally starts on an odd page, so that it is on the right half of an open book from the readers point of view. This is the reason why the previous page was blank (the previous page is the back side of the cover) All rights reserved. No parts of this work may be reproduced in any form or by any means - graphic, electronic, or mechanical, including photocopying, recording, taping, or information storage and retrieval systems - without the written permission of the publisher. Products that are referred to in this document may be either trademarks and/or registered trademarks of the respective owners. The publisher and the author make no claim to these trademarks. While every precaution has been taken in the preparation of this document, the publisher and the author assume no responsibility for errors or omissions, or for damages resulting from the use of information contained in this document or from the use of programs and source code that may accompany it. In no event shall the publisher and the author be liable for any loss of profit or any other commercial damage caused or alleged to have been caused directly or indirectly by this document. Printed: September 2004 in (whereever you are located) Mastering C# Database Programming @Team LiB © 2004 Your company Publisher Special thanks to: All the people who contributed to this document, to mum and dad and grandpa, to my sisters and brothers and mothers in law, to our secretary Kathrin, to the graphic artist who created this great product logo on the cover page (sorry, don't remember your name at the moment but you did a great work), to the pizza service down the street (your daily Capricciosas saved our lives), to the copy shop where this document will be duplicated, and and and Last not least, we want to thank EC Software who wrote this great help tool called HELP & MANUAL which printed this document. Managing Editor Technical Editors Cover Designer enter name enter name enter name enter name enter name Production enter name Team Coordinator enter name Table of Contents Foreword 1 Part I Table of Contents 3 Part II BackCover 5 Part III Mastering C# Database Programming 7 Part IV Introduction 9 91 How to Use This Book 112 Downloading the Example Programs Part V Part 1: Introduction to ADO.NET and Databases 13 131 Chapter 1: Introduction to Database Programming with ADO.NET 13Obtaining the Required Software 14Developing Your First ADO.NET Program 17Connecting to Access and Oracle Databases 19Introducing Visual Studio .NET 23Using the .NET Documentation 25Using the SQL Server Documentation 26Summary 262 Chapter 2: Introduction to Databases 27Introducing Databases 28Using SQL Server 34Exploring the Northwind Database 43Building Queries Using Enterprise Manager 45Creating a Table 50Summary 513 Chapter 3: Introduction to Structured Query Language (SQL) 51Using SQL 70Accessing a Database Using Visual Studio .NET 73Summary 734 Chapter 4: Introduction to Transact-SQL Programming 74Fundamentals of Transact-SQL 77Using Cursors 78Using Functions 86Creating User-Defined Functions 89Introducing Stored Procedures 91Introducing Triggers 94Summary 945 Chapter 5: Overview of the ADO.NET Classes 95The Managed Provider and Generic Data Set Classes 98Performing a SQL SELECT Statement and Storing the Rows Locally 102Summary Mastering C# Database Programming @Team LiBI © 2004 Your company 1026 Chapter 6: Introducing Windows Applications and ADO.NET 103Developing a Simple Windows Application 109Using Windows Controls 110Using a DataGrid Control to Access a Database 116Using the Data Form Wizard to Create a Windows Form 124Summary Part VI Part 2: Fundamental Database Programming with ADO.NET 126 1261 Chapter 7: Connecting to a Database 126Understanding the SqlConnection Class 128Using a SqlConnection Object to Connect to a SQL Server Database 132Creating a Connection Object Using Visual Studio .NET 137Summary 1382 Chapter 8: Executing Database Commands 138The SqlCommand Class 140Creating a SqlCommand Object 141Executing SELECT Statements and TableDirect Commands 149Executing Commands that Modify Information in the Database 151Introducing Transactions 152Supplying Parameters to Commands 156Executing SQL Server Stored Procedures 160Creating a Command Object Using Visual Studio .NET 161Summary 1623 Chapter 9: Using DataReader Objects to Read Results 162The SqlDataReader Class 165Creating a SqlDataReader Object 165Reading Rows from a SqlDataReader Object 167Returning Strongly Typed Column Values 168Using the Get* Methods to Read Column Values 170An Example of Using the Get* Methods 177Reading Null Values 177Executing Multiple SQL Statements 179Using a DataReader Object in Visual Studio .NET 181Summary 1824 Chapter 10: Using Dataset Objects to Store Data 182The SqlDataAdapter Class 185The DataSet Class 195Writing and Reading XML Using a DataSet Object 198Mapping Tables and Columns 199Reading a Column Value Using Strongly Typed DataSet Classes 203Creating a DataAdapter Object Using Visual Studio .NET 207Creating a DataSet Object Using Visual Studio .NET 208Summary 2085 Chapter 11: Using DataSet Objects to Modify Data 209The DataTable Class 211The DataRow Class 212The DataColumn Class 213Adding Restrictions to DataTable and DataColumn Objects 221Finding, Filtering, and Sorting Rows in a DataTable 223Modifying Rows in a DataTable IIContents II © 2004 Your company 228Retrieving New Identity Column Values 230Using Stored Procedures to Add, Modify, and Remove Rows from the Database 235Automatically Generating SQL Statements 236Exploring the DataAdapter and DataTable Events 240Dealing with Update Failures 242Using Transactions with a DataSet (SQL) 243Modifying Data Using a Strongly Typed DataSet 244Summary 2456 Chapter 12: Navigating and Modifying Related Data 245The UniqueConstraint Class 246Creating a UniqueConstraint Object 247The ForeignKeyConstraint Class 248Creating a ForeignKeyConstraint Object 249The DataRelation Class 250Creating and Using a DataRelation Object 252Adding, Updating, and Deleting Related Rows 256Issues Involved When Updating the Primary Key of a Parent Row 259Nested XML 261Defining a Relationship Using Visual Studio .NET 264Summary 2647 Chapter 13: Using DataView Objects 265The DataView Class 267Creating and Using a DataView Object 268Using the Default Sort Algorithm 269Performing Advanced Filtering 269The DataRowView Class 270Finding DataRowView Objects in a DataView 271Adding, Modifying, and Removing DataRowView Objects from a DataView 273Creating Child DataView Objects 274The DataViewManager Class 274Creating and Using a DataViewManager Object 275Creating a DataView Using Visual Studio .NET 277Summary Part VII Part 3: Advanced Database Programming with ADO.NET 279 2791 Chapter 14: Advanced Transaction Control 279The SqlTransaction Class 280Setting a Savepoint 282Setting the Transaction Isolation Level 286Understanding SQL Server Locks 293Summary 2932 Chapter 15: Introducing Web Applications-ASP.NET 294Creating a Simple ASP.NET Web Application Using VS .NET 297The Web Form Controls 299Building a More Complex Application 302Using a DataGrid Control to Access a Database 310Using a DataList Control to Access a Database 315Maintaining State in a Web Application 318Creating a Simple Shopping Cart Application 322Summary 3223 Chapter 16: Using SQL Server's XML Support Mastering C# Database Programming @Team LiBIII © 2004 Your company 322Using the SQL Server FOR XML Clause 327Introducing XPath 329Introducing XSLT 331Accessing SQL Server Using HTTP 339Using the SQL Server OPENXML() Function 341Using an XmlDocument Object to Store an XML Document 346Using an XmlDataDocument Object to Store an XML Document 348Summary 3494 Chapter 17: Web Services 349Creating a Web Service 351Viewing a WSDL File and Testing a Web Service 354Using a Web Service 356Registering a Web Service 359Summary Part VIII Index 362 3621 Index_B 3622 Index_C 3623 Index_D 3624 Index_E 3625 Index_F 3626 Index_G 3627 Index_H 3628 Index_I 3629 Index_J 36210 Index_K 36211 Index_L 36312 Index_M 36313 Index_N 36314 Index_O 36315 Index_P 36316 Index_Q 36317 Index_R 36318 Index_S 36319 Index_T 36320 Index_U 36321 Index_V 36322 Index_W 36323 Index_X 36324 Index_Y Part IX List of Figures 365 IVContents IV © 2004 Your company Part X List of Tables 369 Part XI List of Listings 372 Part XII List of Sidebars 375 Index 0 Mastering C# Database Programming @Team LiBV © 2004 Your company Foreword This is just another title page placed between table of contents and topics 1Foreword © 2004 Your company Top Level Intro This page is printed before a new top-level chapter starts Part I [...]... object of the SqlConnection class to connect to a SQL Server database You use an object of the OleDbConnection class to connect to any database that supports OLE DB (Object Linking and Embedding for Databases), such as Oracle or Access You use an object of the OdbcConnection class to connect to any database that supports ODBC (Open Database Connectivity) Ultimately, all communication with a database is... mySqlConnection.Close(); Handling Exceptions You handle exceptions that might be thrown in your code by placing the code within a try/catch block You'll notice that the nine steps are placed within a try/catch block, with the catch block handling a SqlException object that might be thrown by the code in the try block The SqlException class is specifically for use with code that accesses a SQL Server database. .. id=SCOTT;password=TIGER"; // create an OleDbConnection object to connect to the // database, passing the connection string to the constructor OleDbConnection myOleDbConnection = new OleDbConnection(connectionString); // create an OleDbCommand object OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); // set the CommandText property of the OleDbCommand object to // a SQL SELECT statement that retrieves... OleDbConnectionAccess { public static void Main() { // formulate a string containing the details of the // database connection string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" + "data source=F:\\Program Files\\Microsoft Office\\Office\\Samples\\Northwind.mdb"; // create an OleDbConnection object to connect to the // database, passing the connection string to the constructor OleDbConnection... DB) such as Access or Oracle You'll learn more about the System.Data.OleDb namespace in Chapter 5, "Overview of the ADO.NET Classes." Connecting to an Access Database You connect to an Access database using an OleDbConnection object-rather than a SqlConnection object-with a connection string of the following format: provider=Microsoft.Jet.OLEDB.4.0;data source=databaseFile © 2004 Your company 18 Mastering. .. myOleDbConnection = new OleDbConnection(connectionString); // create an OleDbCommand object OleDbCommand myOleDbCommand = myOleDbConnection.CreateCommand(); // set the CommandText property of the OleDbCommand object to // a SQL SELECT statement that retrieves a row from the Customers table myOleDbCommand.CommandText = "SELECT CustomerID, CompanyName, ContactName, Address "+ "FROM Customers "+ "WHERE CustomerID... Northwind database SqlConnection mySqlConnection = new SqlConnection( "server=localhost ;database= Northwind;uid=sa;pwd=sa" ); // step 2: create a SqlCommand object SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); // step 3: set the CommandText property of the SqlCommand object to // a SQL SELECT statement that retrieves a row from the Customers table mySqlCommand.CommandText = "SELECT CustomerID, CompanyName,... 5.1.3 Connecting to Access and Oracle Databases Connecting to Access and Oracle Databases In this section you'll see examples of connecting to both an Access and an Oracle database To interact with either of these databases in your program, you use classes from the System.Data.OleDb namespace This namespace contains classes for use with databases that support object linking and embedding for databases... "FROM Customers "+ "WHERE CustomerID = 'ALFKI'"; Step 4: Open the SqlConnection Object Step 4 opens the database connection using the Open() method of the SqlConnection object created in step 1: mySqlConnection.Open(); Once the connection to the database is open, you can send commands to the database for © 2004 Your company 16 Mastering C# Database Programming @Team LiB execution Step 5: Run the SELECT... in the try block You'll learn more about this later in the section "Handling Exceptions" after I've discussed the nine steps in the following sections Step 1: Create a SqlConnection Object to Connect to the Database You use an object of the SqlConnection class to connect to a SQL Server database Step 1 in the Main() method creates a SqlConnection object named mySqlConnection to connect to the SQL Server . OdbcConnection. You use an object of the SqlConnection class to connect to a SQL Server database. You use an object of the OleDbConnection class to connect. 1261 Chapter 7: Connecting to a Database 126Understanding the SqlConnection Class 128Using a SqlConnection Object to Connect to a SQL Server Database