Upload
aleesha-leonard
View
231
Download
4
Tags:
Embed Size (px)
Citation preview
Chapter 24 I’m Suffering from Information Overload (Access Databases)
Clearly Visual Basic: Programming with Visual Basic 2010
2nd Edition
Objectives
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition2
After studying Chapter 24, you should be able to:Define basic database terminologyConnect an application to a Microsoft Access databaseBind table and field objects to controlsCustomize a DataGridView controlHandle exceptions using the Try…Catch statementPosition the record pointer in a dataset
Keeping Good Records
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition3
Computer database Electronic file that contains an organized
collection of related informationRelational database
Stores information in tables composed of columns and rows
Field Single item of information about a person,
place, or thing
Keeping Good Records (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition4
Record Group of related fields that contain all the
necessary data about a specific person, place, or thing
TableGroup of related records
Primary keyField that uniquely identifies each record in a
table
4
Keeping Good Records (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition5 5
Figure 24-1 Example of a one-table relational database
Keeping Good Records (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition6
Figure 24-2CD_Number field
Primary key in parent tableCD_Number field in child table
Used solely to link song title and track information to the appropriate CD in parent table
Child tableCD_Number field is called the foreign key
6
Keeping Good Records (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition7 7
Figure 24-2 Example of a two-table relational database
Connecting …Connecting
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition8
Figure 24-3Emp_Number field is the primary keyStatus field contains the employment statusCode field identifies the employee’s
departmentData Source Configuration Wizard
Easy way to connect an application to a database
8
Connecting …Connecting (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition9 9
Figure 24-3 Data contained in the tblEmploy table
Let the Computer Do It
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition10
BindingConnecting an object to a control
Bound controlsThe connected controls
DataGridView control Displays data in row and columnar format,
similar to a spreadsheetOne of the most popular controls for
displaying table data
10
Let the Computer Do It (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition11 11
Figure 24-7 DataGridView control’s task list
Let the Computer Do It (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition12
While an application is running, you can use the BindingNavigator control to:Move from one record to the next in the
datasetAdd or delete a record and save any changes
made to the datasetComponent tray
Stores objects that do not appear in the user interface while an application is running
TableAdapter object in the component tray Connects the database to the DataSet object
12
Let the Computer Do It (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition13
TableAdapterManager object Used to save changes made to the database
BindingSource object Provides the connection between the DataSet
and the bound controls on the FormTblEmployBindingSource in Figure 24-8:
Connects EmployeesDataSet to two bound controlsDataGridView control and BindingNavigator control
Figure 24-9Illustrates relationships
13
Let the Computer Do It (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition14 14
Figure 24-8 Result of dragging the tblEmploy object to the form
Let the Computer Do It (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition15 15
Figure 24-9 Illustration of the relationships among the database, the objects in the component tray, and the bound controls
The Copy to Output Directory Property
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition16
When the Data Source Configuration Wizard connects an application to a database:It adds the database file to the application’s
project folderMethod of saving changes to a local
database file Is determined by the file’s Copy to Output
Directory property
16
How Does Visual Basic Do It?
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition17
When a table or field object is dragged to the form: Computer adds the appropriate controls and
objects to the applicationA form’s Load event
Occurs when the application is started and the form is displayed for the first time
Load event procedureCode to fill a dataset with data belongs here
in most applications
17
Thank You for Catching My Errors
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition18
ExceptionsHandled in Visual Basic by displaying an error
message and then abruptly terminating the application
Try…Catch statementUsed to take control of the exception
handling in your code
18
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition19 19
Figure 24-15 Syntax and examples of the Try…Catch statement (continues)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition20 20
Figure 24-15 Syntax and examples of the Try…Catch statement (cont’d.)
I’ll Use My Own Controls, Thank You
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition21
To bind an object in a dataset to an existing control on the form:Drag the object from the Data Sources
window to the controlOr click the control
Then set one or more properties in the Properties window
Appropriate property to set depends on the control you are binding
21
Coding the Next Record and Previous Record Buttons
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition22
BindingSource object Uses an invisible record pointer to keep track
of the current record in the datasetPosition property
Stores the position of the record pointer Move methods
Can be used to position the record pointer in a dataset
Moves the pointer to the first, last, next, or previous record
22
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition23 23
Figure 24-19 Syntax and examples of the BindingSource object’s Position property
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition24 24
Figure 24-20 Syntax and examples of the BindingSource object’s Move methods
Summary
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition25
Relational databases can contain one or more tables
Data in a relational databaseCan be displayed in any order
Most tables contain a primary key that uniquely identifies each record
To access the data stored in a database:First connect the application to the database
Summary (cont’d.)
Clearly Visual Basic: Programming with Visual Basic 2010, 2nd Edition26
TableAdapter object Connects a database to a DataSet object
DataGridView controlDisplays data in row and column format
Try…Catch statement Used to handle exceptions that occur while
an application is runningBindingSource object
Uses an invisible record pointer to keep track of the current record in a dataset
26