|
Beginning SQL Server 2005 Express Database Applications with Visual Basic Express and Visual Web Developer Express by Rick Dobson, Ph.D. Here's a detailed table of contents for the book. The table of contents shows page numbers so that you can see that our book provides overall in-depth coverage as well as the amount of space devoted to each major topic. If you want a more general overview of the book and who's it for see our FAQ for the book with its answer to the question about who the book is for.
PART 1 ■ ■ ■ Working with SQL Server Express■ CHAPTER 1 Getting Started with SQL Server Express. . . . . . . . . . . . . . . . . . . . . . 3. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Who Is SQL Server Express For? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5. . . . . . . . . . . . . . . . . . . . . 6 Business Analysts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6Database Administrators and Operations Specialists . . . . . . . . . . . . . . . . . . 7Professional Developers in Need of a Free, Modern Database . . . . . . . . . . . 8SQL Server Express vs. SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10SQL Server Express vs. MSDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11Installing SQL Server Express . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12. . . . . . . . . . . . . . . . . . 12 Registration, Feature, and Instance Name Screens . . . . . . . . . . . . . . . . . . . 13Service Account and Authentication Mode Screens . . . . . . . . . . . . . . . . . . . 14Collation, Error Reporting, and Ready to Install Screens . . . . . . . . . . . . . . . 15Using SQL Server Express Query Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Configuring SQL Server Express to Start Automatically . . . . . . . . . . . . . . . . 17Configuring SQL Server Express for Network Access . . . . . . . . . . . . . . . . . 19Configuring the Windows XP Firewall for SQL Server Express . . . . . . . . . . 21Connecting from the SSMS-Based Query Tool . . . . . . . . . . . . . . . . . . . . . . . 22Connecting from Remote Down-Level Clients . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Connecting to SQL Server Express from Visual Studio 2003 . . . . . . . . . . . 25Connecting to SQL Server Express from Access Projects . . . . . . . . . . . . . . 27Connecting to SQL Server Express from Access Linked Tables . . . . . . . . . 28Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31■CHAPTER 2 Graphical and Command-Line Query Tools . . . . . . . . . . . . . . . . . . 33 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Using Database, Table, and View Designers . . . . . . . . . . . . . . . . . . . . . . . . . 41Designing, Running, Saving, and Rerunning SQL Scripts . . . . . . . . . . . . . . 51Using the sqlcmd Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Running Statements from a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62Saving Output to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64Dynamically Running and Batching sqlcmd Statements . . . . . . . . . . . . . . . 65Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71■ CHAPTER 3 Exploring, Creating, and Recovering Databases . . . . . . . . . . . . . 73. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Types of Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Getting Meta Data About Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75Using sp_helpdb for Database Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76Using System Catalog Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78Searching for and Copying Databases with Windows Explorer . . . . . . . . . . 83Using CREATE DATABASE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Designating Data Files in the ON Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85Using the LOG ON Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Attaching and Detaching Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88Attaching Databases with CREATE DATABASE . . . . . . . . . . . . . . . . . . . . . . . 89Copying Files, the Auto-Close Feature, and sp_detach_db . . . . . . . . . . . . . 95Backing Up and Restoring Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 Choosing and Modifying a Database’s Recovery Model . . . . . . . . . . . . . . 101Performing a Full Data Backup and Restore . . . . . . . . . . . . . . . . . . . . . . . . 102Performing Differential Backups and Restores . . . . . . . . . . . . . . . . . . . . . . 103Cleaning Up the Backup and Restore Scripts . . . . . . . . . . . . . . . . . . . . . . . 106Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106■ CHAPTER 4 Data Types, Tables, and Constraints . . . . . . . . . . . . . . . . . . . . . . . . . 107. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Character and Binary Byte Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117Creating Tables and Using Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Adding Data to Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124Managing Data Integrity with Basic Constraints and Column Properties . . . . . . 128. . . . . . . . . . . . . . . . . . . . . . . 128 Not Allowing Null Values in a Column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131Designating Default Column Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132Designating a Column as a Primary Key . . . . . . . . . . . . . . . . . . . . . . . . . . . 135Managing Data Integrity with Sophisticated Constraints . . . . . . . . . . . . . . . . . . . 139. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Using Multicolumn Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143Using Foreign Key Constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154■ CHAPTER 5 Creating Queries from a Single Database Object . . . . . . . . . . . 155. . . . . . . . . . . . . . . . . . . . . . . . . . 155 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Using the Current Database Context or a Different Database . . . . . . . . . . 165Specifying Queries from Another Server Instance . . . . . . . . . . . . . . . . . . . 168Filtering, Grouping, and Aggregating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175. . . . . . . . . . . . . . 175 Filtering for Values in a Range . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177Grouping and Aggregating . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189■ CHAPTER 6 Querying Multiple Database Objectsand Manipulating Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Cross Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200Joins for More Than Two Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205Using Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . 208 Including a Subquery in a WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . 211Including a Correlated Subquery in a WHERE Clause . . . . . . . . . . . . . . . . . 214Explicitly Ordering and Ranking Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215. . . . . . . . . . . . . . . . . . . 215 Ranking Result Set Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219Manipulating Result Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224. . . . . . . . . . . . . . . . . . . . . . . . . . 225 Appending Result Sets to One Another . . . . . . . . . . . . . . . . . . . . . . . . . 229 Reusing Queries with Common Table Expressions . . . . . . . . . . . . . . . . 232 Converting Between Relational and Cross-Tabulated Tables . . . . . . . . . 239 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 ■ CHAPTER 7 Leveraging Database Objects That Encapsulate T-SQL . . . . 247. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 Performing Data Modification with a View . . . . . . . . . . . . . . . . . . . . . . . . . 252Processing Meta Data with System Views . . . . . . . . . . . . . . . . . . . . . . . . . 256Creating and Using User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259. . . . . . . . . . . . . . . . . . . . . 260 Creating and Using IF User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . 263Creating and Using Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267. . . . . . . . . . . . . . . . . . . . . . . . . 268 Returning Result Sets and Input Parameters . . . . . . . . . . . . . . . . . . . . . . . 271Returning Scalar Values with Output Parameters . . . . . . . . . . . . . . . . . . . . 278Processing Return Status Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282Performing Data Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Creating and Using Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292. . . . . . . . . . . . . . . . . . 293 Rolling Back in an AFTER Trigger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295Archiving Changes to a Table with Triggers . . . . . . . . . . . . . . . . . . . . . . . . 297Using an INSTEAD OF Trigger with a View . . . . . . . . . . . . . . . . . . . . . . . . . . 302Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305■ CHAPTER 8 Managing SQL Server Express Security . . . . . . . . . . . . . . . . . . . . . 307. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 Exploring and Creating Principals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309 Creating Principals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313Assigning Permissions to Principals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324. . . . . . . . . . . . . . . . . . . . . . . . . . 325 Assigning Permissions via the Fixed Server Roles . . . . . . . . . . . . . . . . . . . 327Assigning Permissions via the Fixed Database Roles . . . . . . . . . . . . . . . . 332Granting, Denying, and Revoking Permissions . . . . . . . . . . . . . . . . . . . . . . 339Creating and Using Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 Creating Objects in Owned Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353Creating Objects in Any Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 356Using Encryption Keys and Certificates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 Signing Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368PART 2 ■ ■ ■ Working with Visual Basic Expressand Visual Web Developer Express ■ CHAPTER 9 Introduction to Visual BasicExpress and Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 . . . . . . . . . . . . . . . . . . . . . . . 371 Adding Items to a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372Exploring a Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373Using Visual Basic Data Types in VBE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 Overview of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375Declaring Variables and Assigning Data Type Values . . . . . . . . . . . . . . . . . 379Building and Testing a Windows Form Calculator . . . . . . . . . . . . . . . . . . . . . . . . . 385. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Populating a Windows Form with Controls . . . . . . . . . . . . . . . . . . . . . . . . . 386Adding Button Click Event Procedures to a Form . . . . . . . . . . . . . . . . . . . . 387Testing the Code Behind a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389Handling Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390. . . . . . . . . . . . . . . . . . . . . . . . 392 Using Try...Catch Statements Directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393Invoking a Try...Catch Statement in a Function Procedure . . . . . . . . . . . . 394Dynamically Adapting to Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . 395Using the File System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397. . . . . . . . . . . . . . . . . 397 Exploring Folders and Viewing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399Reading Fixed-Width Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 404Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408■ CHAPTER 10 Introduction to Visual Web Developer Express,Web Pages, and Web Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 Starting and Exploring ASP.NET Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411 Exploring a New ASP.NET Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413Creating Pages for an ASP.NET Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 Returning a Formatted Date from a Calendar Control . . . . . . . . . . . . . . . . 419Programming a Message-for-the-Day Application . . . . . . . . . . . . . . . . . . . 424Creating and Linking HTML Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427Interacting with Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429. . . . . . . . . . . . . 429 Saying Hello Dynamically to a Web Form User . . . . . . . . . . . . . . . . . . . . . . 431Populating an Application Variable and Reading from It . . . . . . . . . . . . . . 435Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442■ CHAPTER 11 Programming ADO.NET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445 ADO.NET Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446Programming Connection Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447. . . . . . . . . 448 Getting Help with Constructing a Connection String . . . . . . . . . . . . . . . . . 450Connecting to SQL Server Express and Access Database Files . . . . . . . . 451Programming Command Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454. . . . . . . . . . . . . . . . . . . 455 Returning Forward-Only, Read-Only Rowsets . . . . . . . . . . . . . . . . . . . . . . . 462Performing Data Definition and Database Maintenance . . . . . . . . . . . . . . 471Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483■ CHAPTER 12 Programming DataAdapter and DataSet Objects . . . . . . . . . . . 485. . . . . . . . . . . . . . . . . . . . 486 Sorting and Filtering DataTables with DataViews . . . . . . . . . . . . . . . . . . . . 493Inserting, Updating, and Deleting Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 498Bulk Loading Data from Excel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514■ CHAPTER 13 Using Visual Database and Form Design Tools . . . . . . . . . . . . . 515. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 . . . . . . . . . . . . . . . . . . . . . . . . . . . 516 Exploring a SQL Server Express Database . . . . . . . . . . . . . . . . . . . . . . . . . 517Adding Objects to a SQL Server Express Database . . . . . . . . . . . . . . . . . 519Migrating and Revising an MSDE Database . . . . . . . . . . . . . . . . . . . . . . . . 522Adding a Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525Working with Data Sources and Windows Forms . . . . . . . . . . . . . . . . . . . . . . . . 526. . . . . . . . . . . . . . . 527 Dragging Data Source Items to a Windows Form . . . . . . . . . . . . . . . . . . . 529Modifying Control Behavior with Smart Tags . . . . . . . . . . . . . . . . . . . . . . . 532Creating and Using Interactive Data Displays . . . . . . . . . . . . . . . . . . . . . . 539Visual Web Developer Visual Database Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 Viewing Data and Modifying Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . 558Displaying Data on a Web Form Page with a GridView Control . . . . . . . . 560Performing Lookups on a Web Form Page . . . . . . . . . . . . . . . . . . . . . . . . . 564Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 |