Database, SQL and ADO.NET

Slides:



Advertisements
Similar presentations
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

 Open the Paradise.exe file  Application displays records in the Books database  Allows the store manager to enter an author’s name (or part of a name)
 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.
Chapter 18 - Data sources and datasets 1 Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Databases and LINQ Visual Basic 2010 How to Program 1.
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.
ASP.NET Programming with C# and SQL Server First Edition
 2008 Pearson Education, Inc. All rights reserved Database: SQL, MySQL, ADO.NET 2.0 and Java DB.
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.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Advanced Visual Basic th Edition Chapter 3: Using SQL Server Databases (c) 2007 Pearson Education Inc. All rights reserved. You may modify and copy.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
CHAPTER 8 Database: SQL, MySQL. Topics  Introduction  Relational Database Model  Relational Database Overview: Books.mdb Database  SQL (Structured.
Connecting to Data Sources Using ADO.NET Dr. Awad Khalil Computer Science & Engineering Department AUC.
Database: SQL and MySQL
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.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
1 Chapter 20 – Data sources and datasets Outline How to create a data source How to use a data source How to use Query Builder to build a simple query.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
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.
 2009 Pearson Education, Inc. All rights reserved Databases and LINQ to SQL.
Database Connectivity with ASP.NET. 2 Introduction Web pages commonly used to: –Gather information stored on a Web server database Most server-side scripting.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
CSCI 3327 Visual Basic Chapter 13: Databases and LINQ UTPA – Fall 2011.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
Programming with Microsoft Visual Basic 2012 Chapter 14: Access Databases and SQL.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
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.
ASP.NET Programming with C# and SQL Server First Edition
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Introduction to Database Processing with ADO.NET
Visual Basic 2010 How to Program
Relational Model.
Basic Database Concepts
 2012 Pearson Education, Inc. All rights reserved.
Exploring Microsoft Office Access
JDBC.
Databases Intro (from Deitel)
VB.NET Using Database.
The University of Texas – Pan American
Chapter 8 Working with Databases and MySQL
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
Chapter 22 - SQL, MySQL, DBI and ADO
CIS16 Application Programming with Visual Basic
Databases and Information Management
Database Applications
Chapter 10 Accessing Database Files
Presentation transcript:

Database, SQL and ADO.NET 20 Database, SQL and ADO.NET

OBJECTIVES In this chapter you will learn: The relational database model. To write basic database queries in SQL. To add data sources to projects. To use the IDE's drag-and-drop capabilities to display database tables in applications. To use the classes of namespaces System.Data and System.Data.SqlClient to manipulate databases. To use ADO.NET's disconnected object model to store data from a database in local memory. To create XML documents from data sources.

20.1 Introduction 20.2  Relational Databases 20.3  Relational Database Overview: Books Database 20.4  SQL 20.4.1 Basic SELECT Query 20.4.2 WHERE Clause 20.4.3 ORDER BY Clause 20.4.4 Merging Data from Multiple Tables: INNER JOIN 20.4.5 INSERT Statement 20.4.6 UPDATE Statement 20.4.7 DELETE Statement 20.5  ADO.NET Object Model

20.6 Programming with ADO.NET: Extracting Information from a Database 20.6.1 Displaying a Database Table in a DataGridView 20.6.2 How Data Binding Works 20.7 Querying the Books Database 20.8   Programming with ADO.NET: Address Book Case Study 20.9   Using a DataSet to Read and Write XML 20.10 Wrap-Up 20.11 Web Resources

20.1 Introduction Database Organized collection of data Database management system (DBMS) Provides mechanisms for storing, organizing, retrieving and modifying data SQL The international standard language used universally to perform queries and to manipulate data C# programs communicate with databases and manipulate their data through ADO.NET

20.2 Relational Databases Relational Databases Logical representation of data Allows the data to be accessed independently of its physical structure A relational database organizes data in tables Tables are composed of rows and columns in which values are stored Primary key A column in a table that requires a unique value that cannot be duplicated in other rows Guarantees that a primary key value can be used to uniquely identify a row Composite key A primary key that is composed of two or more columns

Fig. 20.1 | Employees table sample data.

Fig. 20.2 | Result of selecting distinct Department and Location data from the Employees table.

20.3 Relational Database Overview: Books Database SQL Server database files typically end with the .mdf (“master data file”) filename extension Foreign keys Can be specified when creating a table Helps maintain the Rule of Referential Integrity Every foreign key value must appear as another table’s primary key value Allow related data in multiple tables to be selected from those tables (joining the data)

20.3 Relational Database Overview: Books Database (Cont.) Entity-Relationship (ER) Diagram Shows the tables in the database and their relationships First compartment in each box contains the table’s name The names in italic font are primary keys A table’s primary key uniquely identifies each row in the table Every row must have a value in the primary key column, and the value of the key must be unique in the table Known as the Rule of Entity Integrity The lines connecting the tables represent the relationships among the tables The number on the end of these lines indicates a one-to-many relationship The line between the tables links the primary key to the matching foreign key

Fig. 20.3 | Authors table of the Books database.

Fig. 20.4 | Data from the Authors table of the Books database.

Fig. 20.5 | Titles table of the Books database.

Fig. 20.6 | Data from the Titles table of the Books database.

Fig. 20.7 | AuthorISBN table of the Books database.

Fig. 20.8 | Data from the AuthorISBN table of Books.

Common Programming Error 20.1 Not providing a value for every column in a primary key breaks the Rule of Entity Integrity and causes the DBMS to report an error.

Common Programming Error 20.2 Providing the same value for the primary key in multiple rows breaks the Rule of Entity Integrity and causes the DBMS to report an error.

Fig. 20.9 | Entity-relationship diagram for the Books database.

Common Programming Error 20.3 Providing a foreign-key value that does not appear as a primary-key value in another table breaks the Rule of Referential Integrity and causes the DBMS to report an error.

Fig. 20.10 | Common SQL keywords.

20.4.1 SELECT Query SELECT keyword “Selects” rows and columns from one or more tables in a database The basic form of a SELECT query is: SELECT * FROM tableName (*) indicates that all the columns from the tableName table should be retrieved The rows are not guaranteed to be returned in any particular order To retrieve only specific columns from a table, replace the asterisk (*) with a comma-separated list of the column names

20.4.2 WHERE Clause WHERE Clause Specify the selection criteria for the query The basic form of a query with selection criteria is: SELECT columnName1, columnName2, … FROM tableName WHERE criteria WHERE clause criteria can contain the relational operators <, >, <=, >=, = (equality), <> (inequality) and LIKE, as well as the logical operators AND, OR and NOT LIKE is used for pattern matching with wildcard characters Example: SELECT AuthorID, FirstName, LastName FROM Authors WHERE LastName LIKE 'D%'

Fig. 20.12 | Titles with copyright dates after 2004 from table Titles.

Fig. 20.11 | AuthorID and LastName data from the Authors table.

Fig. 20.13 | Authors from the Authors table whose last names start with D.

Fig. 20.14 | The only author from the Authors table whose last name contains h as the second letter.

20.4.3 ORDER BY Clause ORDER BY Clause Rows in the result of a query can be sorted into ascending or descending order SELECT columnName1, columnName2, … FROM tableName ORDER BY column ASC Multiple columns can be used for sorting with an ORDER BY clause of the form ORDER BY column1 sortingOrder, column2 sortingOrder, …

Fig. 20.15 | Authors from table Authors in ascending order by LastName.

Fig. 20.16 | Authors from table Authors in descending order by LastName.

20.4.4 Merging Data from Multiple Tables: INNER JOIN Normalize databases Split related data into separate tables Ensure that a database does not store redundant data Merge data from multiple tables into a single result AKA “joining the tables” Specified by an INNER JOIN operator in the query Merges rows from two tables by testing for matching values in a column that is common to the tables SELECT columnName1, columnName2, … FROM table1 INNER JOIN table2 ON table1.columnName = table2.columnName ON clause Specifies the columns from each table that are compared to determine which rows are merged

Fig. 20.17 | Data from Titles in descending order by Copyright and ascending order by Title.

Fig. 20.18 | Books from table Titles whose titles end with How to Program in ascending order by Title.

Common Programming Error 20.4 In a SQL query, failure to qualify names for columns that have the same name in two or more tables is an error.

20.4.5 INSERT Statement INSERT Statement Inserts a row into a table INSERT INTO tableName ( columnName1, columnName2, …, columnNameN VALUES ( value1, value2, …, valueN ) The tableName is followed by a comma-separated list of column names in ( ) List is not required if value for every column of the table in the correct order The list of column names is followed by the SQL keyword VALUES and a comma-separated list of values in ( ) Must match up with the columns specified after the table name in both order and type Always explicitly list the columns when inserting rows If the ordering changes, using only VALUES may cause an error Not every DBMS supports identity or auto incremented columns

Fig. 20.19 | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 1 of 2.)

Fig. 20.19 | Authors and ISBNs for their books in ascending order by LastName and FirstName. (Part 2 of 2.)

Common Programming Error 20.5 It is an error to specify a value for an identity column.

Common Programming Error 20.6 SQL uses the single-quote (') character to delimit strings. To specify a string containing a single quote (e.g., O’Malley) in a SQL statement, there must be two single quotes in the position where the single-quote character appears in the string (e.g., 'O''Malley'). The first of the two single-quote characters acts as an escape character for the second. Not escaping single-quote characters in a string that is part of a SQL statement is a syntax error.

20.4.6 UPDATE Statement UPDATE Statement Modifies data in a table UPDATE tableName SET columnName1 = value1, …, columnNameN = valueN WHERE criteria The tableName is followed by keyword SET and a comma-separated list of column name-value pairs in the format columnName = value WHERE clause Optional Provides criteria that determine which rows to update

Fig. 20.20 | Table Authors after an INSERT operation.

20.4.7 DELETE Statement DELETE Statement Removes rows from a table DELETE FROM tableName WHERE criteria WHERE clause Optional Specifies the criteria used to determine which rows to delete

Fig. 20.21 | Table Authors after an UPDATE operation.

Fig. 20.22 | Table Authors after a DELETE operation.

20.5 ADO.NET Object Model ADO.NET Provides an API for accessing database systems programmatically Replace Microsoft's ActiveX Data Objects (ADO) technology System.Data is the root namespace for the ADO.NET API Class DataTable Represents a table of data Contains a collection of DataRows and DataColumns System.Data.DataSet Consists of a set of DataTables and the relationships among them Represents a cache of data System.Data.OleDb Contains classes that are designed to work with any data source

20.5 ADO.NET Object Model (Cont.) System.Data.SqlClient Contains classes that are optimized to work with Microsoft SQL Server databases Class SqlConnection Represents a connection to a data source Keep tracks of the location of the data source and how it is accessed Class SqlCommand Represents a SQL command that a DBMS can execute on a database

20.5 ADO.NET Object Model (Cont.) ADO.NET’s Disconnected Model DataSets that is disconnected does not need a persistent connection to work with data Connect to the data source to populate the DataSet, and disconnects immediately afterwards Access and manipulate the data stored in the DataSet If necessary, reconnect to perform an update and then disconnect Class SqlDataAdapter Connects to a SQL data source Executes SQL statements to both populate and update the a DataSet Maintains a SqlConnection object that it opens and closes as needed to perform these operations using SqlCommands

20.6 Programming with ADO.NET: Extracting Information from a Database The IDE provides visual programming tools and wizards that simplify accessing data in your projects These tools establish database connections and create the ADO.NET to view and manipulate the data through GUI controls

20.6.1 Displaying a Database Table in a DataGridView DataGridView component Displays a data source in a GUI Displays data organized in rows and columns Connection string Specifies the path to a database file Determine how the database is accessed Adding a data source causes the IDE to generate a class derived from System.Data.DataSet Designed specifically to store data from that data source BindingNavigator component Provides several ways for users to browse and manipulate data displayed by another GUI control on a Form

20.6.1 Displaying a Database Table in a DataGridView (Cont.) Step 1: Create the Project Step 2: Add a Data Source to the Project (Fig. 20.23) Step 3: Choose the Data Source Type to Add to the Project (Fig. 20.24) Step 4: Add a New Database Connection (Fig. 20.25) Step 5: Choose the Books.mdf Data Connection (Fig. 20.26) Step 6: Save the Connection String (Fig. 20.27) Step 7: Select the Database Objects to Include in Your DataSet (Fig. 20.28) Step 8: View the Data Source in the Data Sources Window (Fig. 20.29) Step 9: View the Database in the Solution Explorer (Fig. 20.30)

Fig. 20.23 | Adding a data source to a project. Data Sources window Data menu Fig. 20.23 | Adding a data source to a project.

Fig. 20.24 | Choosing the data source type in the Data Source Configuration Wizard.

Fig. 20.25 | Adding a new data connection.

Fig. 20.26 | Choosing the Books.mdf data connection.

Fig. 20.27 | Saving the connection string to the application configuration file.

Fig. 20.28 | Choosing the database objects to include in the DataSet.

Fig. 20.29 | Viewing a data source listed in the Data Sources window.

Fig. 20.30 | Viewing a database listed in the Solution Explorer.

authorsBindingNavigator authorsDataGridView Component tray Fig. 20.31 | Design view after dragging the Authors data source node to the Form.

Fig. 20.32 | Displaying the Authors table in a DataGridView. Move to first row Move to next row Move to previous row Move to last row (a) Delete the current row Add a new row Save changes (b) Currently selected row Fig. 20.32 | Displaying the Authors table in a DataGridView.

20.6.2 How Data Binding Works Data Binding Technique in which GUI controls are connected to data sources Changes made through the application to the underlying data source will automatically be reflected in the data-bound control Modifying the data in the data-bound control and saving the changes updates the underlying data source The structure of an untyped DataSet is determined at runtime Based on the results of a specific query The structure of a strongly typed DataSet is known at compile time It is created specifically to store data from a particular data source A TableAdapter is responsible for interacting with a database on disk Encapsulates a SqlDataAdapter object Method Fill Retrieves information from the database and places it in the DataSet

20.6.2 How Data Binding Works (Cont.) BindingSource Identifies a data source that a program can bind to a control Serves as an intermediary between the data source and the corresponding data-bound GUI control Method EndEdit Applies changes made to data through a GUI control to the data source bound to that control

Fig. 20.33 | Data binding architecture used to display the Authors table of the Books database in a GUI.

Outline DisplayTable.cs (1 of 2) Ensure that the object’s associated data source is updated Write the modified version of the Authors table to the database

Outline DisplayTable.cs (2 of 2) Retrieve information from the database and place this information in the DataSet (a) (b)

20.7 Querying the Books Database Step 1: Create the Project Step 2: Add a Data Source to the Project Step 3: Create a DataGridView to Display the Titles Table Step 4: Add Custom Queries to the TitlesTableAdapter (Fig. 20.35) Step 5: Choose How the TableAdapter Should Access the Database (Fig. 20.36) Step 6: Choose a Query Type (Fig. 20.37) Step 7: Specify a SELECT Statement for the Query (Fig. 20.38) Step 8: Build a Query with Query Builder (Fig. 20.39) Step 9: Close the Query Builder (Fig. 20.40) Step 10: Set the Names of the Autogenerated Methods That Perform the Query (Fig. 20.41-2) Step 11: Add an Additional Query Step 12: Add a ComboBox to the Form Step 13: Customize the Form’s Load Event Handler Step 14: Program an Event Handler for the ComboBox (Fig. 20.43)

Fig. 20.35 | Viewing the BooksDataSet in the Dataset Designer. TitlesTableAdapter Fig. 20.35 | Viewing the BooksDataSet in the Dataset Designer.

Fig. 20.36 | TableAdapter Query Configuration Wizard to add a query to a TableAdapter.

Fig. 20.37 | Choosing the type of query to be generated for the TableAdapter.

Fig. 20.38 | Specifying a SELECT statement for the query.

(a) Fig. 20.39 | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 1 of 2.)

(b) Fig. 20.39 | Query Builder after adding a WHERE clause by entering a value in the Filter column. (Part 2 of 2.)

Fig. 20.40 | The SELECT statement created by the Query Builder.

Fig. 20.41 | Specifying names for the methods to be added to the TitlesTableAdapter.

Fig. 20.42 | Dataset Designer after adding Fill and Get methods to the TitlesTableAdapter.

Outline DisplayQuery Result.cs (1 of 5)

Outline DisplayQuery Result.cs (2 of 5)

Executes a SELECT query that selects all rows Outline DisplayQuery Result.cs (3 of 5) Executes a SELECT query that selects all rows Executes a SELECT query that selects all rows in which the copyright year is 2006 Executes a query that selects all rows that have “How To Program” at the end of their titles and sorts them in ascending order by title

Outline (b) (a) DisplayQuery Result.cs (4 of 5)

Outline (c) DisplayQuery Result.cs (5 of 5)

20.8 Programming with ADO.NET: Address Book Case Study Step 1: Add the Database to the Project Step 2: Indicate that the IDE Should Create a Set of Labels and TextBoxes to Display Each Row of Data (Fig. 20.45) Step 3: Drag the Addresses Data Source Node to the Form (Fig. 20.46) Step 4: Make the AddressID TextBox ReadOnly Step 5: Run the Application Step 6: Add a Query to the AddressesTableAdapter (Fig. 20.47) An @ symbol is used to indicate a parameter that will be replaced by a value when the query is executed Step 7: Add Controls to Allow Users to Specify a Last Name to Locate (Fig. 20.48) Step 8: Program an Event Handler That Locates the User-Specified Last Name Step 9: Allow the User to Return to Browsing All Rows in the Database

Outline AddressBook.cs (1 of 3)

Executes a SELECT query that selects all rows Outline AddressBook.cs (2 of 3) Executes a SELECT query that selects all rows in which the last name matches Executes a SELECT query that selects all rows

Outline AddressBook.cs (3 of 3) (b) (a) (c)

Fig. 20.45 | Selecting the control(s) to be created when dragging and dropping a data source member onto the Form.

Fig. 20.46 | Displaying a table on a Form using a series of Labels and TextBoxes.

Fig. 20.47 | Dataset Designer for the AddressBookDataSet after adding a query to AddressesTableAdapter.

Fig. 20.48 | Design view after adding controls to locate a last name in the address book.

Fig. 20. 49 | Viewing the DataBindings Fig. 20.49 | Viewing the DataBindings.Text property of a TextBox in the Properties window.

20.9 Using a DataSet to Read and Write XML ADO.NET has the ability to convert data stored in a data source to XML For exchanging data between applications in a portable format Class DataSet of namespace System.Data Create XML documents from data sources Convert data from XML into data sources Method WriteXml Writes the XML representation of the DataSet instance to a file Method ReadXml Reads the XML representation of the file into its own DataSet Method GetXml Returns a string containing the XML representation of a DataSet

Outline XMLWriter.cs (1 of 3)

Outline XMLWriter.cs (2 of 3) Set baseballDataSet’s Namespace property to specify a namespace for the DataSet Generates an XML representation of the data contained in the DataSet and then write the XML to the specified file

Returns a string containing the XML Outline Returns a string containing the XML XMLWriter.cs (3 of 3)

Outline Declares the document’s default namespace Players.xml Each Players element represents a record in the Players table