Programming with Microsoft Visual Basic 2012 Chapter 13: 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.
Microsoft Office 2010 Access Chapter 1 Creating and Using a Database.
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.
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.
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 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
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.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
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
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 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.
1 Data Bound Controls II Chapter Objectives You will be able to Use a Data Source control to get data from a SQL database and make it available.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Thirteen Working with Access Databases and LINQ.
Lesson 17 Getting Started with Access Essentials
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Key Applications Module Lesson 21 — Access Essentials
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic th Edition.
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.
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.
Chapter Fourteen Access Databases and SQL Programming with Microsoft Visual Basic th Edition.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 8 – Integrating a Database with a FrontPage.
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.
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 Five More on the Selection Structure 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.
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.
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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Visual Basic 2010 How to Program
© 2013, Mike Murach & Associates, Inc.
Brief description on how to navigate within this presentation (ppt)
CIS16 Application Programming with Visual Basic
CIS16 Application Programming with Visual Basic
Database Applications
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
Chapter 10 Accessing Database Files
Introduction to ADO.Net and Visual Studio Database Tools.
Presentation transcript:

Programming with Microsoft Visual Basic 2012 Chapter 13: Working with Access Databases and LINQ

Previewing the Paradise Bookstore Application Programming with Microsoft Visual Basic Figure 13-1 Books written by Carol Smith Figure 13-2 Total value of the inventory 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

Lesson A Objectives After studying Lesson A, you should be able to: Define basic database terminology 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 Customize a DataGridView control Handle errors using the Try…Catch statement Position the record pointer in a dataset Programming with Microsoft Visual Basic 20123

Computer database –An electronic file containing an organized collection of related information Relational database –A 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 –A single piece of information about a person, place, or thing Programming with Microsoft Visual Basic Database Terminology

Record –A group of related fields that contain all the necessary data about a specific person, place, or thing Table –A group of related 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 –A field that links a record in child table to a record in parent table Programming with Microsoft Visual Basic Database Terminology (cont.)

Programming with Microsoft Visual Basic Table Examples in Relational Database Figure 13-3 Example of a one-table relational database Figure 13-4 Example of a two-table relational database

Programming with Microsoft Visual Basic Connecting an Application to a Microsoft Access Database MS Access database has a file extension of.accdb Must create a database connection to access data The connection is made using the Data Source Configuration Wizard –Data Source Configuration Wizard Helps you connect an application to a specified database –The computer makes a copy of the specified data and stores the copy in its internal memory –The copy of the data you want to access is called a dataset

Programming with Microsoft Visual Basic Data Contained in tblEmploy Table Figure 13-5 Data contained in the tblEmploy table

Connecting an Application to a Microsoft Access Database (cont’d.) Open the Solution Explorer window Open the Morgan Industries Solution file –Morgan Industries Solution-DataGridView Click the View menu and then click Server Explorer (Visual Studio) or Database Explorer (Visual Basic Express) 實作練習 : 需下載 Chap13 的 Morgan Industries Solution-DataGridView Access Databases\Employees.accdb 9 9

Connecting an Application to a Microsoft Access Database (cont’d.) Click View on the menu bar, pointing to Other Windows and then click Data Sources to open Data Sources window Click Add New Data Source button, and then select Database to open Add Connection window 1.Choose type of database (data source) Click New Connection button, and then select Microsoft Access Database File (OLE DB) 2.Choose file name of database Click Browse, navigate to database file, and then click Open 3.Click Test Connection button, and if successful, click Next 實作練習 在第 768~771 頁 10 screen 10

11

Connecting an Application to a Microsoft Access Database (cont’d.) Click Yes to include the database file in the current project Click Yes to save connection string On Choose Your Database Objects screen, select tblEmployee table of the Employees.accdb database 12 screen 12

Programming with Microsoft Visual Basic 2010, 5 th Edition Connecting an Application to a Microsoft Access Database (cont’d.) Figure 13-8 Message regarding copying the database file 13

Programming with Microsoft Visual Basic 2010, 5 th Edition Connecting an Application to a Microsoft Access Database (cont’d.) Figure 13-9 Objects selected in the Choose Your Database Objects screen 14

Programming with Microsoft Visual Basic 2010, 5 th Edition Connecting an Application to a Microsoft Access Database (cont’d.) Figure 13-9 Result of running the Data Source Configuration Wizard 15

Previewing the Contents of a Dataset Right-clicking EmployeesDataSet in the Data Sources window and then clicking Preview Data 實作練習 在第 771~772 頁 16 screen 16

Figure Data displayed in the Preview Data dialog box 17 Preview Data dialog Box 17

You must bind one or more objects in the dataset to controls in the interface to view dataset contents Binding –Connecting an 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 Binding the Objects in a Dataset Figure Two ways to bind an object in a dataset

Programming with Microsoft Visual Basic Figure Icons in the Data Sources window Figure Result of clicking the tblEmploy object’s list arrow Binding the Objects in a Dataset (cont.) Having the Computer Create a Bound Control When you drag a dataset object onto a form: –The computer creates the control (its type is indicated by an icon) –The dataset object is automatically bound to the control Example: –Drag the tblEmployee table object to the form –The DataGridView control is created to display tabular data, with rows representing records and columns representing fields Use the list arrow to change the type of control linked to the object

Programming with Microsoft Visual Basic Figure Result of clicking the Last_Name object’s list arrow Figure Result of dragging the table object to the form Binding the Objects in a Dataset (cont.) Having the Computer Create a Bound Control (cont.) 實作練習 在 第 774 頁

Having the Computer Create a Bound Control (cont.) 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 Figure Illustration of the relationships among the database, the objects in the component tray, and the bound controls Binding the Objects in a Dataset (cont.)

Programming with Microsoft Visual Basic The DataGridView Control DataGridView control –Displays data in a row and column format –Cell Intersection of a row and a column –The DataGridView’s task list allows you to add, reorder, and remove columns and set properties of bound columns –AutoSizeColumnsMode property 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 Binding the Objects in a Dataset (cont.)

Programming with Microsoft Visual Basic Figure DataGridView control’s task list DataGridView’s Task List The DataGridView Control (cont.)

Programming with Microsoft Visual Basic Figure Edit Columns dialog box of DataGridView Control The DataGridView Control (cont.) Edit Columns Dialog Box of DataGridView Control

Programming with Microsoft Visual Basic Figure Completed Format String Dialog box Figure Completed CellStyle Builder dialog box CellStyle Builder Dialog Box of DataGridView Control 實作練習 第 778~780 頁

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 –The BindingNavigator’s save event is also coded 26 Visual Basic Code Programming with Microsoft Visual Basic 2012 Figure Code automatically entered in the Code Editor window

Programming with Microsoft Visual Basic Handling Errors in the Code Exception –An 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

Handling Errors in the Code (cont.) Programming with Microsoft Visual Basic Handling Errors – Syntax & Examples Figure Basic syntax and examples of the Try…Catch statement

Handling Errors in the Code (cont.) Programming with Microsoft Visual Basic Visual Basic Code (cont.) Figure Dataset displayed in the DataGridView control Figure Tooltip for the Move last button Figure Completed Click event procedure for the Save Data button 實作練習 第 782~783 頁

Copy to Output Directory property –Determines the way VB saves changes to a local file –Copy always (default setting) The database file is copied to the project’s bin\Debug folder each time the application starts Result: The database file appears in two different folders Changes to the file in the bin\Debug folder are overwritten –Copy if newer Preserves run-time changes Copies over the file in bin\Debug only if it is not current Programming with Microsoft Visual Basic The Copy to Output Directory Property

Programming with Microsoft Visual Basic The Copy to Output Directory Property (cont.) Figure Settings for the Copy to Output Directory property 實作練習 第 785 頁

You can bind an object in a dataset to an existing control on the form in two ways: 1.Drag the object from the Data Sources window to the control 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: –DataGridView: Set the DataSource property –ListBox: Set the DataSource and DisplayMember properties –Label or text box: Set the DataBindings /Text property Programming with Microsoft Visual Basic Binding to an Existing Control

Programming with Microsoft Visual Basic Binding to an Existing Control (cont.) Figure A different version of the Morgan Industries application Figure First record displayed in the interface Figure Result of binding a field to an existing control 實作練習 第 787~788 頁 需下載 Morgan Industries Solution-Labels

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

Coding the Next Record and Previous Record Buttons (cont.) Programming with Microsoft Visual Basic Figure Syntax and examples of the BindingSource object’s Position property Figure Syntax and examples of the BindingSource object’s Move methods

Programming with Microsoft Visual Basic Figure btnNext_Click and btnPrevious_Click procedures Coding the Next Record and Previous Record Buttons (cont.) 實作練習 第 788~790 頁

Lesson A Summary Use the Data Source Configuration Wizard to connect an application to a database Use the Preview Data command 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

Lesson A Summary (cont.) Use the Dock property of DataGridView to anchor it to the borders of a form Use the Try…Catch statement to handle exceptions (errors) occurring during run time Use the BindingSource object’s Position property or its Move methods to move the record pointer while the application is running Programming with Microsoft Visual Basic

Lesson B Objectives After studying Lesson B, you should be able to: Query a dataset using LINQ Customize a BindingNavigator control Use the LINQ aggregate operators Programming with Microsoft Visual Basic

Programming with Microsoft Visual Basic Creating a Query Query –Specifies records to select from a dataset and the order in which to arrange them Language Integrated Query (LINQ) –Used to create queries in Visual Basic Where clause –Contains a condition, or conditions, 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 Creating a Query (cont.) Figure Basic LINQ syntax and examples for selecting and arranging records in a dataset

Programming with Microsoft Visual Basic Figure Syntax and an example of assigning a LINQ variable’s contents to a BindingSource object Creating a Query (cont.)

Programming with Microsoft Visual Basic Figure Code entered in the General Declarations section and btnFind Click event procedure Creating a Query (cont.)

Programming with Microsoft Visual Basic Figure Employees whose last name begins with the letter S Creating a Query (cont.) 實作練習 第 798~800 頁 需下載 Morgan Industries Solution-LINQ

You can add additional items to a BindingNavigator control to personalize it –Button, Textbox, Drop-down button Programming with Microsoft Visual Basic Customizing a BindingNavigator Control Figure Instructions for customizing a BindingNavigator control

Programming with Microsoft Visual Basic Customizing a BindingNavigator Control (cont.) Figure Items Collection Editor dialog box

Programming with Microsoft Visual Basic Figure DropDownItems property in the Items Collection Editor dialog box Customizing a BindingNavigator Control (cont.)

Programming with Microsoft Visual Basic Figure DropDownButton added to the TblEmployBindingNavigator control Customizing a BindingNavigator Control (cont.) 實作練習 第 800~802 頁 需下載 Morgan Industries Solution-Aggregate

Programming with Microsoft Visual Basic 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 Figure Syntax and examples of the LINQ aggregate operators

Programming with Microsoft Visual Basic Figure Code entered in each menu item’s Click event procedure Figure Message box showing the average pay rate for all employees Using the LINQ Aggregate Operators (cont.) 實作練習 第 803~806 頁 UI

Programming with Microsoft Visual Basic Lesson B Summary Use LINQ to select and arrange 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

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, a text box, and a button to a BindingNavigator control Programming with Microsoft Visual Basic

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

Completing the Paradise Bookstore Application (cont.) Programming with Microsoft Visual Basic Figure tblBooks table in the Books database Figure Completed TblBooksBindingNavigator control Figure Completed Items Collection Editor dialog box 實作練習 第 800~802 頁 需下載 Books.accdb 需下載 Paradise Bookstore Customizing BindingNavigator Program SPEC

Coding the Paradise Bookstore Application Programming with Microsoft Visual Basic Go button’s Click event procedure –Displays only records whose Author field starts with the characters entered in the text box If the text box is empty, it displays all records –Use the LINQ LIKE operator Total Value button’s Click event procedure –Displays the total value of all books in the store –Use the LINQ aggregate function SUM

Coding the Paradise Bookstore Application (cont.) Programming with Microsoft Visual Basic Figure Message box showing the total value of the inventory Figure Books written by authors whose names begin with s

Programming with Microsoft Visual Basic Figure btnGo_Click and btnTotal_Click procedures Coding the Paradise Bookstore Application (cont.) 實作練習 第 813~815 頁

Programming with Microsoft Visual Basic Lesson C Summary Use the DataGridView control’s task box to prevent the user from adding or deleting records in the control To delete items from a BindingNavigator control: –Click the BindingNavigator control’s task box and then click Edit Items –In the Members list, click the item you want to remove To add controls to a BindingNavigator control: –Click the BindingNavigator control’s task box and then click Edit Items –Use the “Select item and add to list below” box and the Add button to add the appropriate control