Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.

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

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)
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Chapter 14.3 LINQ to SQL Programming in Visual Basic 2010: The Very Beginner’s Guide by Jim McKeown Databases – Part 3.
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.
Guide to Oracle10G1 Introduction To Forms Builder Chapter 5.
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.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2012 CHAPTER TEN Incorporating Databases with ADO.NET.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 1 1 Microsoft Access 2003 Tutorial 1 – Introduction To Microsoft Access 2003.
Microsoft Access 2007 Microsoft Access 2007 Introduction to Database Programs.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
From VS C# 2010 Programming, John Allwork 1 VS2010 C# Programming - DB intro 1 Topics – Database Relational - linked tables SQL ADO.NET objects Referencing.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Some Basic Database Terminology
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
Databases and LINQ Visual Basic 2010 How to Program 1.
Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ.
Working with a Database
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Microsoft Access Get a green book. Page AC 2 Define Access Define database.
Chapter 6 Generating Form Letters, Mailing Labels, and a Directory
Exploring Microsoft Access Chapter 4 Relational Databases, External Data, Charts, and the Switchboard.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
XP New Perspectives on Microsoft Access 2002 Tutorial 21 Microsoft Access Tutorial 2 – Creating And Maintaining A Database.
XP 1 Microsoft Access 2003 Introduction To Microsoft Access 2003.
XP New Perspectives on Microsoft Office Access 2003, Second Edition- Tutorial 2 1 Microsoft Office Access 2003 Tutorial 2 – Creating And Maintaining A.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Key Applications Module Lesson 21 — Access Essentials
XP New Perspectives on Microsoft Access 2002 Tutorial 1 1 Microsoft Access 2002 Tutorial 1 – Introduction To Microsoft Access 2002.
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
1 Database Design and Development: A Visual Approach © 2006 Prentice Hall Chapter 8 DATABASE DESIGN AND DEVELOPMENT: A VISUAL APPROACH Chapter 8 Creating.
XP New Perspectives on Microsoft Access 2002 Tutorial 1 1 Microsoft Access 2002 Tutorial 1 – Introduction To Microsoft Access 2002.
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.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 25.1 Test-Driving the ATM Application 25.2.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
INTRODUCTION TO ACCESS. OBJECTIVES  Define the terms field, record, table, relational database, primary key, and foreign key  Create a blank database.
Databases and ADO.NET Programming Right from the Start with Visual Basic.NET 1/e 11.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 25 I’m Suffering from Information Overload.
1 Chapter 10 – Database Management 10.1 An Introduction to Databases 10.2 Editing and Designing Databases.
Chapter 24 I’m Suffering from Information Overload (Access Databases) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
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.
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.
COMPREHENSIVE Access Tutorial 1 Creating a Database.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
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.
Copyright © 2014 Pearson Canada Inc. Ext. 5b-1 Copyright © 2014 Pearson Canada Inc. Application Extension 5b Using Microsoft Access Part 2: Using Information.
Common SQL keywords. Building and using CASE Tools Data Base with Microsoft SQL-Server and C#
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
CIS16 Application Development Programming with Visual Basic
CIS16 Application Programming with Visual Basic
CIS16 Application Programming with Visual Basic
Database Applications
Presentation transcript:

Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ

Objectives After studying this chapter, you should be able to: Define the terms used when talking about databases Connect an application to a Microsoft Access database Bind table and field objects to controls Explain the purpose of the DataSet, BindingSource, TableAdapter, TableAdapterManager, and BindingNavigator objects Microsoft Visual Basic 2010: Reloaded, Fourth Edition2

Objectives (cont'd.) Customize a DataGridView control Handle errors using the Try…Catch statement Position the record pointer in a dataset Access the value stored in a field object Query a dataset using LINQ Customize a BindingNavigator control Use the LINQ aggregate methods Microsoft Visual Basic 2010: Reloaded, Fourth Edition3

Database Terminology Computer database: electronic file containing an organized collection of related information Relational database: database that stores information in tables composed of columns and rows Field: single item of information Record: group of related fields Table: group of related records Primary key: field that uniquely identifies each record Microsoft Visual Basic 2010: Reloaded, Fourth Edition4

Database Terminology (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition5 Figure 12-1: Example of a one-table relational database

Database Terminology (cont'd.) Parent table: contains a primary key Child table: contains a foreign key from the parent table to link the tables Foreign key: field in a table that contains the primary key of another table Relational database advantages: –Less redundancy –Fast retrieval –Ability to selectively retrieve data Microsoft Visual Basic 2010: Reloaded, Fourth Edition6

Database Terminology (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition7 Figure 12-2: Example of a two-table relational database

Connecting an Application to a Microsoft Access Database Microsoft Access database: –Has a file extension of.accdb Must connect an application to the database before the application can access the data Use the Data Source Configuration Wizard to connect to a database Dataset: copy of the fields and records stored in the computer’s internal memory, which the application can access Microsoft Visual Basic 2010: Reloaded, Fourth Edition8

Connecting an Application to a Microsoft Access Database (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition9 Figure 12-3: Data contained in the tblEmploy table

Connecting an Application to a Microsoft Access Database (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition10 Figure 12-4: How to connect an application to an Access database

Connecting an Application to a Microsoft Access Database (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition11 Figure 12-5: Result of running the Data Source Configuration Wizard

Connecting an Application to a Microsoft Access Database (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition12 Figure 12-6: How to preview the contents of a dataset

Microsoft Visual Basic 2010: Reloaded, Fourth Edition13 Figure 12-7: EmployeesDataSet shown in the Preview Data dialog box

Binding the Objects in a Dataset Binding: connecting an object in a dataset to a control on a form Bound controls: controls that are connected to an object in a dataset Can bind an object to: –An existing control in the interface –A control the computer creates for you Microsoft Visual Basic 2010: Reloaded, Fourth Edition14

Binding the Objects in a Dataset (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition15 Figure 12-8: How to bind an object in a dataset

Having the Computer Create a Bound Control To allow the computer to create a bound control: –Drag the object from the dataset to the form DataGridView control: displays table data in a row and column format Use the list arrow next to an object’s name to change the type of control to be created Microsoft Visual Basic 2010: Reloaded, Fourth Edition16

Having the Computer Create a Bound Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition17 Figure 12-9: Icons in the Data Sources window

Having the Computer Create a Bound Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition18 Figure 12-10: Result of clicking the tblEmploy table object’s list arrow

Having the Computer Create a Bound Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition19 Figure 12-11: Result of clicking the Last_Name filed object’s list arrow

Having the Computer Create a Bound Control (cont'd.) BindingNavigator control: –Allows movement to first, last, next, or previous record –Allows direct selection of record by number –Allows you to add or delete a record –Allows you to save changes made to the dataset Five objects are placed in the component tray: –DataSet, BindingSource, TableAdapter, TableAdapterManager, BindingNavigator Microsoft Visual Basic 2010: Reloaded, Fourth Edition20

Having the Computer Create a Bound Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition21 Figure 12-12: Result of dragging the table object to the form

Having the Computer Create a Bound Control (cont'd.) TableAdapter object: connects the database to the DataSet object DataSet object: stores the information to be accessed from the database TableAdapterManager object: handles saving data to multiple tables in the DataSet BindingSource object: connects the DataSet object to the bound controls on the form Microsoft Visual Basic 2010: Reloaded, Fourth Edition22

Having the Computer Create a Bound Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition23 Figure 12-13: Illustration of the relationships among the database, the objects in the component tray, and the bound controls

The DataGridView Control DataGridView control: displays data in a row and column format –Each row represents a record –Each column represents a field Cell: the intersection of a row and column DataGridView has a task list used to control its appearance and behavior Microsoft Visual Basic 2010: Reloaded, Fourth Edition24

The DataGridView Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition25 Figure 12-14: Task list for a DataGridView control

The DataGridView Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition26 Figure 12-15: Purpose of each task in the DataGridView’s task list

The DataGridView Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition27 Figure 12-15: Edit Columns dialog box

The DataGridView Control (cont'd.) Many properties of DataGridView are listed only in the Properties window AutoSizeColumnsMode property: –Select Fill setting to automatically adjust column widths to exactly fill the display area –Select ColumnHeader setting to adjust column widths based on the header text Microsoft Visual Basic 2010: Reloaded, Fourth Edition28

The DataGridView Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition29 Figure 12-17: DataGridView control after setting some of its properties

Visual Basic Code DataGridView control allows data to be edited directly within the control to update the database Two event procedures are automatically created in Code Editor window when a table or field object is dragged to the form –MainForm_Load –bindingNavigatorSaveItem_Click MainForm_Load event: –Fill method: TableAdapter object’s method to retrieve data from the database and store it in the dataset Microsoft Visual Basic 2010: Reloaded, Fourth Edition30

Microsoft Visual Basic 2010: Reloaded, Fourth Edition31 Visual Basic Code (cont'd.) bindingNavigatorSaveItem_Click event: –Saves any changes made to the dataset –EndEdit method: applies pending changes to the dataset –UpdateAll method: commits the dataset changes to the database Be sure to use error-handling code

Visual Basic Code (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition32 Figure 12-18: Code automatically entered in the Code Editor window

Handling Errors in the Code Exception: an error that occurs while an application is running VB “handles” errors if the program does not by showing an error message and terminating the application Try…Catch statement: used to trap errors and attempt to handle them –Place code that could cause an exception within the Try block –Catch block is executed if an exception occurs Microsoft Visual Basic 2010: Reloaded, Fourth Edition33

Microsoft Visual Basic 2010: Reloaded, Fourth Edition34 Figure 12-19: How to use the Try…Catch statement

Handling Errors in the Code (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition35 Figure 12-19: How to use the Try…Catch statement (cont’d.)

Handling Errors in the Code (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition36 Figure 12-20: Try…Catch statement entered in the Save Data button’s Click event procedure

Handling Errors in the Code (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition37 Figure 12-21: Sample run of the Morgan Industries application

Handling Errors in the Code (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition38 BindingNavigator control provides buttons for first, last, previous, and next record and for adding records, deleting records, and saving changes Can also use the control to access a record by its record number

The Copy to Output Directory Property Microsoft Visual Basic 2010: Reloaded, Fourth Edition39 Local database file: database file contained in a project Copy to Output Directory property: determines how VB saves changes to a local database file Copy always setting: database file is copied from the project folder to bin\Debug folder each time the application is started –Changes are made only to the bin\Debug copy Copy if newer setting: newer of the two database files (project folder and bin\Debug folder) is saved in bin\Debug folder

The Copy to Output Directory Property (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition40 Figure 12-22: How to use the Copy to Output Directory property

Binding to an Existing Control Can bind an object in a dataset to an existing control in two ways: –Drag an object in the dataset to a control on the form –Select the control and set properties Properties to bind the control are specific to the control DataSet, BindingSource,TableAdapter, and TableAdapterManager objects are added to the component tray BindingNavigator control is NOT added automatically Microsoft Visual Basic 2010: Reloaded, Fourth Edition41

Binding to an Existing Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition42 Figure 12-23: Result of dragging field objects to existing label controls

Binding to an Existing Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition43 Figure 12-24: Sample run of a different version of the Morgan Industries application

Coding the Next Record and Previous Record Buttons BindingSource object uses an invisible record pointer to track the current record in the dataset Position property: stores position of current record; this position number is zero-relative Move method: moves the record pointer’s position to first, last, previous, or next record in the dataset Microsoft Visual Basic 2010: Reloaded, Fourth Edition44

Microsoft Visual Basic 2010: Reloaded, Fourth Edition45 Figure 12-23: How to use the BindingSource object’s Position property

Microsoft Visual Basic 2010: Reloaded, Fourth Edition46 Figure 12-26: How to use the BindingSource object’s Move methods

Coding the Next Record and Previous Record Buttons (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition47 Figure 12-27: Code entered in the Click event procedures for the Next Record and Previous Record buttons

Accessing the Value Stored in a Field Microsoft Visual Basic 2010: Reloaded, Fourth Edition48 Figure 12-28: How to access the value stored in a field object

Creating a Query Can arrange records in a dataset in any order Query: specifies the records to select in a dataset Language Integrated Query (LINQ): language used to create a query –Where clause: optional, specifies a condition to limit which records to view –Order By clause: optional, specifies whether to arrange in descending or ascending order Option Infer On: allows computer to infer the data type from the variables in the query Microsoft Visual Basic 2010: Reloaded, Fourth Edition49

Creating a Query (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition50 Figure 12-29: How to use LINQ to select and arrange records in a dataset

Microsoft Visual Basic 2010: Reloaded, Fourth Edition51 Figure 12-29: How to use LINQ to select and arrange records in a dataset (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition52 Figure 12-30: How to assign a LINQ variable’s contents to a BindingSource control Creating a Query (cont’d.)

Microsoft Visual Basic 2010: Reloaded, Fourth Edition53 Figure 12-31: LINQ code entered in the Find Last Name button’s Click event procedure

Creating a Query (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition54 Figure 12-32: Employees whose last name begins with the letter S

Personalizing a BindingNavigator Control You can include other items on the BindingNavigator control: –Buttons, text boxes, or drop-down buttons Microsoft Visual Basic 2010: Reloaded, Fourth Edition55

Personalizing a BindingNavigator Control (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition56 Figure 12-33: How to manipulate the items on a BindingNavigator control

Personalizing a BindingNavigator Control (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition57 Figure 12-34: Items Collection Editor window

Personalizing a BindingNavigator Control (cont’d.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition58 Figure 12-35: DropDownButton added to the BindingNavigator control

Using the LINQ Aggregate Operators Aggregate operator: returns a single value from a group of values Most commonly used aggregate operators: –Average, Count, Max, Min, and Sum Microsoft Visual Basic 2010: Reloaded, Fourth Edition59

Microsoft Visual Basic 2010: Reloaded, Fourth Edition60 Figure 12-36: How to use the LINQ aggregate operators

Using the LINQ Aggregate Methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition61 Figure 12-37: Code associated with the three items on the DropDownButton

Using the LINQ Aggregate Methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition62 Figure 12-37: Code associated with the three items on the DropDownButton (cont’d.)

Using the LINQ Aggregate Methods (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition63 Figure 12-37: Code associated with the three items on the DropDownButton (cont’d.)

Adding Items to the BindingNavigator Control (cont'd.) Microsoft Visual Basic 2010: Reloaded, Fourth Edition64 Figure 12-38: Average pay rate for part-time employees

Creating the Trivia Game application Microsoft Visual Basic 2010: Reloaded, Fourth Edition65 Programming Tutorial 1 Figure 12-40: MainForm for the Trivia Game application

Creating the CD Collection application Microsoft Visual Basic 2010: Reloaded, Fourth Edition66 Programming Tutorial 2 Figure 12-50: Preview Data dialog box showing the data stored in the CDDataSet

Microsoft Visual Basic 2010: Reloaded, Fourth Edition67 Programming Example Cartwright Industries application Figure 12-55: MainForm in the Cartwright Industries application

Microsoft Visual Basic 2010: Reloaded, Fourth Edition68 Summary Can use Visual Basic to access data stored in databases Relational database: stores information in tables composed of fields and records Primary key: field in a database table that uniquely identifies each record Data in a relational database can be displayed in any order, and you can control the amount of information to view

Microsoft Visual Basic 2010: Reloaded, Fourth Edition69 Summary (cont'd.) You must connect the application to a database to create a dataset Display dataset information by binding controls to dataset objects TableAdapter : connects a database to a DataSet object BindingSource object: connects a DataSet object to bound controls on a form DataGridView control displays data in row and column format

Microsoft Visual Basic 2010: Reloaded, Fourth Edition70 Summary (cont'd.) Use a Try…Catch statement to handle exceptions Database file’s Copy to Output Directory property: determines when and if the file is copied to the project’s bin\Debug folder BindingSource object’s Position property: stores the location of the record pointer in a dataset BindingSource object’s Move methods: used to move the record pointer in a dataset Can access the value stored in a field object in a dataset

Summary (cont'd.) Use LINQ to select and arrange records in a dataset LINQ provides Average, Sum, Count, Min, and Max aggregate methods Can include additional items such as text boxes and drop-down buttons on a BindingNavigator control Microsoft Visual Basic 2010: Reloaded, Fourth Edition71