Working with Access Databases.pdf

Embed Size (px)

Citation preview

  • 8/10/2019 Working with Access Databases.pdf

    1/73

  • 8/10/2019 Working with Access Databases.pdf

    2/73

    Previewing the

    Paradise Bookstore ApplicationOpen the Paradise.exe file

    Application displays records in the Books database

    Allows the store manager to enter an authors name (or part of a name)

    Displays only books written by the author

    Displays the total value of books in the store

    2

  • 8/10/2019 Working with Access Databases.pdf

    3/73

    3

    Figure 13-1 Books written by Carol Smith

  • 8/10/2019 Working with Access Databases.pdf

    4/73

    Lesson A ObjectivesAfter 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

    4

  • 8/10/2019 Working with Access Databases.pdf

    5/73

    Lesson A Objectives (contd.)Customize a DataGridView controlHandle errors using the TryCatch statement

    Position the record pointer in a dataset

    5

  • 8/10/2019 Working with Access Databases.pdf

    6/73

    Database TerminologyComputer 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 a person, place, or thing

    6

  • 8/10/2019 Working with Access Databases.pdf

    7/73

    Database Terminology

    (contd.)Record Group of related fields that contain all the necessary data about a specific

    person, place, or thing

    Table: Group of related records

    Each record in a table pertains to the same topic and contains the same typeof information

    Primary key: Field uniquely identifying a record

    A two-table database has parent and child tables

    Foreign key

    Field that links a child record to a parent record

    7

  • 8/10/2019 Working with Access Databases.pdf

    8/73

    Database Terminology

    (contd.)

    8

    Figure 13-3 Example of a one-table relational database

  • 8/10/2019 Working with Access Databases.pdf

    9/739

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

  • 8/10/2019 Working with Access Databases.pdf

    10/73

    Database Terminology

    (contd.)Advantages of a relational database Data can be retrieved quickly and easily

    Data can be displayed in any order

    You can control how much data to view

    10

  • 8/10/2019 Working with Access Databases.pdf

    11/73

    Connecting an Application to

    a Microsoft Access DatabaseMS Access 2007 database has a file extension of .accdbSample database contains one table named tblEmploy, containingemployee information

    Must create a database connection to access data

    Data Source Configuration Wizard

    Helps you connect an application to a database

    Dataset

    Temporary copy of the data you have requested from the database

    11

  • 8/10/2019 Working with Access Databases.pdf

    12/7312

    Figure 13-5 Data contained in the tblEmploy table

    l

  • 8/10/2019 Working with Access Databases.pdf

    13/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)Open the Solution Explorer window and auto-hide the Propertieswindow

    Open the Morgan Industries Solution file

    Click the View menu and then click Server Explorer (Visual Studio) orDatabase Explorer (Visual Basic Express)

    13

    C i A li i

  • 8/10/2019 Working with Access Databases.pdf

    14/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)Click the Data on the menu bar and then click Show Data Sources toopen Data Sources window

    Click Add New Data Source button, and then select Database

    Click New Connection button, and then select Microsoft AccessDatabase File (OLE DB)

    Click Browse, navigate to database file, and then click Open

    14

  • 8/10/2019 Working with Access Databases.pdf

    15/73

    15

    Figure 13-6 Completed Add Connection dialog box

    C i A li i

  • 8/10/2019 Working with Access Databases.pdf

    16/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)Click Test Connection button, and if successful, click NextClick Yes to include the database file in the current project

    Click Yes to save connection string

    On Choose Your Database Objects screen, select all objects

    16

    C ti A li ti t

  • 8/10/2019 Working with Access Databases.pdf

    17/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)

    17

    Figure 13-7 Message regarding copying the database file

    C ti A li ti t

  • 8/10/2019 Working with Access Databases.pdf

    18/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)

    18

    Figure 13-8 Objects selected in the Choose Your Database Objects screen

    C ti A li ti t

  • 8/10/2019 Working with Access Databases.pdf

    19/73

    Connecting an Application to aMicrosoft Access Database

    (contd.)

    19

    Figure 13-9 Result of running the Data Source Configuration Wizard

  • 8/10/2019 Working with Access Databases.pdf

    20/73

    Previewing the Contents of a

    DatasetClick the form to make it the active windowClick Data on the menu bar and then click Preview Data to openPreview Data dialog box

    Select the object to preview and then click Preview

    After previewing the data, click Close button

    20

  • 8/10/2019 Working with Access Databases.pdf

    21/73

    21

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

  • 8/10/2019 Working with Access Databases.pdf

    22/73

    Binding the Objects in a

    DatasetMust bind one or more objects in the dataset to controls in theinterface 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

    22

  • 8/10/2019 Working with Access Databases.pdf

    23/73

    23

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

  • 8/10/2019 Working with Access Databases.pdf

    24/73

    Having the Computer Create aBound ControlWhen 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 rowsrepresenting records, columns representing fields

    Use list arrow to change the type of control linked to the object

    24

  • 8/10/2019 Working with Access Databases.pdf

    25/73

  • 8/10/2019 Working with Access Databases.pdf

    26/73

    26

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

  • 8/10/2019 Working with Access Databases.pdf

    27/73

    27

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

  • 8/10/2019 Working with Access Databases.pdf

    28/73

    28

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

  • 8/10/2019 Working with Access Databases.pdf

    29/73

  • 8/10/2019 Working with Access Databases.pdf

    30/73

    30

    Figure 13-16 Illustration of the relationships among the database,

    the objects in the component tray, and the bound controls

  • 8/10/2019 Working with Access Databases.pdf

    31/73

    The DataGridView ControlDataGridView control

    Displays data in a row and column format

    Cell: Intersection of a row and column

    DataGridViews task list allows you to add, reorder, and remove columnsand set properties of bound columns

    AutoSizeColumnsMode: Has seven settings that control how thecolumn widths are sized

    Fill setting: Automatically adjusts the column widths so that the display area

    of the control is filled

    31

  • 8/10/2019 Working with Access Databases.pdf

    32/73

    32

    Figure 13-17 DataGridView controls task list

  • 8/10/2019 Working with Access Databases.pdf

    33/73

    33

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

  • 8/10/2019 Working with Access Databases.pdf

    34/73

    34

    Figure 13-19 Edit Columns dialog box

  • 8/10/2019 Working with Access Databases.pdf

    35/73

    35

    Figure 13-20 Completed CellStyle Builder dialog box

  • 8/10/2019 Working with Access Databases.pdf

    36/73

    36

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

  • 8/10/2019 Working with Access Databases.pdf

    37/73

  • 8/10/2019 Working with Access Databases.pdf

    38/73

    Visual Basic Code (contd.)

    38

    Figure 13-22 Code automatically entered in the Code Editor window

  • 8/10/2019 Working with Access Databases.pdf

    39/73

    Handling Errors in the CodeException

    Error that occurs while an application is running

    TryCatch 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

    39

  • 8/10/2019 Working with Access Databases.pdf

    40/73

    40

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

  • 8/10/2019 Working with Access Databases.pdf

    41/73

    41

    Figure 13-23

    Syntax and examples of the

    TryCatch statement (contd.)

  • 8/10/2019 Working with Access Databases.pdf

    42/73

    42

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

  • 8/10/2019 Working with Access Databases.pdf

    43/73

    43

    Figure 13-25 Data displayed in the DataGridView control

  • 8/10/2019 Working with Access Databases.pdf

    44/73

    The Copy to Output DirectoryPropertyCopy to Output Directory property: Determines the way Visual Basicsaves changes to local file

    Copy always (default setting):

    Database file is copied to projects bin\Debug folder each time the

    application starts Result: database file appears in two different folders

    Changes to file in 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

    44

  • 8/10/2019 Working with Access Databases.pdf

    45/73

    Binding to an Existing ControlCan bind an object in a dataset to an existing control on the form in twoways:

    Drag object from Data Sources window to control

    Set one or more of the controls properties in the Properties window

    Properties to set depend on the type of control being bound:

    DataGridView: Set DataSource property

    ListBox: Set DataSource and DisplayMember properties

    Label or text box: Set DataBindings /Text property

    45

  • 8/10/2019 Working with Access Databases.pdf

    46/73

  • 8/10/2019 Working with Access Databases.pdf

    47/73

    47

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

  • 8/10/2019 Working with Access Databases.pdf

    48/73

    48

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

  • 8/10/2019 Working with Access Databases.pdf

    49/73

    49

    Figure 13-31 Applications code

  • 8/10/2019 Working with Access Databases.pdf

    50/73

    Lesson A SummaryUse Data Source Configuration Wizard to connect an application to adatabase

    Use the Preview Data command on the Data menu to preview the datain a dataset

    Bind an object in a dataset by dragging it to the form and letting thecomputer create a control or by dragging it onto an existing control

    Use the Fill setting of the DataGridViews AutoSizeColumnsModeproperty to have columns fill the display area

    50

  • 8/10/2019 Working with Access Databases.pdf

    51/73

    Lesson A Summary (contd.)Use Dock property of DataGridView to anchor it to the borders of form

    Use the TryCatch statement to handle exceptions (errors) occurringduring run time

    Use BindingSource objects Position property or its Move methods tomove record pointer while the application is running

    51

  • 8/10/2019 Working with Access Databases.pdf

    52/73

    Lesson B ObjectivesAfter studying Lesson B, you should be able to:

    Query a dataset using LINQ

    Customize a BindingNavigator control

    Use the LINQ aggregate operators

    52

  • 8/10/2019 Working with Access Databases.pdf

    53/73

    Creating a QueryQuery

    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 2010

    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 morefields

    53

  • 8/10/2019 Working with Access Databases.pdf

    54/73

  • 8/10/2019 Working with Access Databases.pdf

    55/73

    55

    Figure 13-33 Syntax and an example of assigning a LINQ variables

    contents to a BindingSource object

  • 8/10/2019 Working with Access Databases.pdf

    56/73

    56

    Figure 13-34 Code entered in the General Declarations section

    and btnFind Click event procedure

  • 8/10/2019 Working with Access Databases.pdf

    57/73

    57

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

  • 8/10/2019 Working with Access Databases.pdf

    58/73

    Personalizing aBindingNavigator ControlCan add additional items to a BindingNavigator control to personalize it

    Button

    Textbox

    Drop-down button

    58

  • 8/10/2019 Working with Access Databases.pdf

    59/73

    59

    Figure 13-36 Manipulating the items on a BindingNavigator control

  • 8/10/2019 Working with Access Databases.pdf

    60/73

    60

    Figure 13-37 Items Collection Editor dialog box

  • 8/10/2019 Working with Access Databases.pdf

    61/73

    61

    Figure 13-38 DropDownButton added to the TblEmployBindingNavigator control

    h

  • 8/10/2019 Working with Access Databases.pdf

    62/73

    Using the LINQ AggregateOperatorsAggregate operator

    Returns a single value from a group of values

    LINQ provides several aggregate operators

    Average

    Count

    Max

    Min

    Sum

    62

  • 8/10/2019 Working with Access Databases.pdf

    63/73

    63

    Figure 13-39

    Syntax and examples of theLINQ aggregate operators

  • 8/10/2019 Working with Access Databases.pdf

    64/73

    Lesson B SummaryUse LINQ to select and arrange records in a dataset

    You can customize the BindingNavigator control by adding additionalitems to it

    LINQ provides aggregate operators that return a single value from agroup of values

    64

  • 8/10/2019 Working with Access Databases.pdf

    65/73

    C l i h P di

  • 8/10/2019 Working with Access Databases.pdf

    66/73

    Completing the ParadiseBookstore ApplicationRequirements for Paradise Bookstore application

    Display records from the Books database

    Allow store manager to enter an authors name (or part of a name) to displaybooks by that author

    Database contains one table, tblBooks Has five fields and 11 records

    User should not be allowed to add or delete records

    Must modify the BindingNavigatorControl to remove the add and deletebuttons

    66

  • 8/10/2019 Working with Access Databases.pdf

    67/73

    67

    Figure 13-42 tblBooks table in the Books database

  • 8/10/2019 Working with Access Databases.pdf

    68/73

    68

    Figure 13-44 Completed TblBooksBindingNavigator control

    C di th P di B k t

  • 8/10/2019 Working with Access Databases.pdf

    69/73

    Coding the Paradise BookstoreApplicationGo buttons Click event procedure

    Displays only records whose Author field starts with the characters enteredin the text box

    If text box is empty, display all records

    Use the LINQ LIKE operator

    Total Value buttons Click event procedure

    Displays the total value of all books in the store

    Use the LINQ aggregate function SUM

    69

  • 8/10/2019 Working with Access Databases.pdf

    70/73

    70

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

  • 8/10/2019 Working with Access Databases.pdf

    71/73

  • 8/10/2019 Working with Access Databases.pdf

    72/73

    72

    Figure 13-47

    Click event procedures

    for the btnGo and btnTotalcontrols

  • 8/10/2019 Working with Access Databases.pdf

    73/73

    Lesson C SummaryUse the DataGridView controls task box to prevent the user fromadding or deleting records in the control

    Remove items from the BindingNavigator control to prevent the userfrom performing some actions

    Can customize the BindingNavigator control by adding a label, text box,or button to it