CYAN MAGENTA YELLOW BLACK PANTONE 123 CV BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Companion eBook Available Beginning C# 2005 Databases: From Novice to Professional Beginning Beginning VB 2005 Databases Companion eBook Thousands of C# programmers have become database professionals through the first two editions of this book This third edition is even more comprehensive, but, as always, it assumes only basic knowledge of C#-and absolutely no experience with databases Everything you need to know to get off to the fastest start in database programming is clearly explained by examples you can use in professional, real-world programs Database proficiency requires basic understanding of relational concepts, reasonable competence with the database language SQL, and clear knowledge of how to interface between C# programs and databases We introduce all concepts carefully, in the order in which you can best learn them, and then we gradually use them in concert to reinforce your understanding You’ll learn how to define, query, and manipulate database data with T-SQL and how to manage SQL Server Express databases with SQL Server Management Studio Express You’ll learn about ADO.NET and how to use it to access databases from C# You’ll even learn how to write stored procedures and call them from C# programs You’ll learn much more besides Our focus is always on fundamental concepts and techniques that won’t change even when database technology does The major changes to this edition are due to new technology We cover the new XML data type and show you how to use it and other T-SQL features to manipulate XML-techniques that many experienced T-SQL programmers often don’t know well The most dramatic change is the new final chapter, which introduces Language-Integrated Query (LINQ), a powerful alternative to traditional ADO.NET programming that promises to be the future of all data access in NET So, whatever your database background or programming needs, we’re sure you’ll find in here all you need to become a highly proficient database programmer We believe you’ll have as much fun learning database programming as we have doing database programming Enjoy, Jim Huddleston See last page for details on $10 eBook version Beginning C# 2005 Databases www.free-ebooks-download.org From Novice to Professional What every C# programmer needs to know about SQL Server 2005, T-SQL, ADO.NET 2.0, and LINQ THE APRESS ROADMAP Pro C# 2005 and the NET 2.0 Platform, Third Edition Expert C# 2005 Business Objects, Second Edition SOURCE CODE ONLINE ISBN 1-59059-777-X www.apress.com 53999 US $39.99 Shelve in Programming Languages/C# 89253 59777 Huddleston Beginning C# 2005 Databases User level: Beginner–Intermediate C# 2005 Databases Dear Reader, Author of THE EXPERT’S VOICE ® IN NET James Huddleston Ranga Raghuram, Syed Fahad Gilani, Jacob Hammer Pedersen, and Jon Reid 781590 597774 this print for content only—size & color not accurate spine = 0.998" 528 page count 777Xfmfinal.qxd 11/18/06 2:31 PM Page i Beginning C# 2005 Databases From Novice to Professional James Huddleston, Ranga Raghuram, Syed Fahad Gilani, Jacob Hammer Pedersen, and Jon Reid 777Xfmfinal.qxd 11/18/06 2:31 PM Page ii Beginning C# 2005 Databases: From Novice to Professional Copyright © 2006 by James Huddleston, Ranga Raghuram, Syed Fahad Gilani, Jacob Hammer Pedersen, and Jon Reid 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-13 (pbk): 978-1-59509-777-4 ISBN-10 (pbk): 1-59059-777-X Printed and bound in the United States of America 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 Lead Editor: Jonathan Hassell Technical Reviewer: Vidya Vrat Agarwal Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick, Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Elizabeth Seymour Copy Edit Manager: Nicole Flores Copy Editors: Nicole Abramowitz, Liz Welch Assistant Production Director: Kari Brooks-Copony Production Editor: Janet Vail Compositor: Linda Weidemann, Wolf Creek Press Proofreader: April Eddy Indexer: Kevin Broccoli/Broccoli Information Management Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com, or visit http://www.springeronline.com For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710 Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com, or visit http://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(s) 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 The source code for this book is available to readers at http://www.apress.com in the Source Code/ Download section 777Xfmfinal.qxd 11/18/06 2:31 PM Page iii To Jared, Quinn, and Tess I love you —Jim Huddleston 777Xfmfinal.qxd 11/18/06 2:31 PM Page iv 777Xidxfinal.qxd 11/18/06 2:28 PM Page 499 ■INDEX ■T ■U Unhandled Exceptions, 124 See also error messages; exceptions UNION joins, SQL, 311–312 uniquridentifier data type, 63 Unknown connection option in connection string, 102 untyped datasets, 225–226 Update method, 212 UPDATE statement updating data, 57 using transactions, 359 UpdateCommand property, DataAdapter classes persisting changes to database, 200–201 propagating changes to data source, 196–202 updating data, 57–58, 217–218 User ID clause, connection string, 103 using directives, 75, 82 ■V var type, 470 varbinary data type, 62, 404 varchar data type, 61, 404 VCSE, 1–5, 17, 30–35 Find it faster at http://superindex.apress.com/ tables See also relationships creating, 252 using SQL, 255–257 using SSMSE, 252–253 data integrity entity integrity, 265–266 overview, 265 referential integrity, 267–273 derived, 280 dropping overview, 258 with SQ., 259–261 with SSMSE, 258–259 used in example AAA, 412 getting data about, 161–164 normalization, 274–275 overview, 251 relationships between, 261–263 Tables property, 185 TargetSite property, 346 tempdb database, 19 text and binary data overview, 403 retrieving data from text columns, 425–430 retrieving images from database, 412–419 storing images in database, 404–412 storing text data, 419–425 types, 403–404 TextBox control, 230 Third normal form (3NF), 275 time data types, 62 Timeout clause, connection string See Connection Timeout clause timestamp data type, 63 tinyint data type, 61 ToDataTable method, 478 tools BOL, 35–39 SSMSE, 17–28 VCSE, 30–35 ToQueryable( ) method, 479 Transact SQL (T-SQL), 17, 362–373 transactions ACID properties, 360–361 atomicity property, 360 coding in ADO.NET, 373–377 overview, 361 in T-SQL, 362–373 coding in SQL, 365, 367 committing SQL transaction example, 367 durability property, 361 nested, 378 overview, 359 relationship to connections, 376 when to use, 359–360 TRUNCATE TABLE statement, 60 try block, 76, 78 T-SQL (Transact SQL), 17, 362–373 tuples, 251 typed accessor methods, 150–156 typed datasets, 225–226 499 777Xidxfinal.qxd 500 11/18/06 2:28 PM Page 500 ■INDEX vcssetup.exe, version attribute, 434 views, 313 ■W WHERE clause, 57, 471, 473 combining predicates, 52 operators of, 49–51 overview, 48–49 where clause, C# 3.0, 473 Windows Authentication, 75 Windows forms applications data binding complex data binding, 231–238 data-bound controls, 238–239 overview, 227 simple data binding, 228–230 synchronizing controls with data source, 240–244 data grids, 244–249 Windows integrated security, 102 WITH clause, 446, 449, 456 Workstation ID clause, connection string key details, 103 setting WorkstationId property, 111 WorkstationId property, SqlConnection class, 110 WriteLine method, Console class, 121 opening and closing connections, 99 tab character, 108 WriteXml( ) method, 435 WriteXmlSchema method, DataSet class, 218, 226 ■X XHTML (Extensible HTML), 432 XML (Extensible Markup Language), 66 and ADO.NET, 67 and datasets, 218–221 OPENXML generating edge table, 447–448 overview, 442–443 using, 443–445 overlapping elements, 432 overview, 431–432 understanding XML declaration, 434 understanding XML documents, 432–434 working with XML in ADO.NET, 218 FOR XML clause AUTO mode, 440–441 creating sample data, 435–437 overview, 434–435 RAW mode, 438–439 xml data type creating table to store XML, 450 overview, 449 storing and retrieving XML documents, 450–451 using element-centric schema with OPENXML, 454–455 using OPENXML with XML columns, 452–453 XML documents declarations, 434 elements, 433, 438 overview, 434 xml data type, 449 XML Path Language (XPath), 432 XML vocabulary, 432 xml2edge stored procedure, 449 xml2tbl stored procedure, 444, 447, 454 xmlcity table, 437 xmltest stored procedure, 450, 452 XPath (XML Path Language), 432 XSL (Extensible Stylesheet Language), 432 XSLT (XSL Transformations), 432 ... i Beginning C# 2005 Databases From Novice to Professional James Huddleston, Ranga Raghuram, Syed Fahad Gilani, Jacob Hammer Pedersen, and Jon Reid 777Xfmfinal.qxd 11/18/06 2:31 PM Page ii Beginning. .. property, SqlConnection class, 110 WriteLine method, Console class, 121 opening and closing connections, 99 tab character, 108 WriteXml( ) method, 435 WriteXmlSchema method, DataSet class, 218,... Dominic Shakeshaft, Jim Sumser, Keir Thomas, Matt Wade Project Manager: Elizabeth Seymour Copy Edit Manager: Nicole Flores Copy Editors: Nicole Abramowitz, Liz Welch Assistant Production Director: