Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with 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)
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.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Chapter 12: ADO.NET and ASP.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Microsoft Access 2003 Introduction To Microsoft Access 2003.
Using ADO.NET Chapter Microsoft Visual Basic.NET: Reloaded 1.
A Guide to Oracle9i1 Introduction To Forms Builder Chapter 5.
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
1 Pertemuan 09 Database Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Introduction to ADO.Net, VB.Net Database Tools and Data Binding ISYS 512.
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.
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.
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.
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.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 512.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
10-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Microsoft Access Get a green book. Page AC 2 Define Access Define database.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Twelve Access Databases and LINQ.
Exploring Microsoft Access Chapter 4 Relational Databases, External Data, Charts, and the Switchboard.
1 Databound Controls. 2 Objectives You will be able to use design time data binding to display and update SQL Server database data without writing any.
Key Applications Module Lesson 21 — Access Essentials
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
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.
Databases with LINQ. LINQ to SQL LINQ to SQL uses LINQ syntax to query databases. LINQ to SQL classes are automatically generated by the IDE’s LINQ to.
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.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
MS-Access XP Lesson 4. Modifying Queries 1.Select query in queries 2.Click design button or Right click on query and click design view 3.Change query.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Address Book Application Introducing Database Programming.
Understand Databound Controls Windows Development Fundamentals LESSON 4.2A.
Introduction to ADO.Net and Visual Studio Database Tools ISYS 350.
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.
Chapter 9 Working with Databases. Copyright © 2011 Pearson Addison-Wesley Introduction In this chapter you will learn: – Basic database concepts – How.
Creating SQL Database file And Displaying a Database Table in a DataGridView.
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 14: Access Databases and SQL.
DATA GRID VIEW objective of lecture:- After completing this lecture, you will be able to: Create a data grid view object on a windows form and use it to.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
VB 2010 Pertemuan 10.
Incorporating Databases with ADO.NET
Incorporating Databases with ADO.NET
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
CIS16 Application Programming with Visual Basic
Database Applications
Chapter 10 Accessing Database Files
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ

Previewing the Paradise Bookstore Application Open the Chap13\Paradise.exe file (Need to download Books.accdb Access DB too.) Application displays records in the Books database Allows the store manager to enter an author’s name (or part of a name) –Displays only books written by the author Displays the total value of books in the store Programming with Microsoft Visual Basic 2008, Fourth Edition2 GUI Introduction

Previewing the Paradise Bookstore Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition3 Figure 13-1: Books written by Carol Smith

Lesson A Objectives After studying Lesson A, you should be able to: Understand database terms Connect an application to a Microsoft Access D/B Bind table and field objects to VB controls Explain the purpose of VB database objects –DataSet, BindingSource, –TableAdapter, TableAdapterManager, –BindingNavigator Customize a DataGridView control Handle errors using the Try…Catch statement Position the record pointer in a dataset Programming with Microsoft Visual Basic 2008, Fourth Edition4

Database Terminology Computer database: Electronic file containing an organized collection of related information Relational database: Database that stores information in tables composed of rows and columns –Each column ( 直欄 ) in a table represents a field –Each row ( 橫列 ) in a table represents a record Field: Single piece of information about an object, e.g., person, place or thing, … Programming with Microsoft Visual Basic 2008, Fourth Edition5

Database Terminology (continued) Record: A group of related fields that contain all the necessary data about a specific object (person, place, or thing, …) Table: A group of similar records –Each record in a table pertains to the same topic, and contains the same type of information Primary key: A field uniquely identifying a record Foreign key: Field that links a child record to a parent record in a 1:M relationship Programming with Microsoft Visual Basic 2008, Fourth Edition6

7 Figure 13-2: Example of a relational table Example of a relational table

Programming with Microsoft Visual Basic 2008, Fourth Edition8 Figure 13-3: Example of PK-FK pair in parent/child tables Example of PK-FK pair in a 1:M relationship

Connecting an Application to a Microsoft Access Database MS Access 2007 database has a file extension of.accdb (Need to install Access 2007 Database Engine Driver in PC) Sample database contains one table named tblEmploy, containing employee information A VB application must create a database connection to access data Data Source Configuration Wizard ( 資料來源組態精靈 ) –Helps you connect an application to a database Dataset: A temporary copy of the data you have requested from the database Programming with Microsoft Visual Basic 2008, Fourth Edition9

10 Figure 13-4: Data contained in the tblEmploy table Connecting an Application to a Microsoft Access Database (Table example)

Connecting an Application to a Microsoft Access Database (continued) In Visual Studio, perform the following steps: 1.Open the Server Explorer window: Click the View menu and then click Server (Database) Explorer 2.Click the Data menu and then click Show Data Sources to open Data Sources window 3.Click Add New Data Source button, and then select Database 4.Click New Connection button, and then select Microsoft Access Database File (OLE DB) 5.Click Browse, navigate to database file, and then click Open Programming with Microsoft Visual Basic 2008, Fourth Edition11

Connecting an Application to a Microsoft Access Database (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition

Connecting an Application to a Microsoft Access Database (Data Source Configuration Wizard) Programming with Microsoft Visual Basic 2008, Fourth Edition13 4.1

Connecting an Application to a Microsoft Access Database In Visual Studio, perform these steps (continued): 6.Click button on Add Connection screen, and if successful, click Next (May need to install Access 2007 Database Engine Driver) 7.Click button to include the database file into the current project 8.Click Yes to save connection string 9.On Choose Your Database Objects screen, select all table objects 10.Finish Programming with Microsoft Visual Basic 2008, Fourth Edition14 Test Connection Yes

Programming with Microsoft Visual Basic 2008, Fourth Edition15 Figure 13-6: Completed Add Connection dialog box Connecting an Application to a Microsoft Access Database (Test Connection ) 5 4.2

Programming with Microsoft Visual Basic 2008, Fourth Edition16 Figure 13-7: Message regarding copying the database file Connecting an Application to a Microsoft Access Database (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition17 Figure 13-8: Save the Connection String to the Application Configuration File screen Connecting an Application to a Microsoft Access Database (save connection string)

Programming with Microsoft Visual Basic 2008, Fourth Edition18 Figure 13-9: Objects selected in the Choose Your Database Objects screen Connecting an Application to a Microsoft Access Database (Choose Your Database Objects )

Programming with Microsoft Visual Basic 2008, Fourth Edition19 Figure 13-10: Result of running the Data Source Configuration Wizard Connecting an Application to a Microsoft Access Database (continued)

Previewing the Contents of a Dataset 1.Click the form to make it the active window 2.Click Data menu and then click Preview Data to open Preview Data dialog box 3.Select the object to preview and then click Preview 4.After previewing the data, click Close button Check how many new files show up in Morgan Industries Project Programming with Microsoft Visual Basic 2008, Fourth Edition20

Binding the Objects in a Dataset Must bind one or more objects in the dataset to controls in the interface to view dataset contents Binding: Connecting a dataset object to a control Bound controls: Connected controls Types of controls used to bind dataset objects: –Computer-created control –Existing control on the form Programming with Microsoft Visual Basic 2008, Fourth Edition21

Programming with Microsoft Visual Basic 2008, Fourth Edition22 Figure 13-12: Ways to bind an object in a dataset Binding the Objects in a Dataset (continued) 1 Table Attribute

Having the Computer Create a Bound Control When you drag a dataset object onto a form: –Computer creates control (its type is indicated by icon) –Dataset object is automatically bound to control Example: –Drag tblEmployee table object to the form –DataGridView control is created to display tabular data, with rows representing records, columns representing fields Use list arrow to change the type of control linked to the object Programming with Microsoft Visual Basic 2008, Fourth Edition23 Table Attribute

Programming with Microsoft Visual Basic 2008, Fourth Edition24 Figure 13-13: Icons in the Data Sources window Having the Computer Create a Bound Control (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition25 Figure 13-14: Result of clicking the tblEmploy object’s list arrow Having the Computer Create a Bound Control (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition26 Figure 13-15: Result of clicking the Last_Name object’s list arrow Having the Computer Create a Bound Control (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition27 Figure 13-16: Result of dragging the table object to the form Having the Computer Create a Bound Control (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition28 Figure 13-26: Data displayed in the DataGridView control Running DataGridView

Having the Computer Create a Bound Control (continued) In addition to a control, the computer adds: –BindingNavigator control: To move from one record to the next in the dataset –TableAdapter object: Connects the database to the DataSet object; responsible for retrieving data and storing it in the Dataset –TableAdapterManager object: Handles saving data to multiple tables in the Dataset –BindingSource object: Provides the connection between the Dataset and the bound controls Programming with Microsoft Visual Basic 2008, Fourth Edition29

Programming with Microsoft Visual Basic 2008, Fourth Edition30 Figure 13-17: Illustration of the relationships among the database, the objects in the component tray, and the bound controls Having the Computer Create a Bound Control (continued) located in memory located in disk

The DataGridView Control DataGridView control: Displays data in a row and column format Cell: Intersection of a row and column DataGridView’s task list allows you to add, reorder, and remove columns, and set properties of bound columns AutoSizeColumnsMode: Has seven settings that control how the column widths are sized –Fill setting: Automatically adjusts the column widths so that the display area of the control is filled Programming with Microsoft Visual Basic 2008, Fourth Edition31

Programming with Microsoft Visual Basic 2008, Fourth Edition32 Figure 13-18: Task list for a DataGridView control The DataGridView Control (continued) DataGridView’s task list To set the format of table columns

Programming with Microsoft Visual Basic 2008, Fourth Edition33 Figure 13-19: Purpose of each task in the DataGridView’s task list The DataGridView Control (DataGridView’s task list) 常用

Programming with Microsoft Visual Basic 2008, Fourth Edition34 Figure 13-20: Edit Column dialog box The DataGridView Control (Edit Column dialog box) Use Header Text to change the title of columns 3.2 Use DefaultCellStyle to set data format

Programming with Microsoft Visual Basic 2008, Fourth Edition35 Figure 13-21: Completed CellStyle Builder dialog box in DefaultCellStyle The DataGridView Control (CellStyle Builder)

Programming with Microsoft Visual Basic 2008, Fourth Edition36 Figure 13-22: DataGridView control after setting some of its properties The DataGridView Control (continued)

Visual Basic Code When a table or field object is dragged onto a form the computer also enters code in the Code Editor window –Code in the form’s Load event uses the TableAdapter object to retrieve data –BindingNavigator’s save event is also coded Programming with Microsoft Visual Basic 2008, Fourth Edition37

Programming with Microsoft Visual Basic 2008, Fourth Edition38 Figure 13-23: Code automatically entered in the Code Editor window Visual Basic Code (continued) A dataset defined In 資料來源 A table defined in certain dataset

Handling Errors in the Code Exception: Error that occurs while an application is running Try…Catch statement: Used to intercept exceptions and handle them –Try block: Contains the statements that might fail –Catch block: Contains the code to handle the exceptions Programming with Microsoft Visual Basic 2008, Fourth Edition39

Programming with Microsoft Visual Basic 2008, Fourth Edition40 Figure 13-24: Syntax and examples of the Try…Catch statement Handling Errors in the Code (Try…Catch statement)

Programming with Microsoft Visual Basic 2008, Fourth Edition41 Figure 13-25: Try…Catch statement entered in the Save Data button’s Click event procedure Handling Errors in the Code (continued)

Binding to an Existing Control Can bind an object in a dataset to an existing control on the form in two ways: Method 1: Drag object from Data Sources window to control Method 2: Set one or more of the control’s properties in the Properties window –Properties to set depend on the type of control being bound: DataGrid: Set DataSource and DataMember ListBox: Set DataSource and DisplayMember Label or text box: Set DataBindings and Text Programming with Microsoft Visual Basic 2008, Fourth Edition42

Coding the Next Record and Previous Record Buttons BindingSource object’s Position property: –Stores an invisible record pointer –Positions are integer values  0 –First record is at position 0 BindingSource object’s Move methods: Can be used to move the record pointer in a dataset to the first, last, next, or previous record in the dataset Programming with Microsoft Visual Basic 2008, Fourth Edition43

Programming with Microsoft Visual Basic 2008, Fourth Edition44 Figure 13-29: Syntax and examples of the BindingSource object’s Position property Coding the Next Record and Previous Record Buttons (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition45 Figure 13-29: Syntax and examples of the BindingSource object’s Position property (continued) Coding the Next Record and Previous Record Buttons (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition46 Figure 13-29: Syntax and examples of the BindingSource object’s Move methods Coding the Next Record and Previous Record Buttons (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition47 Figure 13-30: Code entered in each button’s Click event procedure Coding the Next Record and Previous Record Buttons (continued)

Lesson A Summary Use Data Source Configuration Wizard to connect an application to a database Use the Preview Data command on the Data menu to preview the data in a dataset Bind an object in a dataset by dragging it to the form and letting the computer create a control, or by dragging it onto an existing control Use the Fill setting of the DataGridView’s AutoSizeColumnsMode property to have columns fill the display area Programming with Microsoft Visual Basic 2008, Fourth Edition48

Lesson A Summary (continued) Use Dock property of DataGridView to anchor it to the borders of form Use BindingSource object’s Position property or its Move methods to move record pointer while the application is running Programming with Microsoft Visual Basic 2008, Fourth Edition49

Lesson B Objectives After studying Lesson B, you should be able to: Query a dataset using LINQ LINQ (Language INtegrated Query) Add an item to a BindingNavigator control Use the LINQ aggregate operators Programming with Microsoft Visual Basic 2008, Fourth Edition50

Creating a Query Query: Specifies –the records to select from a dataset, and –the order in which to arrange the records Language Integrated Query (LINQ): A new database query language in Visual Basic 2008 –Where clause: Contains a condition to limit the records to be selected –Order By clause: Used to arrange the records in ascending or descending order by one or more fields Programming with Microsoft Visual Basic 2008, Fourth Edition51

Programming with Microsoft Visual Basic 2008, Fourth Edition52 Figure 13-32: Basic LINQ syntax and examples for selecting and arranging records Creating a Query (continued) Program Block A Each record in dataset.table is denoted by variable element in program block A same record variable name

Programming with Microsoft Visual Basic 2008, Fourth Edition53 Figure 13-33: Code entered in the Declarations section and btnFind Click event procedure Creating a Query (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition54 Figure 13-33: Code entered in the Declarations section and btnFind Click event procedure (continued) Creating a Query (continued)

Customizing a BindingNavigator Control Can add additional items to a BindingNavigator control to customize it: –Button –Textbox –Drop-down button Programming with Microsoft Visual Basic 2008, Fourth Edition55

Programming with Microsoft Visual Basic 2008, Fourth Edition56 Figure 13-35: Steps for adding an item to a BindingNavigator control Customizing a BindingNavigator Control (continued)

Programming with Microsoft Visual Basic 2008, Fourth Edition57 Figure 13-36: Items Collection Editor dialog box Customizing a BindingNavigator Control (Items Collection Editor) An item’s property

Programming with Microsoft Visual Basic 2008, Fourth Edition58 Figure 13-37: DropDownButton added to the TblEmployBindingNavigator control Customizing a BindingNavigator Control (continued)

Using the LINQ Aggregate Operators Aggregate operator: Returns a single value from a group of values LINQ provides several aggregate operators: –Average ( ) –Count ( ) –Max ( ) –Min ( ) –Sum ( ) Programming with Microsoft Visual Basic 2008, Fourth Edition59

Syntax of LINQ Aggregate Operators Dim variable [As dataType] = Aggregate element In dataset.table [Where condition] Select element.field Into aggregateOperator() Example 1 Dim dblAvgRate As Double _ = Aggregate employee In EmployeeDataSet.tblEmployee _ Select employee.Rate _ Into Average( ) Programming with Microsoft Visual Basic 2008, Fourth Edition60

Examples of LINQ Aggregate Operators Example 2 Dim dblMaxRate As Double _ = Aggregate employee In EmployeeDataSet.tblEmployee _ Select employee.Rate _ Into Max( ) Example 3 Dim intCounter As Integer _ = Aggregate employee In EmployeeDataSet.tblEmployee _ Select employee.Emp_Number _ Into Count( ) Programming with Microsoft Visual Basic 2008, Fourth Edition61

Programming with Microsoft Visual Basic 2008, Fourth Edition62 Figure 13-40: Message box showing the average pay rate for all employees Using the LINQ Aggregate Operators (continued)

Lesson B Summary Use LINQ to select a range of records in a dataset You can customize the BindingNavigator control by adding additional items to it LINQ provides aggregate operators that return a single value from a group of values Programming with Microsoft Visual Basic 2008, Fourth Edition63

Lesson C Objectives After studying Lesson C, you should be able to: Prevent the user from adding and deleting records Remove buttons from a BindingNavigator control Add a label, text box, and button to a BindingNavigator control Programming with Microsoft Visual Basic 2008, Fourth Edition64

Completing the Paradise Bookstore Application Requirements for Paradise Bookstore application: –Display records from the Books database –Allow store manager to enter an author’s name (or part of a name) to display books by that author Database contains one table, tblBooks, with 5 fields and 11 records User should not be allowed to add or delete records –Must modify the BindingNavigatorControl to remove the add and delete buttons Programming with Microsoft Visual Basic 2008, Fourth Edition65

Completing the Paradise Bookstore Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition66 Figure 13-41: tblBooks table in the Books database

Completing the Paradise Bookstore Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition67 Figure 13-42: Completed TblBooksBindingNavigator control

Coding the Paradise Bookstore Application Go button’s Click event should display only records whose Author field starts with the characters entered in the txtAuthor text box –If text box is empty, display all records –Use the LINQ LIKE operator Total Value button’s Click event should display the total value of all books in the store –Use the LINQ aggregate function SUM Programming with Microsoft Visual Basic 2008, Fourth Edition68 Example Program

Coding the Paradise Bookstore Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition69 Figure 13-43: Books written by authors whose name begins with s Click Go 1 2 enter s result 3

Coding the Paradise Bookstore Application (continued) Programming with Microsoft Visual Basic 2008, Fourth Edition70 Figure 13-44: Message box showing the total value of the inventory Click Go 1 2 result

Programming with Microsoft Visual Basic 2008, Fourth Edition71 Figure 13-45: Click event procedures for the btnGo and btnTotal controls Coding the Paradise Bookstore Application (continued)

Lesson C Summary Use the DataGridView control’s task box to prevent the user from adding or deleting records in the control Remove items from the BindingNavigator control to prevent the user from performing some actions Can customize the BindingNavigator control by adding a label, text box, or button to it Programming with Microsoft Visual Basic 2008, Fourth Edition72