Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.

Slides:



Advertisements
Similar presentations
Data Base. Objective Become familiar with database terminology. Create a project to display data for a single database table. Use a DataGrid control.
Advertisements

Benchmark Series Microsoft Access 2010 Level 1
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Concepts of Database Management Seventh Edition
Concepts of Database Management Sixth Edition
Ch. 101 Virtual Table and DataGridView Virtual Table DataGridView.
Ch. 101 DataTable Data Table Object A DataTable object holds the contents of a table as a rectangular array. A data table is similar to a two-dimensional.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Introduction to Structured Query Language (SQL)
Ch. 101 Changing the Contents of a Database. Ch. 102 Changing the Contents of a Database Data grid views can also be used to add, modify, and delete records.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
ASP.NET Database Connectivity I. 2 © UW Business School, University of Washington 2004 Outline Database Concepts SQL ASP.NET Database Connectivity.
QUERYING A DATABASE By: Dr.Ennis-Cole. OBJECTIVES: Learn how to use the Query window in Design view Create, run and Save queries Define a relationship.
1 Chapter 2 Reviewing Tables and Queries. 2 Chapter Objectives Identify the steps required to develop an Access application Specify the characteristics.
Concepts of Database Management Sixth Edition
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
Chapter Extension 6 Using Microsoft Access © 2008 Pearson Prentice Hall, Experiencing MIS, David Kroenke.
CSCI 3328 Object Oriented Programming in C# Chapter 12: Databases and LINQ 1 Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
Microsoft Access 2010 Chapter 7 Using SQL.
Chapter 3 Maintaining a Database
Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
ACCESS CHAPTER 1. OBJECTIVES Tables Queries Forms Reports Primary and Foreign Keys Relationship.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
With Microsoft Office 2007 Intermediate© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Office 2007 Intermediate.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
With Microsoft Access 2007 Volume 1© 2008 Pearson Prentice Hall1 PowerPoint Presentation to Accompany GO! with Microsoft ® Access 2007 Volume 1 Chapter.
ASP.NET Programming with C# and SQL Server First Edition
Using the Select Case Statement and the MsgBox Function (Unit 8)
Copyright © 2008 Pearson Prentice Hall. All rights reserved.1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2: Relational Databases.
Ch VB 2008 by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
P RACTICE Determine the output displayed when the button is clicked. Dim actor(5,5) As String Private Sub btnDisplay_Click(...) Handles btnDisplay.Click.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Analyzing Data For Effective Decision Making Chapter 3.
1 Working with MS SQL Server Textbook Chapter 14.
1 By: Nour Hilal. Microsoft Access is a database software where data is stored in one or more Tables. A Database is a group of related Tables. Access.
Office Management Tools II Ms Saima Gul.  When you create your tables, you should assign each table a primary key—one or more fields whose contents are.
Concepts of Database Management Seventh Edition
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
Chapter 17 Creating a Database.
Access Project 3 Notes. Introduction Maintaining the Database  Modifying the data to keep it up-to-date Restructure the Database  To change the database.
Using Microsoft Access 56:150 Information System Design.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Ch Determine the output displayed from procedures Lab sheet 10.4.
Copyright 2007, Paradigm Publishing Inc. ACCESS 2007 Chapter 2 BACKNEXTEND 2-1 LINKS TO OBJECTIVES Creating Related Tables Creating Related Tables Determining.
Tutorial 91 Databases A database is an organized collection of related information stored in a file on a disk A database allows companies to store information.
1 CSE 2337 Introduction to Data Management Access Book – Ch 1.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Ch. 101 Database Management An Introduction to Databases.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Work with Tables and Database Records Lesson 3. NAVIGATING AMONG RECORDS Access users who prefer using the keyboard to navigate records can press keys.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
Ch VB 2005 by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
MSOffice Access Microsoft® Office 2010: Illustrated Introductory 1 Part 1 ® Database & Table.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Ch. 101 DataTable (Continue) Lab sheet 10.3: Form Display Cities table along with percentage growth.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
XP Chapter 1 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Level 2 Objectives: Understanding and Creating Table.
 2012 Pearson Education, Inc. All rights reserved.
Chapter 10 – Database Management
CIS16 Application Programming with Visual Basic
Presentation transcript:

Chapter 10 - VB.Net by Schneider1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Relational Databases and SQL

Chapter 10 - VB.Net by Schneider An Introduction to Databases The Server Explorer Accessing a Database with a Data Table

Chapter 10 - VB.Net by Schneider3 Database Terminology A table is a rectangular array of data. Each column of the table, called a field, contains the same type of information. Each row, called a record, contains all the information about one entry in the database. In a phone books, there are fields for name, address, and phone number. Your entry in the phone book is a record.

Chapter 10 - VB.Net by Schneider4 Database Management Software (DBMS) Used to create databases Databases can contain one or more related tables Examples of DBMS include Access and Oracle

Chapter 10 - VB.Net by Schneider5 The Server Explorer Allows the programmer to view information located on other computers Can also be used to view a database

Chapter 10 - VB.Net by Schneider6 Using the Server Explorer 1.Place your mouse over the “Server Explorer” tab on the left side of Visual Studio. 2.Right-click on “Data Connections”, and select “Add Connection”. 3.In the Data Link Properties window that appears, click on the “Provider” tab at the top. Select the item “Microsoft Jet 4.0 OLE DB Provider” from the OLE DB Providers List box, and then press the next button. 4.Click on the “...” button to the right of the first text box. This will open up a file,browser that allows you to locate any file then press Open. 5.Clear the contents of the “User name” text box.

Chapter 10 - VB.Net by Schneider7 Server Explorer continued 6.Press the Test Connection button. The message box stating “Test Connection Succeeded” will appear. Press the OK button on that message box, and then press the OK button on the Data Link Properties box. 7.An icon should appear in Server Explorer. Click on the + sign to the left of the icon to expand this entry. Three subentries will appear: Tables, Views, and Stored Procedures. 8.Expand the Tables entry to reveal the subentries. 9.Expand an entry to reveal the fields of the table. 10.Double-click on a table to show the table in a grid.

Chapter 10 - VB.Net by Schneider8 Figure 10.1 – Server Explorer

Chapter 10 - VB.Net by Schneider9 Figure 10.2 – The Cities Table

Chapter 10 - VB.Net by Schneider10 Accessing a Database with a Data Table A DataTable object holds the contents of a table as a rectangular array. A data table is similar to a two- dimensional array; it has rows and columns. The following declares a DataTable variable Dim dt As New DataTable()

Chapter 10 - VB.Net by Schneider11 Connecting with a DataTable Dim dt As New DataTable() Dim connStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = MEGACITIES.MDB " Dim sqlStr As String = "SELECT * FROM Cities" Dim dataAdapter As New _ OleDb.OleDbDataAdapter(sqlStr, connStr) dataAdapter.Fill(dt) dataAdapter.Dispose()

Chapter 10 - VB.Net by Schneider12 Properties of the DataTable After the six lines of code are executed, the number of records in the table is given by dt.Rows.Count The number of columns in the table is given by dt.Columns.Count The records are numbered 0 through dt.Rows.Count – 1 The fields are numbered 0 through dt.Columns.Count – 1

Chapter 10 - VB.Net by Schneider13 More Properties The name of the jth field is given by dt.Columns(j) The entry in the jth field of the ith record is dt.Rows(i)(j) The string containing the entry in the specified field of the ith record is dt.Rows(i)(fieldName)

Chapter 10 - VB.Net by Schneider14 Bound Controls A data table that is bound to a list box can transfer information automatically into the list box. The following statement binds a list box to a data table: lstBox.DataSource = dt The contents of a specified filed can be displayed in the list box by: lstBox.DisplayMember = "country"

Chapter 10 - VB.Net by Schneider Relational Databases and SQL Primary and Foreign Keys SQL Four SQL Requests The DataGrid Control

Chapter 10 - VB.Net by Schneider16 Primary Keys A primary key is used to uniquely identify each record. Databases of student enrollments in a college usually use a field of Social Security numbers as the primary key. Why wouldn't names would not be a good choice as a primary key?

Chapter 10 - VB.Net by Schneider17 Primary Key Fields When a database is then created, a field can be specified as a primary key. VB.NET will insist that every record have an entry in the primary-key field and that the same entry does not appear in two different records. If the user tries to enter a record with no data in the primary key, the error message “Index or primary key can’t contain a null record.” will be generated. If the user tries to enter a record with the same primary key data as another record, the error message “The changes you requested to the table were not successful…"

Chapter 10 - VB.Net by Schneider18 Two or More Tables When a database contains two or more tables, the tables are usually related. For instance, the two tables Cities and Countries are related by their country field. Notice that every entry in Cities.country appears uniquely in Countries.country and Countries.country is a primary key. We say that Cities.country is a foreign key of Countries.country.

Chapter 10 - VB.Net by Schneider19 Foreign Keys Foreign keys can be specified when a table is first created VB.NET will insist on the Rule of Referential Integrity. This Rule says that each value in the foreign key must also appear in the primary key of the other table.

Chapter 10 - VB.Net by Schneider20 Join A foreign key allows VB.NET to link (or join) together two tables from a relational database When the two tables Cities and Countries from MEGACITIES.MDB are joined based on the foreign key Cities.country, the result is Table The record for each city is expanded to show its country’s population and its currency.

Chapter 10 - VB.Net by Schneider21 SQL Structured Query Language developed for use with relational databases Very powerful language Allows for the request of specified information from a database Allows displaying information from database in a specific format

Chapter 10 - VB.Net by Schneider22 Four SQL Requests Show the records of a table in a specified order SELECT * FROM Table1 ORDER BY field1 ASC or SELECT * FROM Table1 ORDER BY field1 DESC Specifies ASCending Or DESCending

Chapter 10 - VB.Net by Schneider23 Show just the records that meet certain criteria SELECT * FROM Table1 WHERE criteria * means "all the fields" Name of the Table where the Records may be found Specified Criteria

Chapter 10 - VB.Net by Schneider24 Join the tables together connected by a foreign key, and present the records as in Requests I and II. SELECT * FROM Table1 INNER JOIN Table2 ON foreign field = primary field WHERE criteria

Chapter 10 - VB.Net by Schneider25 Make available just some of the fields of either the basic tables or the joined table. SELECT field1, field2,..., fieldN FROM Table1 WHERE criteria

Chapter 10 - VB.Net by Schneider26 Criteria Clause A string containing a condition of the type used with If blocks. Uses the standard operators, and = Also can use the operator Like. Like uses the wildcard characters “_” and “%” to compare a string to a pattern.

Chapter 10 - VB.Net by Schneider27 Like Examples An underscore character stands for a single character in the same position as the underscore character. The pattern “B_d” is matched by “Bid”, “Bud”, and “Bad”. A percent sign stands for any number of characters in the same position as the asterisk. The pattern “C%r” is matched by “Computer”, “Chair”, and “Car”.

Chapter 10 - VB.Net by Schneider28 SELECT clause SELECT fields FROM clause fields is either * (to indicate all fields) or a sequence of the fields to be available (separated by commas) clause is either a single table or a join of two tables

Chapter 10 - VB.Net by Schneider29 Join clause A join of two tables is indicated by a clause of the form table1 INNER JOIN table2 ON foreign key of table1=primary key of table2 Appending WHERE criteria to the end of the sentence restricts the records to those satisfying criteria. Appending ORDER BY field(s) ASC (or DESC) presents the records ordered by the specified field or fields.

Chapter 10 - VB.Net by Schneider30 General SQL statements SELECT www FROM xxx WHERE yyy ORDER BY zzz where SELECT www FROM xxx is always present May be accompanied by one or both of WHERE yyy and ORDER BY zzz. The xxx portion might contain an INNER JOIN phrase.

Chapter 10 - VB.Net by Schneider31 More on SQL statements The single quote, rather than the normal double quote, is used to surround strings. Fields may be specified with the table they come from by tableName.FieldName

Chapter 10 - VB.Net by Schneider32 Virtual Tables SQL statements create a new “virtual” table from existing tables. Think of the following statement as creating a "virtual table" SELECT city, pop2015 FROM Cities WHERE pop2015>=20 Results in: city pop2015 Tokyo 28.7 Sao Paulo 20.8 Bombay 27.4 Shanghai 23.4

Chapter 10 - VB.Net by Schneider33 Views “Virtual” tables don’t exist physically. For all practical purposes, VB.NET acts as if they did. You may also see a “virtual” table called a view.

Chapter 10 - VB.Net by Schneider34 The DataGrid Control The DataGrid, displays the values for an entire view in a table format identical to the table displayed by Server Explorer. The prefix for the name of a DataGrid control is dg. After a data table has been filled, the statement dgDisplay.DataSource = dt displays the contents of the data table dt in the data grid.

Chapter 10 - VB.Net by Schneider35 Example 1 Private Sub Form1_Load(...) Handles MyBase.Load UpdateDataGrid("Select * From Cities") End Sub Private Sub btnOrderbyPop_Click(...) Handles btnOrderbyPop.Click UpdateDataGrid("Select * From Cities Order By pop1995 ASC") End Sub Private Sub btnShowCurrency_Click(...) Handles btnShowCurrency.Click UpdateDataGrid("SELECT city, Cities.country, " & _ "Cities.pop1995, currency " & _ "FROM Cities INNER JOIN Countries " & _ "ON Cities.country=Countries.country " & _ "ORDER BY city ASC") End Sub

Chapter 10 - VB.Net by Schneider36 Example 1 continued Sub UpdateDataGrid(ByVal sqlStr As String) Dim dt As New DataTable() Dim connStr As String ="Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source = MEGACITIES.MDB" Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr) dataAdapter.Fill(dt) dataAdapter.Dispose() dgDisplay.DataSource = dt End Sub

Chapter 10 - VB.Net by Schneider37 Example 2 Private Sub btnFindCities_Click(...) Handles btnFindCities.Click UpdateDataGrid("SELECT city FROM Cities WHERE" & _ "country = '" & txtCountry.Text & _ "' ORDER BY city ASC") End Sub Sub UpdateDataGrid(ByVal sqlStr As String) 'Declare and populate the data table. Dim dt As New DataTable() Dim connStr As String = "Provider = " & _ "Microsoft.Jet.OLEDB.4.0;Data Source = " & _ "MEGACITIES.MDB"

Chapter 10 - VB.Net by Schneider38 Example 2 continued Dim dataAdapter As New _ OleDb.OleDbDataAdapter(sqlStr, connStr) dataAdapter.Fill(dt) dataAdapter.Dispose() 'Display the names of the cities in the specified 'country. If dt.Rows.Count = 0 Then MsgBox("No cities from that country " & _ "in the database") Else dgDisplay.DataSource = dt End If End Sub

Chapter 10 - VB.Net by Schneider39 Example 2 Output

Chapter 10 - VB.Net by Schneider40 Changing the Contents of a Database Data grids can also be used to add, modify, and delete records from a database. After a DataAdapter has been created, the statement Dim commandBuilder As New OleDbCommandBuilder(dataAdapter) will automatically generate the commands used for the Insert, Update, and Delete operations.

Chapter 10 - VB.Net by Schneider41 Using the DataAdapter to Change a Database If changes is an Integer variable, then the statement changes = dataAdapter.Update(dt) will store all of the insertions, updates, and deletions made in the data table to the database and assign the number of records changed to the variable changes.

Chapter 10 - VB.Net by Schneider42 Comments 1.There is a one-to-many relationship from the Countries table to the Cities table if each record of the Countries table is related to one or more records of the Cities table, but each record of the Cities table is related to only one record of the Countries table. 2.SQL statements are case insensitive. 3.When the Like operator is used, the “pattern” must appear on the right of the operator. SELECT * FROM Cities WHERE city Like 'S%'

Chapter 10 - VB.Net by Schneider43 Comments continued 4.An expression such as “[letter1-letter2]” is a placeholder for any letter from letter1 to letter2. Example: the pattern “[A-F]ad” is matched by Bad and Dad, but not Sad. 5.When Like is used in SQL statements, it is case insensitive. That is, (‘bad’ Like ‘[A-F]ad’) is True. When Like is used in an If block, the asterisk is used instead of the percent sign to denote any number of characters, and the question mark stands for any one character.

Chapter 10 - VB.Net by Schneider44 Comments continued 6.The requirement that no record may have a null primary key and that entries for primary keys be unique is called the Rule of Entity Integrity.