Working With Databases

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
McGraw-Hill/Irwin Copyright © 2013 by The McGraw-Hill Companies, Inc. All rights reserved. Extended Learning Module D (Office 2007 Version) Decision Analysis.
BASIC SKILLS AND TOOLS USING ACCESS
Tutorial 3 – Creating a Multiple-Page Report
Microsoft Access 2007 Advanced Level. © Cheltenham Courseware Pty. Ltd. Slide No 2 Forms Customisation.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Windows XP Project An Introduction to Microsoft Windows XP and Office 2003.
1.
Information Systems Today: Managing in the Digital World
Chapter 17 Linked Lists.
Campaign Overview Mailers Mailing Lists
Chapter 7 Working with Databases and MySQL
Copyright © 2008 Pearson Prentice Hall. All rights reserved.1 1 Committed to Shaping the Next Generation of IT Experts. Chapter 2.1: Relational Databases.
© Paradigm Publishing, Inc Access 2010 Level 1 Unit 1Creating Tables and Queries Chapter 2Creating Relationships between Tables.
Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall.
Microsoft Office Illustrated Fundamentals Unit K: Working with Data.
Yong Choi School of Business CSU, Bakersfield
Microsoft Access.
Displaying Data from Multiple Tables
Chapter Information Systems Database Management.
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
Access Tables 1. Creating a Table Design View Define each field and its properties Data Sheet View Essentially spreadsheet Enter fields You must go to.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Office 2003 Introductory Concepts and Techniques M i c r o s o f t Office 2003 Integration Integrating Office 2003 Applications and the World Wide Web.
Benchmark Series Microsoft Excel 2013 Level 2
1 Displaying Open Purchase Orders (F/Y 11). 2  At the end of this course, you should be able to: –Run a Location specific report of all Open Purchase.
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
PSSA Preparation.
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
Essential Cell Biology
Chapter 13 Web Page Design Studio
Computer Concepts BASICS 4th Edition
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 2Advanced Reports, Access Tools, and Customizing Access Chapter 8Integrating Access Data.
Benchmark Series Microsoft Excel 2013 Level 2
South Dakota Library Network MetaLib User Interface South Dakota Library Network 1200 University, Unit 9672 Spearfish, SD © South Dakota.
Windfall Web Throughout this slide show there will be hyperlinks (highlighted in blue). Follow the hyperlinks to navigate to the specified Topic or Figure.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 10 Database Applications Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
 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)
Chapter 12: Using ADO.NET 2.0 Programming with Microsoft Visual Basic 2005, Third Edition.
XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Copyright © 2014 Pearson Education, Inc. Chapter 10 Working with Databases.
Some Basic Database Terminology
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Copyright © 2010 Pearson Education, Inc. Publishing as Prentice Hall 1 1. Chapter 2: Relational Databases and Multi-Table Queries Exploring Microsoft Office.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
ASP.NET Programming with C# and SQL Server First Edition
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 10 Working with Databases.
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 17 Creating a Database.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Copyright © 2014 Pearson Education, Inc. Chapter 10 Working with Databases.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Binding to ListBox and ComboBox Controls List and combo boxes are frequently.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Brief description on how to navigate within this presentation (ppt)
Working with Databases
CIS16 Application Programming with Visual Basic
Database Applications
Presentation transcript:

Working With Databases Chapter 10 Working With Databases

Chapter 10 Introduction

Chapter 10 Topics Basic database terminology Fundamental database concepts Use ADO .NET to access databases Display, sort, and update database data Use the DataGridView control

Section 10.1 What Is a Database? A Database is a Collection of Data Organized in Tables, Rows, and Columns

Terminology Database: a collection of interrelated tables Table: a logical grouping of related data A category of people, places, or things For example, employees or departments Organized into rows and columns Field: an individual piece of data pertaining to an item, an employee name for instance Record: the complete data about a single item such as all information about an employee A record is a row of a table

Database Table Each table has a primary key Uniquely identifies that row of the table PartNumber is the primary key in this example Columns also called fields or attributes Each column has a particular data type Row (Record) Column Field

VB and Access Data Types VB data types must match table data types MS Access and VB have similar data types Access Data Type Visual Basic Data Type AutoNumber Long Date/Time Date, DateTime Number, Integer Integer Number, long integer Long Integer Number, single Single Number, double Double Text String Yes/No Boolean

Database Design A database schema is the design of tables, columns, and relationships between tables Define a column for each piece of data Allow plenty of space for text fields For the members of an organization: Column Name Type Size First_Name Text 40 Last_Name Text 40 Phone Text 30 Email Text 40 Date_Joined Date/Time Officer Yes/No 1

Issues with Redundant Data Database design minimizes redundant data In the following employee table: ID First_Name Last_Name Department 001234 Ignacio Fleta Accounting 002000 Christian Martin Computer Support 002122 Orville Gibson Human Resources 00300 Jose Ramirez Research & Devel 003400 Ben Smith Accounting 003780 Allison Chong Computer Support Same dept name appears multiple times Requires additional storage space Causes problems if misspelled What if a department needs to be renamed?

Eliminating Redundant Data Create a department table Dept_ID Dept_Name Num_Employees 1 Human Resources 10 2 Accounting 5 3 Computer Support 30 4 Research & Development 15 Reference department table in employee table ID First_Name Last_Name Dept_ID 001234 Ignacio Fleta 2 002000 Christian Martin 3 002122 Orville Gibson 1 003000 Jose Ramirez 4 003400 Ben Smith 2 003780 Allison Chong 3

One-to-Many Relationships The previous changes created a one-to-many relationship Every employee has one and only one dept Every department has many employees DeptID in department table is a primary key DeptID in employee table is a foreign key In general, a one-to-many relationship is created when the primary key of one table is specified as a field of a another table

Section 10.2 Using the DataGridView The DataGridView Control Allows you to Display a Database Table in a Grid Which Can be Used at Runtime to Sort and Edit the Contents of the Table

Connecting VB to a Database VB provides tools to display database tables Data binding links tables to form controls Controls called components establish the link A wizard guides you through the process We’ll use these data-related components: Data source – usually a database Binding source – holds database name, location, and other connection information Table adapter – uses SQL to select data Dataset – in-memory copy of data from tables

Connecting VB to a Database The flow of data from database to application Note that data moves in both directions Data travels from data source to application Application can view/change dataset contents Changes to dataset can be written back to the data source Tutorial 10-1 demonstrates how to connect a database table to a DataGridView control Data Source Binding Source Table Adapter Dataset Application

Section 10.3 Selecting Dataset Rows Visual Basic Provides Convenient Tools for Selecting Which Rows from the Dataset You Want to Display

Structured Query Language (SQL) Often need to select certain rows in a table using Structured Query Language (SQL) Standard method of working with a database Adopted by most all database software Not a general purpose programming language Defines how to construct queries that return selected data from the database

SQL Query Example You might want to select all employees hired before 1998 and earning less than $45,000 Select ID, Name, Full_Time, Hire_Date, Salary From employees Where Hire_Date < 1/1/1998 and Salary < 45000 Select, From, and Where are keywords Fields to be returned listed after Select Table containing the data listed after From Conditions affecting row selection after Where SQL query is a property of the TableAdapter

Configuring the TableAdapter Right-click on dataset and select Configure to start TableAdapter Configuration Wizard Can modify a simple query directly in wizard Or use Query Builder for more complex query Query Builder is a tool to create or modify queries with minimal knowledge of SQL To add a query to a DataGridView Right-click Table Adapter icon attached to grid Select Add Query from shortcut menu Tutorial 10-2 demonstrates how this is done

Section 10.4 Data-Bound Controls Some Controls Can Be Bound to a Dataset. A Data-bound Control Can be Used to Display and Edit the Contents of a Particular Row and Co.lumn

Advantages of Data-Binding Can add multiple data sources to a project Can bind fields in a data source to controls: Text boxes Labels List boxes Contents of data-bound controls change automatically when moving from row to row Updating the contents of a database field from a data-bound control is also very easy

Binding Existing Dataset to DataGrid Use the Data Sources window Locate the dataset table Drag table to an open area of a form Creates a data grid bound to the data source Automatically adds a navigation bar to form Set Dock property to Center Docking to make the data grid fill the entire form

Binding Individual Fields to Controls Use the Data Sources window Locate the dataset table Select Details from the table drop-down list Drag table to an open area of a form Creates a separate control for each field Can also drag columns individually Adds automatic navigation bar as before Text and numeric fields added as text boxes Yes/No fields added as checkboxes May wish to change some control properties

Binding to List and Combo Boxes List and combo boxes are frequently used to supply a list of items for a user to select from Such lists are often populated from a table Must set two list/combo box properties DataSource identifies a table within a dataset DisplayMember identifes the table column to be displayed in the list/combo box If table column dragged onto a list/combo box Visual Studio creates the required dataset, table adapter, and binding source components Tutorial 10-3 shows this type of binding

Adding New Rows to a Dataset NewRow method creates a new, empty row Assign values to columns in the empty row Add method appends new row to the dataset The following example adds a new row to the Payments table of the dsPayments dataset Dim row as dsPayments.PaymentsRow Row = CType(DsPayments.Payments.NewRow(), _ dsPayments.PaymentsRow) With row row.Member_ID = 5 row.Payment_Date = #5/15/2006# row.Amount = 500D End With DsPayments.Payments.Rows.Add(row)

Another Means to Add Dataset Rows Can call the dataset Add method directly This approach is more straightforward but: Must specify values in correct column order Previous example becomes far simpler: DsPayments.Payments.Rows.Add( _ Nothing, 5, #5/15/2006#, 500D) Payments table 1st column is autonumber so: Must have an argument for this column Pass Nothing as the value for this column

Removing a Row from a Dataset Two steps to remove a row Get a reference to the row to remove Call Remove method on Rows collection Following example calls FindByID with the primary key to get a reference to the row Dim row as DataRow = _ DsPayments.Payments.FindByID(36) DsPayments.Payments.Rows.Remove(row)

Updating the Database Previous add and remove examples change the dataset but not the underlying database Must call TableAdapter Update method to save dataset changes to the database To write changes in the DsPayments dataset to the underlying database: PaymentsTableAdapter.Update(DsPayments) Tutorial 10-4 demonstrates adding new rows to the Payments table

Reading Dataset Rows with For-Each A For-Each statement can be used to iterate over all rows of a dataset Usually use a strongly typed dataset for this Sum Amount column of dsPayments dataset Dim row as dsPayments.PaymentsRow Dim decTotal as Decimal = 0 For Each row in DsPayments.Payments.Rows decTotal += row.Amount Next Tutorial 10-5 demonstrates this technique

Section 10.5 Structured Query Language (SQL) SQL Is a Standard Language for Working With Databases

The Select Statement Select retrieves rows from one or more tables in a database Basic form of Select for a single table is Select column-list From table column-list contains column names to select from table, each separated by a comma The following Select statement retrieves the ID and Salary fields from the SalesStaff table Select ID, Salary From SalesStaff

Column Names Use asterisk to select all columns in a table From SalesStaff Unlike VB names, SQL columns can have embedded spaces If so, use square brackets around column names Select [Last Name], [First Name] Better to avoid embedded spaces for this reason As operator can be used to rename columns Select Last_Name, Hire_Date As Date_Hired

Creating New Columns Sometimes useful to create a new column by appending existing columns together Create a Full_Name field from first and last name Select Last_Name + ‘, ‘ + First_Name as Full_Name From SalesStaff Creates a Full_Name field in the format last, first Can also be useful to create a new column by performing arithmetic operations Columns involved must be numeric Select ID, hrsWorked * hourlyRate As payAmount From Payroll Creates a payAmount column with gross pay

Sorting Rows with Order By Clause SQL Select has an optional Order By clause that affects the order in which rows appear Order by Last_Name, First_Name Displays rows in order by last name, then first Sort in reverse order (high to low) using Desc Order by Last_Name DESC Order By clause appears after From clause Select First_Name, Last_Name, Date_Joined From Members Order By Last_Name, First_Name Lists all members by last name, then first

Selecting Rows with Where Clause SQL Select has an optional Where clause that can be used to select (or filter) certain rows Where Last_Name = ‘Gomez’ Displays only rows where last name is Gomez Must be a defined column (in table or created) This example selects based on a created field Select Last_Name, hrsWorked * Rate As payAmount From Payroll Where payAmount > 1000 Order by Last_Name Selects those being paid more than $1,000

SQL Relational Operators SQL Where uses relational operators just like a VB If Operator Meaning = equal to <> not equal to < less than <= less than or equal to > greater than >= greater than or equal to Between between two values (inclusive) Like similar to (match using wildcard) Example of Between operator: Where Hire_Date Between #1/1/1992# and #12/31/1999# Example of Like operator with % sign as wildcard: Where Last_Name Like ‘A%’

Compound Expressions SQL uses And, Or, and Not to create compound expressions Select all employees hired after 1/1/1990 and with a salary is greater than $40,000 Where (Hire_Date > #1/1/1990#) and (Salary > 40000) Select all employees hired after 1/1/1990 or with a salary is greater than $40,000 Where (Hire_Date > #1/1/1990#) or (Salary > 40000) Select employee names not beginning with A Where Last_Name Not Like ‘A%’

Section 10.6 Karate School Management Application Create an Application that Works With the Karate School Database