+ All Categories
Home > Documents > 9781111529437_PPT_ch13.ppt

9781111529437_PPT_ch13.ppt

Date post: 12-Nov-2015
Category:
Upload: obert-sanyambe
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
73
Chapter Thirteen Working with Access Databases and LINQ Programming with Microsoft Visual Basic 2010 5 th Edition
Transcript
  • Chapter ThirteenWorking with Access Databases and LINQProgramming with Microsoft Visual Basic 2010 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Previewing the Paradise Bookstore Application*Open the Paradise.exe file Application displays records in the Books databaseAllows the store manager to enter an authors name (or part of a name) Displays only books written by the authorDisplays the total value of books in the store

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-1 Books written by Carol Smith

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson A Objectives*After studying Lesson A, you should be able to: Define basic database terminologyConnect an application to a Microsoft Access databaseBind table and field objects to controlsExplain the purpose of the DataSet, BindingSource, TableAdapter, TableAdapterManager, and BindingNavigator objects

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson A Objectives (contd.)*Customize a DataGridView controlHandle errors using the TryCatch statementPosition the record pointer in a dataset

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Database Terminology*Computer databaseElectronic file containing an organized collection of related information Relational databaseDatabase that stores information in tables composed of rows and columnsEach column in a table represents a fieldEach row in a table represents a recordFieldSingle piece of information about a person, place, or thing

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Database Terminology (contd.)*RecordGroup of related fields that contain all the necessary data about a specific person, place, or thingTable: Group of related recordsEach record in a table pertains to the same topic and contains the same type of informationPrimary key: Field uniquely identifying a record A two-table database has parent and child tablesForeign keyField that links a child record to a parent record

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Database Terminology (contd.)*Figure 13-3 Example of a one-table relational database

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-4 Example of a two-table relational database

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Database Terminology (contd.)*Advantages of a relational databaseData can be retrieved quickly and easilyData can be displayed in any orderYou can control how much data to view

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database*MS Access 2007 database has a file extension of .accdbSample database contains one table named tblEmploy, containing employee informationMust create a database connection to access dataData Source Configuration WizardHelps you connect an application to a databaseDatasetTemporary copy of the data you have requested from the database

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-5 Data contained in the tblEmploy table

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Open the Solution Explorer window and auto-hide the Properties windowOpen the Morgan Industries Solution fileClick the View menu and then click Server Explorer (Visual Studio) or Database Explorer (Visual Basic Express)

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Click the Data on the menu bar and then click Show Data Sources to open Data Sources windowClick Add New Data Source button, and then select DatabaseClick New Connection button, and then select Microsoft Access Database File (OLE DB)Click Browse, navigate to database file, and then click Open

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-6 Completed Add Connection dialog box

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Click Test Connection button, and if successful, click NextClick Yes to include the database file in the current projectClick Yes to save connection stringOn Choose Your Database Objects screen, select all objects

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Figure 13-7 Message regarding copying the database file

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Figure 13-8 Objects selected in the Choose Your Database Objects screen

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Connecting an Application to a Microsoft Access Database (contd.)*Figure 13-9 Result of running the Data Source Configuration Wizard

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Previewing the Contents of a Dataset*Click the form to make it the active windowClick Data on the menu bar and then click Preview Data to open Preview Data dialog box Select the object to preview and then click Preview After previewing the data, click Close button

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-10 Data displayed in the Preview Data dialog box

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Binding the Objects in a Dataset *Must bind one or more objects in the dataset to controls in the interface to view dataset contents BindingConnecting an object to a controlBound controlsConnected controlsTypes of controls used to bind dataset objectsComputer-created controlExisting control on the form

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-11 Ways to bind an object in a dataset

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    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 controlExampleDrag tblEmployee table object to the formDataGridView control is created to display tabular data, with rows representing records, columns representing fieldsUse list arrow to change the type of control linked to the object

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-12 Icons in the Data Sources window

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-13 Result of clicking the tblEmploy objects list arrow

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-14 Result of clicking the Last_Name objects list arrow

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-15 Result of dragging the table object to the form

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Having the Computer Create a Bound Control (contd.)*In addition to a control, the computer adds:BindingNavigator control: To move from one record to the next in the datasetTableAdapter object: Connects the database to the DataSet object; responsible for retrieving data and storing it in the DataSetTableAdapterManager object: Handles saving data to multiple tables in the DataSetBindingSource object: Provides the connection between the DataSet and the bound controls

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-16 Illustration of the relationships among the database, the objects in the component tray, and the bound controls

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    The DataGridView Control*DataGridView controlDisplays data in a row and column formatCell: Intersection of a row and columnDataGridViews task list allows you to add, reorder, and remove columns and set properties of bound columnsAutoSizeColumnsMode: Has seven settings that control how the column widths are sizedFill setting: Automatically adjusts the column widths so that the display area of the control is filled

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-17 DataGridView controls task list

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-18 Purpose of each task in the DataGridViews task list

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-19 Edit Columns dialog box

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-20 Completed CellStyle Builder dialog box

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-21 DataGridView control after setting some of its properties

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Visual Basic Code*When a table or field object is dragged onto a form, the computer also enters code in the Code Editor windowCode in the forms Load event uses the TableAdapter object to retrieve dataBindingNavigators save event is also coded

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Visual Basic Code (contd.)*Figure 13-22 Code automatically entered in the Code Editor window

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Handling Errors in the Code*ExceptionError that occurs while an application is runningTryCatch statementUsed to intercept exceptions and handle themTry blockContains the statements that might failCatch blockContains the code to handle the exceptions

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-23 Syntax and examples of the TryCatch statement (continues)

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-23 Syntax and examples of the TryCatch statement (contd.)

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-24 Completed Click event procedure for the Save Data button

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-25 Data displayed in the DataGridView control

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    The Copy to Output Directory Property*Copy to Output Directory property: Determines the way Visual Basic saves changes to local fileCopy always (default setting):Database file is copied to projects bin\Debug folder each time the application startsResult: database file appears in two different folders Changes to file in bin\Debug folder are overwrittenCopy if newer: Preserves run-time changesCopies over the file in bin\Debug only if it is not current

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Binding to an Existing Control*Can bind an object in a dataset to an existing control on the form in two ways:Drag object from Data Sources window to controlSet one or more of the controls properties in the Properties windowProperties to set depend on the type of control being bound:DataGridView: Set DataSource propertyListBox: Set DataSource and DisplayMember properties Label or text box: Set DataBindings /Text property

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Coding the Next Record and Previous Record Buttons*BindingSource objects Position propertyStores an invisible record pointer Positions are integer values 0 First record is at position 0BindingSource objects Move methodsCan 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 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-29 Syntax and examples of the BindingSource objects Position property

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-30 Syntax and examples of the BindingSource objects Move methods

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-31 Applications code

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson A Summary*Use Data Source Configuration Wizard to connect an application to a databaseUse the Preview Data command on the Data menu to preview the data in a datasetBind 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 controlUse the Fill setting of the DataGridViews AutoSizeColumnsMode property to have columns fill the display area

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson A Summary (contd.)*Use Dock property of DataGridView to anchor it to the borders of formUse the TryCatch statement to handle exceptions (errors) occurring during run timeUse BindingSource objects Position property or its Move methods to move record pointer while the application is running

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson B Objectives*After studying Lesson B, you should be able to: Query a dataset using LINQCustomize a BindingNavigator controlUse the LINQ aggregate operators

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Creating a Query*QuerySpecifies records to select from a dataset and the order in which to arrange themLanguage Integrated Query (LINQ)Used to create queries in Visual Basic 2010 Where clauseContains a condition to limit the records to be selectedOrder By clauseUsed to arrange the records in ascending or descending order by one or more fields

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-32 Basic LINQ syntax and examples for selecting and arranging records in a dataset

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-33 Syntax and an example of assigning a LINQ variables contents to a BindingSource object

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-34 Code entered in the General Declarations section and btnFind Click event procedure

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-35 Employees whose last name begins with the letter S

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Personalizing a BindingNavigator Control*Can add additional items to a BindingNavigator control to personalize itButtonTextboxDrop-down button

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-36 Manipulating the items on a BindingNavigator control

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-37 Items Collection Editor dialog box

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-38 DropDownButton added to the TblEmployBindingNavigator control

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Using the LINQ Aggregate Operators*Aggregate operatorReturns a single value from a group of valuesLINQ provides several aggregate operatorsAverageCountMaxMinSum

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-39 Syntax and examples of the LINQ aggregate operators

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson B Summary*Use LINQ to select and arrange records in a datasetYou can customize the BindingNavigator control by adding additional items to itLINQ provides aggregate operators that return a single value from a group of values

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson C Objectives*After studying Lesson C, you should be able to: Prevent the user from adding and deleting recordsRemove buttons from a BindingNavigator controlAdd a label, text box, and button to a BindingNavigator control

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Completing the Paradise Bookstore Application*Requirements for Paradise Bookstore application Display records from the Books databaseAllow store manager to enter an authors name (or part of a name) to display books by that authorDatabase contains one table, tblBooksHas five fields and 11 recordsUser should not be allowed to add or delete recordsMust modify the BindingNavigatorControl to remove the add and delete buttons

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-42 tblBooks table in the Books database

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-44 Completed TblBooksBindingNavigator control

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Coding the Paradise Bookstore Application*Go buttons Click event procedureDisplays only records whose Author field starts with the characters entered in the text boxIf text box is empty, display all recordsUse the LINQ LIKE operatorTotal Value buttons Click event procedureDisplays the total value of all books in the storeUse the LINQ aggregate function SUM

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-45 Books written by authors whose name begins with s

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Coding the Paradise Bookstore Application (contd.)*Figure 13-46 Message box showing the total value of the inventory

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    *Figure 13-47 Click event procedures for the btnGo and btnTotal controls

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Programming with Microsoft Visual Basic 2010, 5th Edition

    Lesson C Summary*Use the DataGridView controls task box to prevent the user from adding or deleting records in the controlRemove items from the BindingNavigator control to prevent the user from performing some actionsCan customize the BindingNavigator control by adding a label, text box, or button to it

    Programming with Microsoft Visual Basic 2010, 5th Edition

    ************************************************


Recommended