Practical Database Programming with Visual Basic.NET, 2nd Edition

  Practical Database Programming with Visual Basic.NET

  IEEE Press 445 Hoes Lane Piscataway, NJ 08854

  

IEEE Press Editorial Board

  Lajos Hanzo, Editor in Chief R. Abari M. El - Hawary S. Nahavandi J. Anderson

  B. M. Hammerli W. Reeve

  F. Canavero M. Lanzerotti T. Samad T. G. Croda O. Malik

  G. Zobrist Kenneth Moore, Director of IEEE Book and Information Services (BIS)

  Practical Database

Programming with Visual

Basic.NET Second Edition Ying Bai Department of Computer Science and Engineering Johnson C. Smith University Charlotte, North Carolina

  IEEE PRESS A John Wiley & Sons, Inc., Publication

  Copyright © 2012 by the Institute of Electrical and Electronics Engineers, Inc. Published by John Wiley & Sons, Inc., Hoboken, New Jersey. All rights reserved. Published simultaneously in Canada. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 750-

4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to

the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011,

fax (201) 748-6008, or online at http://www.wiley.com/go/permissions.

  Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or

completeness of the contents of this book and specifi cally disclaim any implied warranties of merchantability

or fi tness for a particular purpose. No warranty may be created or extended by sales representatives or

written sales materials. The advice and strategies contained herein may not be suitable for your situation. You

should consult with a professional where appropriate. Neither the publisher nor author shall be liable for any

loss of profi t or any other commercial damages, including but not limited to special, incidental, consequential,

or other damages.

  For general information on our other products and services or for technical support, please contact our Customer Care Department within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not

be available in electronic formats. For more information about Wiley products, visit our web site at www. wiley.com.

  Library of Congress Cataloging-in-Publication Data: Bai, Ying, 1956– Practical database programming with Visual Basic.NET / Ying Bai. – 2nd ed. p. cm.

  ISBN 978-1-118-16205-7 (pbk.) 1. Microsoft Visual BASIC. 2. BASIC (Computer program language) 3. Microsoft .NET.

4. Database design. I. Title.

  QA76.73.B3B335 2012 005.2'768–dc23 2011039947 Printed in United States of America.

  10 9 8 7 6 5 4 3 2 1

  This book is dedicated to my wife, Yan Wang, and my daughter, Xue Bai.

  Contents Preface xxv Acknowledgments xxvii

Chapter 1 Introduction

  1 Outstanding Features about This Book

  2 Who This Book Is For

  2 What This Book Covers

  2 How This Book Is Organized and How to Use This Book

  5 How to Use the Source Code and Sample Databases

  6 Instructors and Customers Supports

  8 Chapter 2 Introduction to Databases

  10

2.1 What Are Databases and Database Programs?

  11

  2.1.1 File Processing System

  11

  2.1.2 Integrated Databases 12

  2.2 Develop a Database

  13

  2.3 Sample Database 14

  2.3.1 Relational Data Model

  14

  2.3.2 Entity-Relationship Model 17

2.4 Identifying Keys 17

  2.4.1 Primary Key and Entity Integrity

  17

  2.4.2 Candidate Key 18

  2.4.3 Foreign Keys and Referential Integrity

  18

2.5 Defi ne Relationships

  19

2.5.1 Connectivity 19

  2.6 ER Notation 21

  2.7 Data Normalization 22

  2.7.1 First Normal Form (1NF)

  22

  2.7.2 Second Normal Form (2NF)

  

23

  2.7.3 Third Normal Form (3NF)

  

24 vii Contents

2.8 Database Components in Some Popular Databases

  26

2.8.1 Microsoft Access Databases 26

  2.8.1.1 Database File 27

  2.8.1.2 Tables 27

  2.8.1.3 Queries 27

2.8.2 SQL Server Databases

  28

  2.8.2.1 Data Files 28

  2.8.2.2 Tables 29

  2.8.2.3 Views 29

  2.8.2.4 Stored Procedures 29

  2.8.2.5 Keys and Relationships

  30

  2.8.2.6 Indexes 30

  2.8.2.7 Transaction Log Files

  30

2.8.3 Oracle Databases 31

  2.8.3.1 Data Files 31

  2.8.3.2 Tables 31

  2.8.3.3 Views 32

  2.8.3.4 Stored Procedures 32

  2.8.3.5 Indexes 33

  2.8.3.6 Initialization Parameter Files

  33

  2.8.3.7 Control Files 33

  2.8.3.8 Redo Log Files

  34

  2.8.3.9 Password Files 34

  2.9 Create Microsoft Access Sample Database 34

  2.9.1 Create the LogIn Table

  34

  2.9.2 Create the Faculty Table

  36

  2.9.3 Create the Other Tables

  38

  2.9.4 Create Relationships among Tables

  41

  2.10 Create Microsoft SQL Server 2008 Sample Database

  44

  2.10.1 Create the LogIn Table

  46

  2.10.2 Create the Faculty Table

  48

  2.10.3 Create Other Tables 49

  2.10.4 Create Relationships among Tables

  54

  

2.10.4.1 Create Relationship between the LogIn and the Faculty Tables

  54

  2.10.4.2 Create Relationship between the LogIn and the Student Tables

  57

  2.10.4.3 Create Relationship between the Faculty and the Course Tables

  58

  2.10.4.4 Create Relationship between the Student and the StudentCourse Tables

  59

  2.10.4.5 Create Relationship between the Course and the StudentCourse Tables

  60

  2.11 Create Oracle 11g XE Sample Database

  61

  2.11.1 Create a New Oracle Customer User or User Account

  63

  2.11.2 Create New Customer Sample Database CSE_DEPT

  65

  2.11.3 Create the LogIn Data Table

  69

  2.11.4 Create the Faculty Data Table

  71

  2.11.5 Create Other Tables 74

  2.11.6 Create the Constraints between Tables

  78

  Contents

  3.4.4 The DataAdapter Class 112

  99

  3.4.2.1 The Open() Method of the Connection Class 101

  3.4.2.2 The Close() Method of the Connection Class 102

  3.4.2.3 The Dispose() Method of the Connection Class 102

  3.4.3 The Command and the Parameter Classes 103

  3.4.3.1 The Properties of the Command Class 103

  

3.4.3.2 The Constructors and Properties of the Parameter Class 104

  3.4.3.3 Parameter Mapping 105

  3.4.3.4 The Methods of the ParameterCollection Class 107

  3.4.3.5 The Constructor of the Command Class 108

  3.4.3.6 The Methods of the Command Class 109

  3.4.4.1 The Constructor of the DataAdapter Class 112

  98

  3.4.4.2 The Properties of the DataAdapter Class 112

  3.4.4.3 The Methods of the DataAdapter Class 113

  3.4.4.4 The Events of the DataAdapter Class 113

  3.4.5 The DataReader Class 115

  3.4.6 The DataSet Component 117

  3.4.6.1 The DataSet Constructor 119

  3.4.6.2 The DataSet Properties 120

  3.4.6.3 The DataSet Methods 120

  3.4.6.4 The DataSet Events 121

  3.4.7 The DataTable Component 123

  3.4.7.1 The DataTable Constructor 124

  3.4.2 The Connection Class

  3.4.1.4 The Oracle Data Provider

  

2.11.6.1 Create the Constraints between the LogIn and Faculty Tables

  3.1 The ADO and ADO.NET

  78

  

2.11.6.2 Create the Constraints between the LogIn and Student Tables

  81

  2.11.6.3 Create the Constraints between the Course and Faculty Tables

  83

  2.11.6.4 Create the Constraints between the StudentCourse and Student Tables

  83

  2.11.6.5 Create the Constraints between the StudentCourse and Course Tables

  85

  2.12 Chapter Summary 87 Homework 88 Chapter 3 Introduction to ADO.NET

  91

  91

  98

  3.2 Overview of ADO.NET 93

  3.3 The Architecture of ADO.NET

  94

  3.4 The Components of ADO.NET

  95

  3.4.1 The Data Provider

  95

  3.4.1.1 The ODBC Data Provider

  97

  3.4.1.2 The OLEDB Data Provider

  97

  3.4.1.3 The SQL Server Data Provider

  3.4.7.2 The DataTable Properties 125

  Contents

  4.3.3 Overview of LINQ to SQL 166

  4.1.4.3 Last (Element Purpose) 159

  4.1.4.4 Single (Element Purpose) 159

  4.1.4.5 ToArray (Conversion Purpose) 160

  4.1.4.6 ToList (Conversion Purpose) 160

  4.2 Introduction to LINQ Query 161

  4.3 The Architecture and Components of LINQ 164

  4.3.1 Overview of LINQ to Objects 165

  4.3.2 Overview of LINQ to DataSet 165

  4.3.4 Overview of LINQ to Entities 167

  4.1.4.1 ElementAt (Element Purpose) 158

  4.3.5 Overview of LINQ to XML 168

  4.4 LINQ to Objects 168

  4.4.1 LINQ and ArrayList 169

  4.4.2 LINQ and Strings 170

  4.4.2.1 Query a String to Determine the Number of Numeric Digits 171

  

4.4.2.2 Sort Lines of Structured Text by any Field in the Line 172

  4.4.3 LINQ and File Directories 175

  4.4.3.1 Query the Contents of Files in a Folder 175

  4.1.4.2 First (Element Purpose) 159

  4.1.4 Nondeferred Standard Query Operators 158

  3.4.7.3 The DataTable Methods 126

  

4.1.1.1 The IEnumerable and IEnumerable(Of T) Interfaces 150

  3.4.7.4 The DataTable Events 126

  3.4.8 ADO.NET Entity Framework 4.1 128

  3.4.8.1 Advantages of Using the Entity Framework 4.1 130

  3.4.8.2 The ADO.NET 4.1 Entity Data Model 132

  3.4.8.3 Using the ADO.NET 4.1 Entity Data Model Wizard 136

  3.5 Chapter Summary 145 Homework 146

Chapter 4 Introduction to Language Integrated Query (LINQ) 149

  4.1 Overview of Language Integrated Query 149

  4.1.1 Some Special Interfaces Used in LINQ 150

  4.1.1.2 The IQueryable and IQueryable(Of T) Interfaces 151

  4.1.3.7 Where (Restriction Purpose) 158

  4.1.2 Standard Query Operators 152

  4.1.3 Deferred Standard Query Operators 154

  4.1.3.1 AsEnumerable (Conversion Purpose) 154

  4.1.3.2 Cast (Conversion Purpose) 154

  4.1.3.3 Join (Join Purpose) 154

  4.1.3.4 OfType (Conversion Purpose) 156

  4.1.3.5 OrderBy (Ordering Purpose) 156

  4.1.3.6 Select (Projection Purpose) 157

  4.4.4 LINQ and Refl ection 177

  Contents

  4.5 LINQ to DataSet 179

  4.5.1 Operations to DataSet Objects 179

  4.5.1.1 Query Expression Syntax 180

  4.5.1.2 Method-Based Query Syntax 182

  4.5.1.3 Query the Single Table 184

  4.5.1.4 Query the Cross Tables 186

  4.5.1.5 Query Typed DataSet 189

  4.5.2 Operations to DataRow Objects Using the Extension Methods 192

  4.5.3 Operations to DataTable Objects 196

  4.6 LINQ to SQL 197

  4.6.1 LINQ to SQL Entity Classes and DataContext Class 198

  4.6.2 LINQ to SQL Database Operations 202

  4.6.2.1 Data Selection Query 203

  4.6.2.2 Data Insertion Query 205

  4.6.2.3 Data Updating Query 206

  4.6.2.4 Data Deletion Query 207

  4.6.3 LINQ to SQL Implementations 210

  4.7 LINQ to Entities 210

  4.7.1 The Object Services Component 211

  4.7.2 The ObjectContext Component 211

  4.7.3 The ObjectQuery Component 211

  4.7.4 LINQ to Entities Flow of Execution 211

  4.7.4.1 Construct an ObjectQuery Instance 212

  4.7.4.2 Compose a LINQ to Entities Query 212

  4.7.4.3 Convert the Query to Command Trees 212

  4.7.4.4 Execute the Query 213

  4.7.4.5 Materialize the Query 214

  4.7.5 Implementation of LINQ to Entities 214

  4.8 LINQ to

  XML 215

  4.8.1 LINQ to XML Class Hierarchy 215

  4.8.2 Manipulate

  XML Elements 216

  4.8.2.1 Creating XML from Scratch 216

  4.8.2.2 Insert

  XML 218

  4.8.2.3 Update

  XML 219

  4.8.2.4 Delete

  XML 220

  4.8.3 Manipulate

  XML Attributes 220

  4.8.3.1 Add

  XML Attributes 220

  4.8.3.2 Get

  XML Attributes 221

  4.8.3.3 Delete

  XML Attributes 222

  4.8.4 Query XML with LINQ to XML 222

  4.8.4.1 Standard Query Operators and XML 223

  4.8.4.2 XML Query Extensions 224

  4.8.4.3 Using Query Expressions with XML 224

  4.8.4.4 Using XPath and XSLT with LINQ to XML 225

  4.8.4.5 Mixing XML and Other Data Models 225

  Contents

  4.9 Visual Basic.NET Language Enhancement for LINQ 227

  4.9.1 Lambda Expressions 227

  4.9.2 Extension Methods 229

  4.9.3 Implicitly Typed Local Variables 232

  4.9.4 Query Expressions 234

  4.10 Chapter Summary 236 Homework 237

  

Chapter 5 Data Selection Query with Visual Basic.NET 241

Part I Data Query with Visual Studio.NET Design Tools and Wizards 242

  5.1 A Completed Sample Database Application Example 242

  5.2 Visual Studio.NET Design Tools and Wizards 245

  5.2.1 Data Components in the Toolbox Window 245

  5.2.1.1 The DataSet 246

  5.2.1.2 DataGridView 247

  5.2.1.3 BindingSource 248

  5.2.1.4 BindingNavigator 248

  5.2.1.5 TableAdapter 249

  5.2.2 Data Source Window 249

  5.2.2.1 Add New Data Sources 250

  5.2.2.2 Data Source Confi guration Wizard 251

  5.2.2.3 DataSet Designer 255

  5.3 Query Data from SQL Server Database Using Design Tools and Wizards 257

  5.3.1 Application User Interface 257

  5.3.1.1 The LogIn Form 258

  5.3.1.2 The Selection Form 259

  5.3.1.3 The Faculty Form 260

  5.3.1.4 The Course Form 260

  5.3.1.5 The Student Form 263

  5.4 Add and Utilize Visual Studio Wizards and Design Tools 265

  5.4.1 Add and Confi gure a New Data Source 265

  5.5 Query and Display Data using the DataGridView Control 268

  5.5.1 View the Entire Table 268

  5.5.2 View Each Record or the Specifi ed Columns 270

  5.6 Use DataSet Designer to Edit the Structure of the DataSet 272

  5.7 Bind Data to the Associated Controls in LogIn Form 274

  5.8 Develop Codes to Query Data Using the Fill() Method 278

  5.9 Use Return a Single Value to Query Data for LogIn Form 281

  5.10 Develop the Codes for the Selection Form 284

  5.11 Query Data from the Faculty Table for the Faculty Form 286

  5.12 Develop Codes to Query Data from the Faculty Table 289

  

5.12.1 Develop Codes to Query Data Using the TableAdapter Method 289

  

5.12.2 Develop Codes to Query Data Using the LINQ to DataSet Method 291

  5.13 Display a Picture for the Selected Faculty 292

  5.13.1 Modify the Codes for the Select Button Event Procedure 292

  5.13.2 Create a Function to Select the Matched Faculty Image 293

  Contents

  5.14 Query Data from the Course Table for the Course Form 295

  5.14.1 Build the Course Queries Using the Query Builder 296

  

5.14.2 Bind Data Columns to the Associated Controls in the Course Form 298

  5.15 Develop Codes to Query Data for the Course Form 300

  

5.15.1 Query Data from the Course Table Using the TableAdapter Method 300

  5.15.2 Query Data from the Course Table Using the LINQ to DataSet Method 302

  5.16 Query Data from Oracle Database Using Design Tools and Wizards 304

  5.16.1 Introduction to dotConnect for Oracle 6.30 Express 305

  5.16.2 Create a New Visual Basic.NET Project: SelectWizardOracle 305

  5.16.3 Select and Add Oracle Database 11g XE as the Data Source 307

  5.16.4 Modify the Codes to Access the Oracle Database 310

  Part II Data Query with Runtime Objects 311

  5.17 Introduction to Runtime Objects 312

  5.17.1 Procedure of Building a Data-Driven Application Using Runtime Object 314

  5.18 Query Data from Microsoft Access Database Using Runtime Object 315

5.18.1 Query Data Using Runtime Objects for the LogIn Form 315

  5.18.1.1 Declare Global Variables and Runtime Objects 316

  

5.18.1.2 Connect to the Data Source with the Runtime Object 317

  5.18.1.3 Coding for Method 1: Using DataSet-TableAdapter to Query Data 318

  5.18.1.4 Coding for Method 2: Using the DataReader to Query Data 320

  5.18.1.5 Clean up the Objects and Terminate the Project 321

  5.18.2 Coding for the Selection Form 322

  5.18.3 Query Data Using Runtime Objects for the Faculty Form 323

  5.18.4 Query Data Using Runtime Objects for the Course Form 331

  5.18.5 Query Data Using Runtime Objects for the Student Form 339

  

5.18.5.1 Coding for the Student Form_Load Event Procedure 341

  

5.18.5.2 Coding for the Select Button Click Event Procedure 342

5.19 Query Data from SQL Server Database Using Runtime Object 349

  5.19.1 Migrating from Access to SQL Server and Oracle Databases 350

  5.19.2 Query Data Using Runtime Objects for the LogIn Form 353

  5.19.2.1 Declare the Runtime Objects 354

  

5.19.2.2 Connect to the Data Source with the Runtime Object 354

  5.19.2.3 Coding for Method 1: Using the TableAdapter to Query Data 356

  5.19.2.4 Coding for Method 2: Using the DataReader to Query Data 357

  5.19.3 The Coding for the Selection Form 359

  5.19.4 Query Data Using Runtime Objects For the Faculty Form 359

  5.19.5 Query Data Using Runtime Objects for the Course Form 362

  5.19.6 Retrieve Data from Multiple Tables Using Tables JOINS 363

  5.19.7 Query Data Using Runtime Objects for the Student Form 367

  Contents

5.19.8 Query Student Data Using Stored Procedures 369

  5.19.8.1 Create the Stored Procedure 370

  5.19.8.2 Call the Stored Procedure 371

  

5.19.8.3 Query Data Using Stored Procedures for Student Form 372

  

5.19.8.4 Query Data Using More Complicated Stored Procedures 380

5.20 Query Data from Oracle Database Using Runtime Object 384

  5.20.1 Install and Confi gure the Oracle Database 11g Express Edition 384

  5.20.2 Confi gure the Oracle Database Connection String 385

  5.20.3 Query Data Using Runtime Objects for the LogIn Form 386

  5.20.3.1 Declare the Runtime Objects and Modify the ConnModule 387

  

5.20.3.2 Connect to the Data Source with the Runtime Object 388

  5.20.3.3 Coding for Method 1: Using the TableAdapter to Query Data 389

  5.20.3.4 Coding for Method 2: Using the DataReader to Query Data 390

  5.20.4 The Coding for the Selection Form 392

  5.20.5 Query Data Using Runtime Objects for the Faculty Form 392

  5.20.6 Query Data Using Runtime Objects and LINQ to DataSet for the Course Form 396

  5.20.7 The Stored Procedures in Oracle Database Environment 397

  

5.20.7.1 The Syntax of Creating a Stored Procedure in the Oracle 398

  5.20.7.2 The Syntax of Creating a Package in the Oracle 398

  5.20.8 Create the Faculty_Course Package for the Course Form 400

  5.20.9 Query Data Using the Oracle Package For the Course Form 405

  5.21 Chapter Summary 411 Homework 413

Chapter 6 Data Inserting with Visual Basic.NET 417 Part I Data Inserting with Visual Studio.NET Design Tools and Wizards 418

6.1 Insert Data into a Database 418

  6.1.1 Insert New Records into a Database Using the TableAdapter.Insert Method 419

  6.1.2 Insert New Records into a Database Using the TableAdapter.Update Method 420

  6.2 Insert Data into the SQL Server Database Using a Sample Project InsertWizard 420

  6.2.1 Create New Project Based on the SelectWizard Project 421

  6.2.2 Application User Interfaces 421

  6.2.3 Validate Data Before the Data Insertion 421

  6.2.3.1 Visual Basic Collection and .NET Framework Collection Classes 422

6.2.3.2 Validate Data Using the Generic Collection 422

  6.2.4 Initialization Coding for the Data Insertion 425

  6.2.5 Build the Insert Query 426

  6.2.5.1 Confi gure the TableAdapter and Build the Data Inserting Query 426

  6.2.6 Develop Codes to Insert Data Using the TableAdapter.Insert Method 427

  Contents

  6.7.6.3 Modify the Codes in the Student Form 470

  6.7.3 Modify the LogIn Query Strings 464

  6.7.4 Modify the Faculty Query String and Query Related Codes 466

  6.7.5 Modify the Faculty Insert String and Insertion Related Codes 466

  6.7.6 Modifi cations to Other Forms 468

  6.7.6.1 Modify the Codes in the Selection Form 469

  6.7.6.2 Modify the Codes in the Course Form 469

  6.7.6.4 Modify the Codes in the SP Form 470

  6.7.1 Add the Oracle Driver Reference and Modify the Imports Commands 462

  6.8 Insert Data into the Database Using Stored Procedures 471

  6.8.1 Insert Data into the SQL Server Database Using Stored Procedures 471

  

6.8.1.1 Develop Stored Procedures of SQL Server Database 471

  6.8.1.2 Develop Codes to Call Stored Procedures to Insert Data into the Course Table 474

  6.8.2 Insert Data into the Oracle Database Using Stored Procedures 478

  6.8.2.1 Develop Stored Procedures in Oracle Database 479

  6.7.2 Modify the Database Connection String 463

  6.7 Insert Data into the Oracle Database Using the Runtime Objects 461

  6.2.7 Develop Codes to Insert Data Using the TableAdapter.Update Method 430

  

6.5.1 Insert Data into the Faculty Table for the SQL Server Database 444

  6.2.8 Insert Data into the Database Using the Stored Procedures 435

  

6.2.8.1 Create the Stored Procedure Using the TableAdapter Query

Confi guration Wizard 436

  6.2.8.2 Modify the Codes to Perform the Data Insertion Using the Stored Procedure 436

  6.3 Insert Data into the Oracle Database Using a Sample Project InsertWizardOracle 441 Part II Data Insertion with Runtime Objects 442

  6.4 The General Runtime Objects Method 442

  6.5 Insert Data into the SQL Server Database Using the Runtime Object Method 444

  

6.5.1.1 Develop the Codes to Insert Data into the Faculty Table 444

  6.6.6 Modifi cations to Other Forms 459

  6.6 Insert Data into the Microsoft Access Database Using the Runtime Objects 453

  6.6.1 Modify the Imports Commands and the ConnModule 454

  6.6.2 Modify the Database Connection String 454

  6.6.3 Modify the LogIn Query Strings 455

  6.6.4 Modify the Faculty Query String 456

  6.6.5 Modify the Faculty Insert String 458

  6.8.2.2 Develop Codes to Call Stored Procedures to Insert Data into the Course Table 483

  Contents

6.9 Insert Data into the Database Using the LINQ to DataSet Method 486

  6.9.1 Insert Data Into the SQL Server Database Using the LINQ to SQL Queries 488

  6.10 Chapter Summary 488 Homework 489

Chapter 7 Data Updating and Deleting with Visual Basic.NET 493 Part I Data Updating and Deleting with Visual Studio.NET Design Tools and Wizards 494

  7.1 Update or Delete Data Against Databases 495

  7.1.1 Updating and Deleting Data from Related Tables in a DataSet 495

  

7.1.2 Update or Delete Data Against Database Using TableAdapter DBDirect

Methods: TableAdapter.Update and TableAdapter.Delete 496

  7.1.3 Update or Delete Data Against Database Using TableAdapter.Update Method 497

  7.2 Update and Delete Data for Microsoft SQL Server Database 498

  7.2.1 Create a New Project Based on the InsertWizard Project 498

  7.2.2 Application User Interfaces 499

  7.2.3 Validate Data before the Data Updating and Deleting 499

  7.2.4 Build the Update and Delete Queries 499

  7.2.4.1 Confi gure the TableAdapter and Build the Data Updating Query 499

  7.2.4.2 Build the Data Deleting Query 500

  7.2.5 Develop Codes to Update Data Using the TableAdapter DBDirect Method 502

  7.2.5.1 Modifi cations of the Codes 502

  7.2.5.2 Creations of the Codes 502

  7.2.6 Develop Codes to Update Data Using the TableAdapter.Update Method 503

  7.2.7 Develop Codes to Delete Data Using the TableAdapter DBDirect Method 505

  7.2.8 Develop Codes to Delete Data Using the TableAdapter.Update Method 507

7.2.9 Validate the Data after the Data Updating and Deleting 508

  7.3 Update and Delete Data for Oracle Database 511

  7.4 Update and Delete Data for Microsoft Access Database 512

  Part II Data Updating and Deleting with Runtime Objects 512

  7.5 The Runtime Objects Method 513

  7.6 Update and Delete Data for SQL Server Database Using the Runtime Objects 514

  7.6.1 Update Data Against the Faculty Table for the SQL Server Database 515

  7.6.1.1 Develop Codes to Update the Faculty Data 515

  7.6.1.2 Validate the Data Updating 516

  Contents

  

7.6.2 Delete Data from the Faculty Table for the SQL Server Database 517

  7.6.2.1 Develop Codes to Delete Data 517

  7.6.2.2 Validate the Data Deleting 518

7.7 Update and Delete Data for Oracle Database Using the Runtime Objects 520

  7.7.1 Add the Oracle Namespace Reference and Modify the Imports Command 521

  7.7.2 Modify the Connection String and Query String for the LogIn Form 522

  7.7.2.1 Modify the Connection String in the Form Load Event Procedure 522

  7.7.2.2 Modify the SELECT Query String in the TabLogIn Button Event Procedure 522

  7.7.2.3 Modify the SELECT Query String in the ReadLogIn Button Event Procedure 523

  7.7.3 Modify the Query Strings for the Faculty Form 523

  

7.7.3.1 Modify the SELECT Query String for the Select Button Event

Procedure 523

  

7.7.3.2 Modify the INSERT Query String for the Insert Button Event

Procedure 523

  7.7.3.3 Modify the UPDATE Query String for the Update Button Event Procedure 524

  7.7.3.4 Modify the DELETE Query String for the Delete Button Event Procedure 524

  7.7.4 Modify the Query Strings for the Course Form 524

  7.7.4.1 Modify the SELECT Query String for the Select Button Event Procedure 525

  7.7.4.2 Modify the SELECT Query String for the CourseList Event Procedure 525

  7.7.5 Modify the Query Strings for the Student Form 525

  7.7.6 Other Modifi cations 525

7.8 Update and Delete Data Against Database Using Stored Procedures 528

  7.8.1 Update and Delete Data Against SQL Server Database Using Stored Procedures 528

  

7.8.1.1 Modify the Existing Project to Create Our New Project 529

  7.8.1.2 Modify the Codes to Update and Delete Data from the Faculty Table 529

  7.8.1.3 Develop Two Stored Procedures in the SQL Server Database 531

  7.8.1.4 Call the Stored Procedures to Perform the Data Updating and Deleting 537

  7.8.2 Update and Delete Data Against Oracle Database Using Stored Procedures 538

  

7.8.2.1 Modify the Existing Project to Create Our New Project 538

  7.8.2.2 Modify the Codes to Update and Delete Data from the Faculty Table 539

  7.8.2.3 Develop Stored Procedures in the Oracle Database 542

  7.8.2.4 Call the Stored Procedure to Perform the Data Updating and Deleting 546

  Contents

Chapter 8 Accessing Data in ASP.NET 559

  

8.3.10.2 Coding for the Select Button’s Click Event Procedure 592

  8.3.8 Develop the Codes to Select the Desired Faculty Information 580

  

8.3.8.1 Develop the Codes for the Page_Load Event Procedure 581

  8.3.8.2 Develop the Codes for the Select Button Event Procedure 582

  8.3.8.3 Develop the Codes for Other Procedures 583

  8.3.10.1 Coding for the Course Page Loading and Ending Event Procedures 591

  

8.3.10.4 Coding for Other User-Defi ned Subroutine Procedures 595

  

8.3.10.3 Coding for the SelectedIndexChanged Event Procedure of the

CourseList Box 594

  7.8.3 Update and Delete Data Against Databases Using the LINQ to SQL Query 548

  8.4 Develop ASP.NET Web Application to Insert Data into SQL Server Databases 598

  8.4.1 Develop the Codes to Perform the Data Insertion Function 598

  

8.4.2 Develop the Codes for the Insert Button Click Event Procedure 599

  8.4.3 Modify the Codes in the Subroutine ShowFaculty() for the Data Validation 601

  8.4.4 Validate the Data Insertion 602

  8.3.7 Create the Third User Interface: Faculty Page 578

  8.3.6 Modify the Codes in the LogIn Page to Transfer to the Selection Page 577

  8.3.5 Develop the Codes to Open the Other Page 576

  8.3.4 Create the Second User Interface: Selection Page 574

  7.8.3.1 Create a New Object of the DataContext Class 549

  7.8.3.2 Develop the Codes for the Select Button Click Event Procedure 550

  7.8.3.3 Develop the Codes for the Update Button Click Event Procedure 551

  7.8.3.4 Develop the Codes for the Delete Button Click Event Procedure 552

  7.9 Chapter Summary 554 Homework 555

  8.1 What Is the .NET Framework? 560

  8.2 What Is ASP.NET? 561

  8.2.1 ASP.NET Web Application File Structure 563

  8.2.2 ASP.NET Execution Model 563

  8.2.3 What Really Happens When a Web Application Is Executed? 564

  8.2.4 The Requirements to Test and Run the Web Project 565

  8.3 Develop ASP.NET Web Application to Select Data from SQL Server Databases 566

  8.3.1 Create the User Interface: LogIn Form 567

  

8.3.2 Develop the Codes to Access and Select Data from the Database 569

  8.3.3 Validate the Data in the Client Side 573

8.3.9 Create the Fourth User Interface: Course Page 587

8.3.9.1 The AutoPostBack Property of the List Box Control 589

8.3.10 Develop the Codes to Select the Desired Course Information 590

  Contents

  8.5 Develop Web Applications to Update and Delete Data in SQL Server Databases 604

  8.5.1 Modify the Codes for the Faculty Page 605

  

8.5.2 Develop the Codes for the Update Button Click Event Procedure 606

  

8.5.3 Develop the Codes for the Delete Button Click Event Procedure 609

  8.5.3.1 Relationships between Five Tables in Our Sample Database 609

  8.5.3.2 Data Deleting Sequence 610

  8.5.3.3 Use the Cascade Deleting Option to Simplify the Data Deleting 611

  8.5.3.4 Create the Stored Procedure to Perform the Data Deleting 612

  

8.5.3.5 Develop the Codes to Call the Stored Procedure to Perform

the Data Deleting 616

8.6 Develop ASP.NET Web Applications with LINQ to SQL Query 618

  8.6.1 Add an Existing Web Page FacultyLINQ.aspx 620

  8.6.2 Create a New Object of the DataContext Class 621

  8.6.3 Develop the Codes for the Data Selection Query 622

  8.6.4 Develop the Codes for the Data Insertion Query 623

  

8.6.5 Develop the Codes for the Data Updating and Deleting Queries 625

  8.7 Develop ASP.NET Web Application to Select Data from Oracle Databases 628

  8.7.1 Add the Oracle Database Reference and Modify Imports Commands 629

  8.7.2 Modify the Connection String in the LogIn Page 629

  8.7.3 Modify the Query String in the LogIn Page 630

  8.7.4 Modify the Query String in the Faculty Page 631

  8.7.5 Modify the Query Strings in the Course Page 633

  8.7.6 Modify the Global Connection Object in the Selection Page 636

8.8 Develop ASP.NET Web Application to Insert Data into Oracle Databases 636

8.8.1 Create the Codes for the Insert Button Click Event Procedure 637

  

8.8.2 Create the Codes for the TextChanged Event Procedure of the Faculty ID

Textbox 639

  8.8.3 Modify the Codes in the Subroutine ShowFaculty() for the Data Validation 639

  8.9 Develop ASP.NET Web Application to Update and Delete Data in Oracle Databases 642

  8.9.1 Build the Codes for the Project to Perform the Data Updating 642

8.9.1.1 Modifi cations to the Select Button’s Click Event Procedure 642

  8.9.1.2 Add the Codes to the Update Button Event and UpdateParameters Procedures 643

8.9.2 Develop Stored Procedures to Perform the Data Deleting 646

  8.9.2.1 Delete an Existing Record from the Faculty Table 646

  8.9.2.2 Develop the Codes for the Delete Button’s Event Procedure 647

  8.9.2.3 Validate the Data Deleting Actions 649

  8.9.2.4 The Constraint Property: On Delete Cascade in the Data Table 650

  8.10 Chapter Summary 653 Homework 654

  Contents

  Chapter 9 ASP.NET Web Services 657

  9.1 What Are Web Services and Their Components? 658

  9.2 Procedures to Build a Web Service 659

  9.2.1 The Structure of a Typical Web Service Project 660

  9.2.2 The Real Considerations When Building a Web Service Project 660

  9.2.3 Introduction to Windows Communication Foundation (WCF) 661

  9.2.3.1 What Is WCF? 662

  9.2.3.2 WCF Data Services 662

  9.2.3.3 WCF Services 663

  9.2.3.4 WCF Clients 663

  9.2.3.5 WCF Hosting 664

  9.2.3.6 WCF Visual Studio Templates 664

  9.2.4 Procedures to Build an ASP.NET Web Service 665

  9.3 Build ASP.NET Web Service Project to Access SQL Server Database 666

  9.3.1 Files and Items Created in the New Web Service Project 667

  9.3.2 A Feeling of the Hello World Web Service Project As it Runs 671

  9.3.3 Modify the Default Namespace 674

  

9.3.4 Create a Base Class to Handle Error Checking for Our Web Service 675

  9.3.5 Create the Real Web Service Class 676

  9.3.6 Add Web Methods into Our Web Service Class 677

  9.3.7 Develop the Codes for Web Methods to Perform the Web Services 678

  9.3.7.1 Web Service Connection Strings 678

  9.3.7.2 Modify the Existing HelloWorld Web Method 680

  

9.3.7.3 Develop the Codes to Perform the Database Queries 682

  9.3.7.4 Develop the Codes for Subroutines Used in the Web Method 684

  9.3.8 Develop the Stored Procedure to Perform the Data Query 687

  9.3.8.1 Develop the Stored Procedure WebSelectFacultySP 687

  

9.3.8.2 Add Another Web Method to Call the Stored Procedure 688

  9.3.9 Use DataSet as the Returning Object for the Web Method 689

  9.3.10 Build Windows-Based Web Service Clients to Consume the Web Services 692

  9.3.10.1 Create a Web Service Proxy Class 693

  

9.3.10.2 Develop the Graphic User Interface for the Windows-Based

Client Project 695

  9.3.10.3 Develop the Code to Consume the Web Service 696

  

9.3.11 Build Web-Based Web Service Clients to Consume the Web Service 703

  9.3.11.1 Create a New Web Site Project and Add an Existing Web Page 704

  9.3.11.2 Add a Web Service Reference and Modify the Web Form Window 704

  

9.3.11.3 Modify the Codes for the Related Event Procedures 706

  9.3.12 Deploy the Completed Web Service to Production Servers 710

  9.3.12.1 Copy Web Service Files to the Virtual Directory 712

  9.3.12.2 Publish Precompiled Web Service 713

  Contents

9.4.5 Build Web-Based Web Service Clients to Consume the Web Services 749

  9.6.2 Add a New Web Reference to Our Client Project 784

  9.5.2.3 Modify the Web Method GetSQLInsertCourse to GetSQLCourseDetail 768

  9.5.2.4 Add a New Web Method SQLDeleteSP 769

  9.5.3 Develop Two Stored Procedures WebUpdateCourseSP and WebDeleteCourseSP 771

  

9.5.3.1 Develop the Stored Procedure WebUpdateCourseSP 771

  9.5.3.2 Develop the Stored Procedure WebDeleteCourseSP 774

  9.6.1 Modifi cations to the File Folder and Project Files 784

  9.6.3 Modify the Codes for the Different Event Procedures and Subroutines 786

  9.5.2.1 Modify the Web Method from SetSQLInsertSP to SQLUpdataSP 764

  9.6.3.1 Modify the Codes of the Form_Load Event Procedure and Form-Level Variables 786

  9.6.3.2 Modify the Codes for the Select Button Click Event Procedure and

Related User-defi ned Subroutine Procedures 787

  9.6.3.3 Remove the Insert Button Click Event Procedure 788

  9.6.3.4 Modify the Codes for the SelectedIndexChanged Event Procedure 788

  9.6.3.5 Develop the Codes for the Update Button Event Procedure 789

  9.6.3.6 Develop the Codes for the Delete Button Event Procedure 790

  9.5.2.2 Modify the Web Method GetSQLInsert to GetSQLCourse 766

  9.5.2 Guideline in Modifying Related Web Methods 764

  9.4 Build ASP.NET Web Service Project to Insert Data into SQL Server Database 714

  

9.4.3.4 Develop the Fourth Web Method GetSQLInsertCourse 729

  9.4.1 Modify an Existing Web Service Project 714

  9.4.2 Develop the Web Service Methods 715

  9.4.3 Develop and Modify the Codes for the Code-Behind Page 716

  9.4.3.1 Develop and Modify the First Web Method SetSQLInsertSP 716

  9.4.3.2 Develop the Second Web Method GetSQLInsert 721

  9.4.3.3 Develop the Third Web Method SQLInsertDataSet 725

  9.4.4 Build Windows-Based Web Service Clients to Consume the Web Services 734

  9.5.1 Modify an Existing Web Service Project 763

  9.4.4.1 Create a Windows-Based Consume Project and a Web Service Proxy Class 734

  9.4.4.2 Develop the Graphic User Interface for the Client Project 736

  9.4.4.3 Develop the Code to Consume the Web Service 738

  9.4.5.1 Create a New Web Site Project and Add an Existing Web Page 750

  9.4.5.2 Add a Web Service Reference and Modify the Web Form Window 750

  

9.4.5.3 Modify the Codes for the Related Event Procedures 752

  9.5 Build ASP.NET Web Service to Update and Delete Data for SQL Server Database 762

9.6 Build Windows-Based Web Service Clients to Consume the Web Services 783

  Contents

9.7 Build Web-Based Web Service Clients to Consume the Web Services 793

  9.7.1 Create a New Web Site Project and Add an Existing Web Page 794

  

9.7.2 Add a Web Service Reference and Modify the Web Form Window 794

  

9.7.3 Modify the Codes for the Related Event Procedures and Subroutines 796

  

9.7.3.1 Modify the Codes in the Page_Load Event Procedure 796

  9.7.3.2 Modify Codes in the Select Button Event Procedure and Related Subroutines 796

  9.7.3.3 Modify the Codes in the SelectedIndexChanged Event Procedure of the Course List Box Control and Related Subroutines 798

  9.7.3.4 Remove the Insert Button Click Event Procedure and the TextChanged

Event Procedure of the Course ID Textbox 799

  9.7.3.5 Develop Codes for the Update Button Click Event Procedure 799

  9.7.3.6 Develop Codes for the Delete Button Click Event Procedure 800

9.8 Build ASP.NET Web Service Project to Access Oracle Database 804

  9.8.1 Build a Web Service Project WebServiceOracleSelect 805

  9.8.2 Modify the Connection String 806

  9.8.3 Add Oracle Database References and Modify the Namespace Directories 806

  9.8.4 Modify the Web Method GetSQLSelect and Related Subroutines 807

  

9.8.5 Modify the Web Method GetSQLSelectSP and Related Subroutines 809