35
WORKING WITH ACCESS DATABASES AND LINQ

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)

Embed Size (px)

Citation preview

WORKING WITH ACCESS DATABASES AND LINQ

PREVIEWING THE PARADISE BOOKSTORE APPLICATION 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) Displays only books written by the author

Displays the total value of books in the store

2

3

Figure 13-1 Books written by Carol Smith

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

4

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

5

DATABASE TERMINOLOGY (CONT’D.) 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 type of 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

6

DATABASE TERMINOLOGY (CONT’D.)

7

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

8

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

DATABASE TERMINOLOGY (CONT’D.) 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

9

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 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

10

11

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

12

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

HAVING THE COMPUTER CREATE A BOUND CONTROL (CONT’D.) 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

13

14

Figure 13-16 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

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

15

16

Figure 13-17 DataGridView control’s task list

17

Figure 13-18 Purpose of each task in the DataGridView’s task list

18

Figure 13-19 Edit Columns dialog box

19

Figure 13-20 Completed CellStyle Builder dialog box

20

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

21

BINDING TO AN EXISTING CONTROL Open the Morgan Industries Solution (Morgan Industries

Solution.sln) file contained in the VB2010\Chap13\Morgan Industries Solution-Labels folder

22

ทำ��ก�รเชื่�อมต่ อฐ�นข้�อม�ลด้�วย Data Source Configuration Wizard

23

CODING THE NEXT RECORD AND PREVIOUS RECORD BUTTONS

24

25

LESSON B

After studying Lesson B, you should be able to:

• Query a dataset using LINQ

• Customize a BindingNavigator control

• Use the LINQ aggregate operators

26

CREATING A QUERY

You use a query to specify both the records to select in a dataset and the order in which to arrange the records.

You can create a query in Visual Basic using a language feature called Language Integrated Query ( LINQ)

27

28

29

30

31

PERSONALIZING A BINDINGNAVIGATOR CONTROL

32

USING THE LINQ AGGREGATE OPERATORS

33

34

LESSON C

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

35

COMPLETING THE PARADISE BOOKSTORE APPLICATION