Professional Documents
Culture Documents
Programming
1
Chapter
Database Management
9
2
Introduction
3
Database
Management Systems
5
Layered Approach to Using a
DBMS
• Applications that work with a DBMS
use a layered approach
• VB application is topmost layer
• VB sends instructions to next layer, the
DBMS
• DBMS works directly with data
• Programmer need not understand
the physical structure of the data
• Just need to know how to interact with
the database
6
Visual Basic Supports Many
DBMS’s
• Visual Basic can interact with many DBMS’s
• Microsoft SQL Server
• Oracle
• DB2
• MySQL
7
Database Concepts
9
Database Table
• Each table has a primary key
• Uniquely identifies that row of the table
• Emp_Id is the primary key in this example
• Columns are also called fields or attributes
• Each column has a particular data type
Column Field
10
VB and SQL Server Data Types
• VB data types must match table data types
• SQL Server and VB have similar data types
16
Connecting VB to a Database
• The flow of data from database to application
17
Data-Bound Controls
◼ Labels
◼ List boxes
19
Sample Table – Cities Table
20
Sample Table – Countries Table
21
Megacities.accdb
▪ Contains the two tables Cities and
Countries shown earlier.
▪ This database will be used extensively in
the examples for this chapter.
▪ Several steps are required to bind to a
table of the database.
22
Binding to the Cities Table
23
DataSource Property of
BindingSource1
click here
24
Choose Data Source Type
select
25
Choose Database Model
select
26
Choose Data Connection
27
Add Connection Dialog Box
28
Change Data Source Box
select
click on OK button
29
Add Connection Dialog Box
30
Select Database File
double-click on Megacities.accdb
31
Add Connection Dialog Box
click on OK button
32
Choose Data Connection
33
click on Yes button
34
Save to File
35
Choose Database Objects
check on
Tables box
click on Finish
button
36
Changes in Properties Window
and Form
37
After Clicking on Data Member
Down-Arrow
click on Cities
38
VB Generated Items
new
new icon code
39
Binding Complete
40
Connect an Additional Table
Add another BindingSource control to the form.
41
Set Data Source Property
click on MegacitiesDataSet
42
Set Data Member Property
click on Countries
43
VB Generated Items
new icon
additional code shows in Load event procedure
Me.CountriesTableAdapter.Fill(Me.MegacitiesDataSet.Countries)
44
Examples
45
Example 1: Form
txtTotalPop
46
Example 1: Code
Private Sub btnDisplay_Click(...) Handles _
btnDisplay.Click
Dim query1 = From city In
MegacitiesDataSet.Cities
Where city.country = "India"
Order By city.pop2010 Descending
Select city.name
lstOutput.DataSource = query1.ToList
lstOutput.SelectedItem = Nothing
47
Example 1: Code (continued)
48
Example 1: Output
49
Example 2: Form
txtName
dgvOutput
50
Example 2: Code
52
Example 3: Form
dgvOutput
53
Example 3: Code
54
Example 3: Code (continued)
dgvOutput.DataSource = query.ToList
dgvOutput.CurrentCell = Nothing
dgvOutput.Columns("name").HeaderText = "City"
dgvOutput.Columns("formattedIncr").HeaderText
= "Population Increase"
55
Example 3: Output
56