 2002 Prentice Hall. All rights reserved. 1 Chapter 19 – Database, SQL, and ADO.NET Outline 19.1 Introduction 19.2 Relational Database Model 19.3 Relational.

Slides:



Advertisements
Similar presentations
 2003 Prentice Hall, Inc. All rights reserved. Chapter 22 – Database: SQL, MySQL, DBI and ADO.NET Outline 22.1 Introduction 22.2 Relational Database Model.
Advertisements

 Pearson Education, Inc. All rights reserved Accessing Databases with JDBC.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
1 Introduction to Web Application Introduction to Data Base.
Chapter 14: Advanced Topics: DBMS, SQL, and ASP.NET
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
INFORMATION TECHNOLOGY IN BUSINESS AND SOCIETY SESSION 16 – SQL SEAN J. TAYLOR.
Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
Java Database Connectivity (JDBC). Introduction Database –Collection of data DBMS –Database management system –Storing and organizing data SQL –Relational.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 25 – Database: SQL, ADO and RDS Outline 25.1Introduction 25.2Relational Database Model 25.3Relational.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Chapter 11 Introduction to Database Processing. Class 11: Database Processing Use a Visual Studio Wizard to establish a database connection used to load.
Programming with Visual Basic.NET An Object-Oriented Approach  Chapter 8 Introduction to Database Processing.
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 13 Database Management Systems: Getting Data Together.
ASP.NET Programming with C# and SQL Server First Edition
Intro to JDBC To effectively use Java Data Base Connectivity we must understand: 1.Relational Database Management Systems (RDBMS) 2.JDBC Drivers 3.SQL.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
Chapter 10 Queries and Updating Part C. SQL Copyright 2005 Radian Publishing Co.
CHAPTER 7 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2003 Prentice Hall, Inc. All rights reserved. 1 Java Database Connectivity with JDBC TM.
CIS 270—Application Development II Chapter 25—Accessing Databases with JDBC.
ADO.NET Tools and Wizards. Slide 2 Data Sources Window (Introduction) Use the Data Sources window to Establish a connection Create bound control instances.
Pemrograman VisualMinggu …10… Page 1 MINGGU Ke Sepuluh Pemrograman Visual Pokok Bahasan: ADO.NET II Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Needs for Accessing Database To make your web site more dynamic and maintainable, you can display information on your web pages that are retrieved from.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 MySQL and JDBC.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Database: SQL and MySQL
CHAPTER EIGHT Accessing Data Processing Databases.
1 Databases November 15, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel, and Goldberg. Published.
 2004 Prentice Hall, Inc. All rights reserved. 1 Segment – 6 Web Server & database.
 2006 Pearson Education, Inc. All rights reserved Database, SQL and ADO.NET.
Database, SQL, and ADO.NET- Part 1 Session 11 Mata kuliah: M0874 – Programming II Tahun: 2010.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ch Determine the output displayed from procedures Lab sheet 10.4.
Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
CHAPTER 1 – INTRODUCTION TO ACCESS Aliya Farheen March 5, 2014.
A CCESSING D ATABASES WITH JDBC CH 24 C S 442: A DVANCED J AVA P ROGRAMMING.
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 22 - SQL, MySQL, DBI and ADO Outline 22.1 Introduction 22.2 Relational Database Model 22.3 Relational.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
LINQ to DATABASE-2.  Creating the BooksDataContext  The code combines data from the three tables in the Books database and displays the relationships.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
 2002 Prentice Hall. All rights reserved. 1 Chapter 19 – Database, SQL, and ADO.NET Outline 19.1 Introduction 19.2 Relational Database Model 19.3 Relational.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
ORDER BY Clause The result of a query can be sorted in ascending or descending order using the optional ORDER BY clause. The simplest form of.
Database, SQL and ADO.NET
Chapter 19 – Database, SQL, and ADO .NET
Database Programming in Java
 2012 Pearson Education, Inc. All rights reserved.
JDBC.
Databases Intro (from Deitel)
Chapter 22 - SQL, MySQL, DBI and ADO
Presentation transcript:

 2002 Prentice Hall. All rights reserved. 1 Chapter 19 – Database, SQL, and ADO.NET Outline 19.1 Introduction 19.2 Relational Database Model 19.3 Relational Database Overview: Books Database 19.4 Structured Query Language (SQL) Basic SELECT Query WHERE Clause ORDER BY Clause Merging Data from Multiple Tables: INNER JOIN Joining Data from Tables Authors, AuthorISBN, Titles and Publishers INSERT Statement UPDATE Statement DELETE Statement

 2002 Prentice Hall. All rights reserved. 2 Chapter 19 – Database, SQL, and ADO.NET Outline 19.5 ADO.NET Object Model 19.6 Programming with ADO.NET: Extracting Information from a DBMS Connecting to and Querying an Access Data Source Querying the Books Database 19.7 Programming with ADO.NET: Modifying a DBMS 19.8 Reading and Writing XML Files

 2002 Prentice Hall. All rights reserved Introduction Database: –Integrated collection of data –Database management system (DBMS) Provides mechanisms for storing and organizing data in a way that is consistent with database’s format Allows storage and access to database without knowledge of internal representation –Relational Databases most popular Use Structured Query Language to perform queries (search) and manipulate data Programming languages need an interface to interact with relational databases

 2002 Prentice Hall. All rights reserved Relational Database Model Logical representation of data: –Relationships can be considered without concern for physical structure of data Composed of tables –Rows called records –Columns called fields –Primary key: field that contains unique data Each record can be identified by at least one distinct value –New sets made from queries called result sets

 2002 Prentice Hall. All rights reserved Relational Database Model Fig. 19.1Relational-database structure of an Employee table. numbernamedepartmentsalarylocation 23603Jones New Jersey 24568Kerwin New Jersey 34589Larson Los Angeles 35761Myers Orlando 47132Neumann New Jersey 78321Stephens Orlando record/row field/column primary key

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Rule of Entity Integrity: every record must have a unique value in its primary-key field Compound Primary key: when a record has a unique key based on a combination of two fields Foreign key: –Field for which every entry has a unique value in another table and where the field in the other table is the primary key for that table –Rule of Referential Integrity: every foreign-key field value must appear in another table’s primary-key field –One to many relationship: A foreign key can appear many times in its own table, but only once as primary key in another table

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Fig. 19.2Result set formed by selecting Department and Location data from the Employee table. departmentlocation 413New Jersey 642Los Angeles 611Orlando

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Fig part 1

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Fig part 2

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Fig part 3

 2002 Prentice Hall. All rights reserved Relational Database Overview: Books Database Fig Table relationships in Books. AuthorISBN authorID isbn Authors authorID firstName lastName Publishers publisherID publisherName Titles isbn title editionNumber copyright publisherID imageFile price 1 ¥ ¥ ¥ 1 1

 2002 Prentice Hall. All rights reserved Structured Query Language (SQL) Used to request data (perform queries) and manipulate data

 2002 Prentice Hall. All rights reserved Structured Query Language (SQL)

 2002 Prentice Hall. All rights reserved Basic Select Query Extracts information from one or more tables in a database Format: –Basic: SELECT * FROM tableName –Example: SELECT * FROM Authors –* extracts all columns –To get specific fields use a comma separated list instead of *

 2002 Prentice Hall. All rights reserved Basic Select Query

 2002 Prentice Hall. All rights reserved Where Clause Used to specify certain criteria in a query Basic form: –SELECT * FROM tableName WHERE criteria Example: –SELECT * FROM Titles WHERE copyright > 1999 Can use LIKE clause –Used for pattern matching Uses wildcards –*: zero or more characters take its place –?: exactly one character takes its place

 2002 Prentice Hall. All rights reserved WHERE Clause

 2002 Prentice Hall. All rights reserved WHERE Clause

 2002 Prentice Hall. All rights reserved ORDER BY Clause Used to arrange results of a query –Can be ascending or descending order Uses ASC and DESC respectively Example: –SELECT authorID FROM Authors ORDER BY authorID ASC Can be used to sort by multiple fields

 2002 Prentice Hall. All rights reserved ORDER BY Clause

 2002 Prentice Hall. All rights reserved ORDER BY Clause

 2002 Prentice Hall. All rights reserved ORDER BY Clause

 2002 Prentice Hall. All rights reserved ORDER BY Clause

 2002 Prentice Hall. All rights reserved Merging Data from Multiple Tables: INNER JOIN INNER JOIN –Merges records from multiple tables into a single record –Tests for matching values in a common field –General Form: SELECT * FROM table1 INNER JOIN table2 ON table1.fieldName=table2.fieldName –Example: SELECT firstName, isbn FROM Authors INNER JOIN AuthorISBN ON Authors.authorID= AuthorISBN.authorID Fully-qualified names use the table name and dot operator followed by the field name

 2002 Prentice Hall. All rights reserved Merging Data from Multiple Tables: INNER JOIN

 2002 Prentice Hall. All rights reserved Joining Data from Tables Authors, AuthorISBN, Titles and Publishers Tables produced by INNER JOIN can be used as arguments for another INNER JOIN

 2002 Prentice Hall. All rights reserved Joining Data from Tables Authors, AuthorISBN, Titles and Publishers Fig TitleAuthor query of Books database. Join Publishers and Titles tables if the publisherID matches Join Authors and AuthorISBN if authorID matches Join two created tables if titlesISBN matches authorsISBN Sort new table by title

 2002 Prentice Hall. All rights reserved Joining Data from Tables Authors, AuthorISBN, Titles and Publishers Fig part 1

 2002 Prentice Hall. All rights reserved Joining Data from Tables Authors, AuthorISBN, Titles and Publishers Fig part 2

 2002 Prentice Hall. All rights reserved Insert Statement Inserts a new record into a table Form: INSERT INTO tableName(fieldName1) VALUES (value1) Values must match field names in order and type

 2002 Prentice Hall. All rights reserved INSERT Statement

 2002 Prentice Hall. All rights reserved UPDATE Statement Modifies data in a table Form: UPDATE tableName SET fieldName1 = value1 WHERE criteria

 2002 Prentice Hall. All rights reserved UPDATE Statement

 2002 Prentice Hall. All rights reserved DELETE Statement Removes data from a table Form: DELETE FROM tableName WHERE criteria

 2002 Prentice Hall. All rights reserved DELETE Statement

 2002 Prentice Hall. All rights reserved ADO.NET and Object Model Provides an API for accessing database systems programmatically DataSets act as caches: – Store data from source in local memory

 2002 Prentice Hall. All rights reserved Programming with ADO.NET: Extracting Information from a DBMS Examples that demonstrate how to connect to a database, query the database and display the results of the query

 2002 Prentice Hall. All rights reserved Connecting to and Querying an Access Data Source Retrieves data and stores it in a DataGrid

 2002 Prentice Hall. All rights reserved. Outline 44 TableDisplay.cs 1 // Fig : TableDisplay.cs 2 // Displays data from a database table. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; // Summary description for TableDisplay.cs. 12 public class TableDisplay : System.Windows.Forms.Form 13 { 14 private System.Data.DataSet dataSet1; 15 private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; 16 private System.Windows.Forms.DataGrid dataGrid1; 17 private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; 18 private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; 19 private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; 20 private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; 21 private System.Data.OleDb.OleDbConnection oleDbConnection1; private System.ComponentModel.Container components = null; public TableDisplay() 26 { 27 InitializeComponent(); // Fill dataSet1 with data 30 oleDbDataAdapter1.Fill( dataSet1, "Authors" ); // bind data in Users table in dataSet1 to dataGrid1 33 dataGrid1.SetDataBinding( dataSet1, "Authors" ); 34 } 35 Constructor to populate dataSet1Call method fill to retrieve data from database associated with oleDbConnection Bind DataGrid to data source

 2002 Prentice Hall. All rights reserved. Outline 45 TableDisplay.cs 36 private void InitializeComponent() 37 { 38 this.dataSet1 = new System.Data.DataSet(); 39 this.oleDbDataAdapter1 = 40 new System.Data.OleDb.OleDbDataAdapter(); 41 this.dataGrid1 = new System.Windows.Forms.DataGrid(); 42 this.oleDbSelectCommand1 = 43 new System.Data.OleDb.OleDbCommand(); 44 this.oleDbInsertCommand1 = 45 new System.Data.OleDb.OleDbCommand(); 46 this.oleDbUpdateCommand1 = 47 new System.Data.OleDb.OleDbCommand(); 48 this.oleDbDeleteCommand1 = 49 new System.Data.OleDb.OleDbCommand(); 50 this.oleDbConnection1 = 51 new System.Data.OleDb.OleDbConnection(); 52 ((System.ComponentModel.ISupportInitialize) 53 (this.dataSet1)).BeginInit(); 54 ((System.ComponentModel.ISupportInitialize) 55 (this.dataGrid1)).BeginInit(); 56 this.SuspendLayout(); 57 // 58 // dataSet1 59 // 60 this.dataSet1.DataSetName = "NewDataSet"; 61 this.dataSet1.Locale = 62 new System.Globalization.CultureInfo("en-US"); 63 // 64 // oleDbDataAdapter1 65 // 66 this.oleDbDataAdapter1.DeleteCommand = 67 this.oleDbDeleteCommand1; 68 this.oleDbDataAdapter1.InsertCommand = 69 this.oleDbInsertCommand1; Specify how oleDbDataAdapter deletes data Specify how oleDbDataAdapter inserts data

 2002 Prentice Hall. All rights reserved. Outline 46 TableDisplay.cs 70 this.oleDbDataAdapter1.SelectCommand = 71 this.oleDbSelectCommand1; 72 this.oleDbDataAdapter1.TableMappings.AddRange( 73 new System.Data.Common.DataTableMapping[] { 74 new System.Data.Common.DataTableMapping( 75 "Table", "Authors", 76 new System.Data.Common.DataColumnMapping[] { 77 new System.Data.Common.DataColumnMapping( 78 "authorID", "authorID"), 79 new System.Data.Common.DataColumnMapping( 80 "firstName", "firstName"), 81 new System.Data.Common.DataColumnMapping( 82 "lastName", "lastName")})}); 83 this.oleDbDataAdapter1.UpdateCommand = 84 this.oleDbUpdateCommand1; 85 // 86 // dataGrid1 87 // 88 this.dataGrid1.DataMember = ""; 89 this.dataGrid1.HeaderForeColor = 90 System.Drawing.SystemColors.ControlText; 91 this.dataGrid1.Location = 92 new System.Drawing.Point(16, 16); 93 this.dataGrid1.Name = "dataGrid1"; 94 this.dataGrid1.Size = new System.Drawing.Size(264, 248); 95 this.dataGrid1.TabIndex = 0; 96 // 97 // oleDbSelectCommand1 98 // 99 this.oleDbSelectCommand1.CommandText = 100 "SELECT authorID, firstName, lastName FROM Authors"; 101 this.oleDbSelectCommand1.Connection = 102 this.oleDbConnection1; Specify how oleDbDataAdapter selects data Specify how oleDbDataAdapter updates data

 2002 Prentice Hall. All rights reserved. Outline 47 TableDisplay.cs 103 // 104 // oleDbInsertCommand1 105 // 106 this.oleDbInsertCommand1.CommandText = 107 "INSERT INTO Authors(firstName, lastName) VALUES " "(?, ?)"; 109 this.oleDbInsertCommand1.Connection = 110 this.oleDbConnection1; 111 this.oleDbInsertCommand1.Parameters.Add( 112 new System.Data.OleDb.OleDbParameter("firstName", 113 System.Data.OleDb.OleDbType.VarWChar, 50, 114 "firstName")); 115 this.oleDbInsertCommand1.Parameters.Add( 116 new System.Data.OleDb.OleDbParameter("lastName", 117 System.Data.OleDb.OleDbType.VarWChar, 50, 118 "lastName")); 119 // 120 // oleDbUpdateCommand1 121 // 122 this.oleDbUpdateCommand1.CommandText = 123 "UPDATE Authors SET firstName = ?, lastName = ? WHERE" " (authorID = ?) AND (firstNam" "e = ? OR ? IS NULL AND firstName IS NULL) AND " "(lastName = ? OR ? IS NULL AND las" "tName IS NULL)"; 128 this.oleDbUpdateCommand1.Connection = 129 this.oleDbConnection1; 130 this.oleDbUpdateCommand1.Parameters.Add( 131 new System.Data.OleDb.OleDbParameter( 132 "firstName", 133 System.Data.OleDb.OleDbType.VarWChar, , "firstName")); 135 this.oleDbUpdateCommand1.Parameters.Add( 136 new System.Data.OleDb.OleDbParameter( 137 "lastName", Set connection property for oleDbUpdageCommand1 Set CommandText for oleDbUpdageCommand1

 2002 Prentice Hall. All rights reserved. Outline 48 TableDisplay.cs 138 System.Data.OleDb.OleDbType.VarWChar, 50, 139 "lastName")); 140 this.oleDbUpdateCommand1.Parameters.Add( 141 new System.Data.OleDb.OleDbParameter( 142 "Original_authorID", 143 System.Data.OleDb.OleDbType.Integer, 0, 144 System.Data.ParameterDirection.Input, false, 145 ((System.Byte)(10)), ((System.Byte)(0)), 146 "authorID", System.Data.DataRowVersion.Original, 147 null)); 148 this.oleDbUpdateCommand1.Parameters.Add( 149 new System.Data.OleDb.OleDbParameter( 150 "Original_firstName", 151 System.Data.OleDb.OleDbType.VarWChar, 50, 152 System.Data.ParameterDirection.Input, false, 153 ((System.Byte)(0)), ((System.Byte)(0)), 154 "firstName", System.Data.DataRowVersion.Original, 155 null)); 156 this.oleDbUpdateCommand1.Parameters.Add( 157 new System.Data.OleDb.OleDbParameter( 158 "Original_firstName1", 159 System.Data.OleDb.OleDbType.VarWChar, 50, 160 System.Data.ParameterDirection.Input, false, 161 ((System.Byte)(0)), ((System.Byte)(0)), 162 "firstName", System.Data.DataRowVersion.Original, 163 null)); 164 this.oleDbUpdateCommand1.Parameters.Add( 165 new System.Data.OleDb.OleDbParameter( 166 "Original_lastName", 167 System.Data.OleDb.OleDbType.VarWChar, 50, 168 System.Data.ParameterDirection.Input, false, 169 ((System.Byte)(0)), ((System.Byte)(0)), 170 "lastName", System.Data.DataRowVersion.Original, 171 null));

 2002 Prentice Hall. All rights reserved. Outline 49 TableDisplay.cs 172 this.oleDbUpdateCommand1.Parameters.Add( 173 new System.Data.OleDb.OleDbParameter( 174 "Original_lastName1", 175 System.Data.OleDb.OleDbType.VarWChar, 50, 176 System.Data.ParameterDirection.Input, false, 177 ((System.Byte)(0)), ((System.Byte)(0)), 178 "lastName", System.Data.DataRowVersion.Original, 179 null)); 180 // 181 // oleDbDeleteCommand1 182 // 183 this.oleDbDeleteCommand1.CommandText = 184 "DELETE FROM Authors WHERE (authorID = ?) AND " "(firstName = ? OR ? IS NULL AND firs" "tName IS NULL) AND (lastName = ? OR ? IS NULL AND " "lastName IS NULL)"; 188 this.oleDbDeleteCommand1.Connection = 189 this.oleDbConnection1; 190 this.oleDbDeleteCommand1.Parameters.Add( 191 new System.Data.OleDb.OleDbParameter( 192 "Original_authorID", 193 System.Data.OleDb.OleDbType.Integer, 0, 194 System.Data.ParameterDirection.Input, false, 195 ((System.Byte)(10)), ((System.Byte)(0)), 196 "authorID", System.Data.DataRowVersion.Original, 197 null)); 198 this.oleDbDeleteCommand1.Parameters.Add( 199 new System.Data.OleDb.OleDbParameter( 200 "Original_firstName", 201 System.Data.OleDb.OleDbType.VarWChar, 50, 202 System.Data.ParameterDirection.Input, false, 203 ((System.Byte)(0)), ((System.Byte)(0)), 204 "firstName", System.Data.DataRowVersion.Original, 205 null));

 2002 Prentice Hall. All rights reserved. Outline 50 TableDisplay.cs 206 this.oleDbDeleteCommand1.Parameters.Add( 207 new System.Data.OleDb.OleDbParameter( 208 "Original_firstName1", 209 System.Data.OleDb.OleDbType.VarWChar, 50, 210 System.Data.ParameterDirection.Input, false, 211 ((System.Byte)(0)), ((System.Byte)(0)), 212 "firstName", System.Data.DataRowVersion.Original, 213 null)); 214 this.oleDbDeleteCommand1.Parameters.Add( 215 new System.Data.OleDb.OleDbParameter( 216 "Original_lastName", 217 System.Data.OleDb.OleDbType.VarWChar, 50, 218 System.Data.ParameterDirection.Input, false, 219 ((System.Byte)(0)), ((System.Byte)(0)), 220 "lastName", System.Data.DataRowVersion.Original, 221 null)); 222 this.oleDbDeleteCommand1.Parameters.Add( 223 new System.Data.OleDb.OleDbParameter( 224 "Original_lastName1", 225 System.Data.OleDb.OleDbType.VarWChar, 50, 226 System.Data.ParameterDirection.Input, false, 227 ((System.Byte)(0)), ((System.Byte)(0)), 228 "lastName", System.Data.DataRowVersion.Original, 229 null)); 230 // 231 // oleDbConnection1 232 //

 2002 Prentice Hall. All rights reserved. Outline 51 TableDisplay.cs 233 this.oleDbConnection1.ConnectionString = + ID=Admin;Data Source=C:\Books\2001\csphtp1\" + " + None;Extended Properties="""";Jet OLEDB:" + database="""";Jet OLEDB:Registry " + OLEDB:Database Password="""";" + OLEDB:Engine Type=5;Jet OLEDB:Database " + Mode=1;Jet OLEDB:Global Partial Bulk " + OLEDB:Global Bulk Transactions=1;Jet " + Database Password="""";Jet OLEDB:" + System Database=False;Jet OLEDB:Encrypt " + OLEDB:Don't Copy Locale on " + OLEDB:Compact Without Replica " + OLEDB:SFP=False"; 248 // 249 // TableDisplay 250 // 251 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 252 this.ClientSize = new System.Drawing.Size(292, 273); 253 this.Controls.AddRange( 254 new System.Windows.Forms.Control[] { 255 this.dataGrid1}); 256 this.Name = "TableDisplay"; 257 this.Text = "TableDisplay"; 258 ((System.ComponentModel.ISupportInitialize) 259 (this.dataSet1)).EndInit(); 260 ((System.ComponentModel.ISupportInitialize) 261 (this.dataGrid1)).EndInit(); 262 this.ResumeLayout(false); 263 Initialize oleDbConnection, and specify path to database

 2002 Prentice Hall. All rights reserved. Outline 52 TableDisplay.cs Program Output 264 } // end of InitializeComponent [STAThread] 267 static void Main() 268 { 269 Application.Run( new TableDisplay() ); 270 } 271 }

 2002 Prentice Hall. All rights reserved Querying the Books Database Use SELECT statements on database and display results

 2002 Prentice Hall. All rights reserved. Outline 54 DisplayQueryResu lts.cs 1 // Fig : DisplayQueryResults.cs 2 // Displays the contents of the authors database. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; public class DisplayQueryResults : System.Windows.Forms.Form 12 { 13 private System.Data.OleDb.OleDbConnection oleDbConnection1; 14 private System.Data.DataSet dataSet1; 15 private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; 16 private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; 17 private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; 18 private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; 19 private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; 20 private System.Windows.Forms.TextBox queryTextBox; 21 private System.Windows.Forms.Button submitButton; 22 private System.Windows.Forms.DataGrid dataGrid1; 23 private System.ComponentModel.Container components = null; public DisplayQueryResults() 26 { InitializeComponent(); 29 } // Visual Studio.NET generated code 32

 2002 Prentice Hall. All rights reserved. Outline 55 DisplayQueryResu lts.cs 33 [STAThread] 34 static void Main() 35 { 36 Application.Run( new DisplayQueryResults() ); 37 } // perform SQL query on data 40 private void submitButton_Click( object sender, 41 System.EventArgs e ) 42 { 43 try 44 { 45 // set SQL query to what user 46 // input into queryTextBox 47 oleDbDataAdapter1.SelectCommand.CommandText = 48 queryTextBox.Text; // clear DataSet from previous operation 51 dataSet1.Clear(); // Fill data set with information that results 54 // from SQL query 55 oleDbDataAdapter1.Fill( dataSet1, "Authors" ); // bind DataGrid to contents of DataSet 58 dataGrid1.SetDataBinding( dataSet1, "Authors" ); 59 } catch ( System.Data.OleDb.OleDbException oleException ) 62 { 63 MessageBox.Show( "Invalid query" ); 64 } } // end of submitButton_Click 67 } When user clicks submit button, assign SELECT query string to OleDbDataAdapter’s SelectCommand property Place data from database into dataSet1 Bind DataGrid to data and display results

 2002 Prentice Hall. All rights reserved. Outline 56 DisplayQueryResu lts.cs Program Output

 2002 Prentice Hall. All rights reserved Programming with ADO.NET: Modifying a DBMS Example implements an address book –User can insert, locate and update records

 2002 Prentice Hall. All rights reserved. Outline 58 AddressBook.cs 1 // Fig : AddressBook.cs 2 // Using SQL statements to manipulate a database. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; public class AddressBook : System.Windows.Forms.Form 12 { 13 private System.Windows.Forms.TextBox faxTextBox; 14 private System.Windows.Forms.TextBox homeTextBox; 15 private System.Windows.Forms.TextBox firstTextBox; 16 private System.Windows.Forms.TextBox stateTextBox; 17 private System.Windows.Forms.TextBox idTextBox; 18 private System.Windows.Forms.TextBox lastTextBox; 19 private System.Windows.Forms.TextBox postalTextBox; 20 private System.Windows.Forms.TextBox addressTextBox; 21 private System.Windows.Forms.TextBox cityTextBox; 22 private System.Windows.Forms.TextBox countryTextBox; 23 private System.Windows.Forms.TextBox TextBox; 24 private System.Data.DataSet dataSet1; 25 private System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1; 26 private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; 27 private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; 28 private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; 29 private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; 30 private System.Data.OleDb.OleDbConnection oleDbConnection1; 31 private System.Windows.Forms.TextBox statusTextBox; 32 private System.Windows.Forms.Label addressLabel; 33 private System.Windows.Forms.Label cityLabel; 34 private System.Windows.Forms.Label stateLabel; 35 private System.Windows.Forms.Label idLabel;

 2002 Prentice Hall. All rights reserved. Outline 59 AddressBook.cs 36 private System.Windows.Forms.Label firstLabel; 37 private System.Windows.Forms.Label lastLabel; 38 private System.Windows.Forms.Label postalLabel; 39 private System.Windows.Forms.Label countryLabel; 40 private System.Windows.Forms.Label Label; 41 private System.Windows.Forms.Button clearButton; 42 private System.Windows.Forms.Button helpButton; 43 private System.Windows.Forms.Button findButton; 44 private System.Windows.Forms.Button addButton; 45 private System.Windows.Forms.Button updateButton; 46 private System.Windows.Forms.Label faxLabel; 47 private System.Windows.Forms.Label homeLabel; 48 private System.ComponentModel.Container components = null; public AddressBook() 51 { 52 InitializeComponent(); 53 oleDbConnection1.Open(); 54 } // Visual Studio.NET generated code [STAThread] 59 static void Main() 60 { 61 Application.Run( new AddressBook() ); 62 } private void findButton_Click( object sender, 65 System.EventArgs e ) 66 { 67 try 68 { Perform SELECT query on database

 2002 Prentice Hall. All rights reserved. Outline 60 AddressBook.cs 69 if ( lastTextBox.Text != "" ) 70 { 71 // clear DataSet from last operation 72 dataSet1.Clear(); // create SQL query to find contact with 75 // specified last name 76 oleDbDataAdapter1.SelectCommand.CommandText = 77 "SELECT * FROM addresses WHERE lastname = '" + 78 lastTextBox.Text + "'"; // fill dataSet1 with rows resulting from 81 // query 82 oleDbDataAdapter1.Fill( dataSet1 ); // display information 85 Display( dataSet1 ); 86 statusTextBox.Text += "\r\nQuery successful\r\n"; 87 } 88 else 89 lastTextBox.Text = 90 "Enter last name here then press Find"; 91 } catch ( System.Data.OleDb.OleDbException oleException ) 94 { 95 Console.WriteLine( oleException.StackTrace ); 96 statusTextBox.Text += oleException.ToString(); 97 } 98 Empty DataSet of prior data Modify SQL query’s text to perform SELECT operation Fill DataSet with query results Display data in textboxes

 2002 Prentice Hall. All rights reserved. Outline 61 AddressBook.cs 99 catch ( InvalidOperationException invalidException ) 100 { 101 MessageBox.Show( invalidException.Message ); 102 } } // end of findButton_Click private void addButton_Click( object sender, System.EventArgs e ) 107 { 108 try 109 { 110 if ( lastTextBox.Text != "" && firstTextBox.Text != "" ) 111 { 112 // create SQL query to insert row 113 oleDbDataAdapter1.InsertCommand.CommandText = 114 "INSERT INTO addresses (" "firstname, lastname, address, city, " "stateorprovince, postalcode, country, " " address, homephone, faxnumber" ") VALUES ('" firstTextBox.Text + "', '" lastTextBox.Text + "', '" addressTextBox.Text + "', '" cityTextBox.Text + "', '" stateTextBox.Text + "', '" postalTextBox.Text + "', '" countryTextBox.Text + "', '" TextBox.Text + "', '" homeTextBox.Text + "', '" faxTextBox.Text + "')"; // notify user that query is being sent 131 statusTextBox.Text += "\r\nSending query: " oleDbDataAdapter1.InsertCommand.CommandText "\r\n" ; Perform INSERT operation using OleDbCommand members Sets CommandText property of InsertCommand to execute proper INSERT statement

 2002 Prentice Hall. All rights reserved. Outline 62 AddressBook.cs // send query 136 oleDbDataAdapter1.InsertCommand.ExecuteNonQuery(); statusTextBox.Text += "\r\nQuery successful\r\n"; 139 } 140 else 141 statusTextBox.Text += "\r\nEnter at least first " "and last name then press Add\r\n"; 143 } catch ( System.Data.OleDb.OleDbException oleException ) 146 { 147 Console.WriteLine( oleException.StackTrace ); 148 statusTextBox.Text += oleException.ToString(); 149 } } // end of addButton_Click private void updateButton_Click( object sender, 154 System.EventArgs e ) 155 { 156 try 157 { 158 // make sure users have found record 159 // they wish to update 160 if ( idTextBox.Text != "" ) 161 { 162 // set SQL query to update all fields in 163 // table where id number matches id 164 // in idTextBox 165 oleDbDataAdapter1.UpdateCommand.CommandText = 166 "UPDATE addresses SET " "firstname ='" + firstTextBox.Text "', lastname='" + lastTextBox.Text + Perform UPDATE operation using OleDbCommand members Send inset query to perform INSERT operation Sets CommandText property of UpdateCommand to execute proper Update statement

 2002 Prentice Hall. All rights reserved. Outline 63 AddressBook.cs 169 "', address='" + addressTextBox.Text "', city='" + cityTextBox.Text "', stateorprovince='" + stateTextBox.Text "', postalcode='" + postalTextBox.Text "', country='" + countryTextBox.Text "', address='" + TextBox.Text "', homephone='" + homeTextBox.Text "', faxnumber='" + faxTextBox.Text "' WHERE id=" + idTextBox.Text; // notify user that query is being set 180 statusTextBox.Text += "\r\nSending query: " oleDbDataAdapter1.UpdateCommand.CommandText "\r\n"; // execute query 185 oleDbDataAdapter1.UpdateCommand.ExecuteNonQuery(); statusTextBox.Text += "\r\nQuery successful\r\n"; 188 } 189 else 190 statusTextBox.Text += "\r\nYou may only update " "an existing record. Use Find to locate the" "record, then modify the information and " "press Update.\r\n"; 194 } catch ( System.Data.OleDb.OleDbException oleException ) 197 { 198 Console.WriteLine( oleException.StackTrace ); 199 statusTextBox.Text += oleException.ToString(); 200 } } // end of updateButton_Click 203 Send update query to perform UPDATE operation

 2002 Prentice Hall. All rights reserved. Outline 64 AddressBook.cs 204 private void clearButton_Click( object sender, 205 System.EventArgs e ) 206 { 207 idTextBox.Clear(); 208 ClearTextBoxes(); 209 } private void helpButton_Click( object sender, 212 System.EventArgs e ) 213 { 214 statusTextBox.AppendText( 215 "\r\nClick Find to locate a record\r\n" "Click Add to insert a new record.\r\n" "Click Update to update the information in a record " "\r\nClick Clear to empty the textboxes" ); 219 } private void Display( DataSet dataSet ) 222 { 223 try 224 { 225 // get first DataTable--there always will be one 226 DataTable dataTable = dataSet.Tables[ 0 ]; if ( dataTable.Rows.Count != 0 ) 229 { 230 int recordNumber = ( int ) dataTable.Rows[ 0 ][ 0 ]; idTextBox.Text = recordNumber.ToString(); 233 firstTextBox.Text = 234 ( string ) dataTable.Rows[ 0 ][ 1 ]; 235 lastTextBox.Text = 236 ( string ) dataTable.Rows[ 0 ][ 2 ]; 237 addressTextBox.Text = 238 ( string ) dataTable.Rows[ 0 ][ 3 ]; Updates display with data acquired by query DataTable from DataSet, contains results of query Determine if query returned any rows Retrieve first field of database Retrieve the rest of the fields of database Display instructions if user clicks help Clear textboxes if user clicks clear

 2002 Prentice Hall. All rights reserved. Outline 65 AddressBook.cs 239 cityTextBox.Text = 240 ( string ) dataTable.Rows[ 0 ][ 4 ]; 241 stateTextBox.Text = 242 ( string ) dataTable.Rows[ 0 ][ 5 ]; 243 postalTextBox.Text = 244 ( string ) dataTable.Rows[ 0 ][ 6 ]; 245 countryTextBox.Text = 246 ( string ) dataTable.Rows[ 0 ][ 7 ]; 247 TextBox.Text = 248 ( string ) dataTable.Rows[ 0 ][ 8 ]; 249 homeTextBox.Text = 250 ( string ) dataTable.Rows[ 0 ][ 9 ]; 251 faxTextBox.Text = 252 ( string ) dataTable.Rows[ 0 ][ 10 ]; 253 } else 256 statusTextBox.Text += "\r\nNo record found\r\n"; 257 } catch( System.Data.OleDb.OleDbException oleException ) 260 { 261 Console.WriteLine( oleException.StackTrace ); 262 statusTextBox.Text += oleException.ToString(); 263 } } // end Display private void ClearTextBoxes() 268 { 269 firstTextBox.Clear(); 270 lastTextBox.Clear(); 271 addressTextBox.Clear(); 272 cityTextBox.Clear(); 273 stateTextBox.Clear();

 2002 Prentice Hall. All rights reserved. Outline 66 AddressBook.cs Program Output 274 postalTextBox.Clear(); 275 countryTextBox.Clear(); 276 TextBox.Clear(); 277 homeTextBox.Clear(); 278 faxTextBox.Clear(); 279 } 280 }

 2002 Prentice Hall. All rights reserved. Outline 67 AddressBook.cs Program Output

 2002 Prentice Hall. All rights reserved. Outline 68 AddressBook.cs Program Output

 2002 Prentice Hall. All rights reserved. Outline 69 AddressBook.cs Program Output

 2002 Prentice Hall. All rights reserved Reading and Writing XML Files ADO.NET can convert data from data source into XML files –Uses methods: WriteXml, ReadXml and GetXml

 2002 Prentice Hall. All rights reserved. Outline 71 XMLWriter.cs 1 // Fig XMLWriter.cs 2 // Demonstrates generating XML from an ADO.NET DataSet. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; public class DatabaseXMLWriter : System.Windows.Forms.Form 12 { 13 private System.Data.OleDb.OleDbConnection baseballConnection; 14 private System.Data.OleDb.OleDbDataAdapter playersDataAdapter; 15 private System.Data.OleDb.OleDbCommand oleDbSelectCommand1; 16 private System.Data.OleDb.OleDbCommand oleDbInsertCommand1; 17 private System.Data.OleDb.OleDbCommand oleDbUpdateCommand1; 18 private System.Data.OleDb.OleDbCommand oleDbDeleteCommand1; 19 private System.Data.DataSet playersDataSet; 20 private System.Windows.Forms.DataGrid playersDataGrid; 21 private System.Windows.Forms.Button writeButton; 22 private System.Windows.Forms.TextBox outputTextBox; 23 private System.ComponentModel.Container components = null; public DatabaseXMLWriter() 26 { 27 // 28 // Required for Windows Form Designer support 29 // 30 InitializeComponent(); // open database connection 33 baseballConnection.Open(); 34 Create connection to database

 2002 Prentice Hall. All rights reserved. Outline 72 XMLWriter.cs 35 // fill DataSet with data from OleDbDataAdapter 36 playersDataAdapter.Fill( playersDataSet, "Players" ); // bind DataGrid to DataSet 39 playersDataGrid.SetDataBinding( playersDataSet, "Players" ); } // Visual Studio.NET generated code // main entry point for application. 46 [STAThread] 47 static void Main() 48 { 49 Application.Run( new DatabaseXMLWriter() ); 50 } // write XML representation of DataSet when button is clicked 53 private void writeButton_Click( 54 object sender, System.EventArgs e) 55 { 56 // write XML representation of DataSet to file 57 playersDataSet.WriteXml( "Players.xml" ); // display XML in TextBox 60 outputTextBox.Text += "Writing the following XML:\n\n" + 61 playersDataSet.GetXml() + "\n\n"; } 64 } Populate playersDataSet with data from database Bind DataGrid to DataSet to display data Create XML representation of data when user clicks button Append XML representation to textbox

 2002 Prentice Hall. All rights reserved. Outline 73 XMLWriter.cs

 2002 Prentice Hall. All rights reserved Reading and Writing XML Documents Fig XML document generated from DataSet in DatabaseXMLWriter.