CSC301_CHAPTER 9
CSC301_CHAPTER 9
Chapter 9
Accessing
Database
• Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Objectives (1 of 2)
• Understand database files
• Connect to a database using ADO.NET
• Connect Form objects to the data source
• Bind database fields to the Windows Form object
• Access database information on a Windows Form
object
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Objectives (2 of 2)
• Add a record
• Delete a record
• Select records from a list
• Program beyond the Database Wizard
• Create the OleDbDataAdapter object
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Chapter Project
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Database Files (1 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Database Files (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (1 of 6)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (2 of 6)
• Create a Windows application named Gaming Art. Name the form
frmArtPrize. Change the Text property to Gaming Art Commission –
Game Art & Design Competition. Resize the form to a size of 670, 450.
Change the BackColor property to White on the Web tab. An image
representing one of the local artists, named Art.jpg, is available with
your Data Files. Place a PictureBox object on the left side of the
window. Name the PictureBox object picArt. Change the Size property
to 293, 160. Using the Image property, import the Art.jpg image for the
PictureBox object. Change the SizeMode to StretchImage. On the right
side of the form, place a Label object named lblTitle. Change the Text
property to Game Design Finalists on two lines. Make the Font property
Script MT, Bold, size 40, and the ForeColor property Green on the Web
tab. Center-align the text. Close the Toolbox, and then tap or click
Project on the menu bar
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (3 of 6)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (4 of 6)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (5 of 6)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Establish a Database Connection (6 of 6)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
View Data Available in the Source
Database (1 of 2)
• DataSet string is called data binding
• Data binding allows you to display each field as an object
on the form
• In the Art project window, point to the Data Sources tab on
the left side. If the Data Sources tab is not visible, tap or
click View on the menu bar, tap or click Other Windows, and
then tap or click Data Sources
• Tap or click Data Sources to view the database sources
• Tap or click the expand icon for the Artist table to expand
the list of field names within the table. Each bindable field
item in the Data Sources window can be placed on the
Windows Form object
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
View Data Available in the Source
Database (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Bind Database Fields to the Windows
Form (1 of 4)
• Visual Studio automatically creates a databinding to populate
the form by binding the form object to the DataSet information
• After the first field item is placed on the Windows form, a
navigation toolbar control called the BindingNavigator appears
on the form
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Bind Database Fields to the Windows
Form (2 of 4)
• Select the Art ID field in the Data Sources window.
Drag the Art ID field to the Windows Form object below
the PictureBox object
• Drag the rest of the field objects from the Data Sources
window to the Windows form. Select all the field labels
and field TextBox objects and change their font size to
10 points and bold. Use the formatting tools on the
Format menu to equally distribute the bound objects.
You can select the Labels and the TextBox objects
separately to move them independently of each other.
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Bind Database Fields to the Windows
Form (3 of 4)
• Run the application by tapping or clicking the
StartDebugging button on the Standard toolbar to fill
the Windows Form object with the data from the Artist
table. Use the Move next button on the navigation
toolbar to move through the records. Tap or click the
Move last button to display the last record
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Bind Database Fields to the Windows
Form (4 of 4)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Add Records (1 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Add Records (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Delete Records (1 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Delete Records (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Select Records from a List (1 of 3)
• Select the Artist Name Label and TextBox objects on the Windows form.
Press the DELETE key to delete the Artist Name objects from the
Windows form. Select the Artist Name table field in the Data Sources
window and then tap or click its list arrow
• Tap or click the ComboBox object from the Toolbox object listing for the
Artist Name field. Drag the Artist Name field ComboBox object to the
original location of the Artist Name TextBox object on the Windows Form
object. Change the font size to 10 and bold. Align the ComboBox with
the other objects on the Windows form
• To fill the ComboBox object with the names of the artists, the
ComboBox object must be bound to the Artist Name field. To bind the
items to the ComboBox object, select the Artist Name object on the
Windows form and tap or click the Action tag on the Artist Name
ComboBox object
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Select Records from a List (2 of 3)
• Tap or click the Use Data Bound Items check box on the
ComboBox Tasks menu. The Data Binding Mode list is displayed.
Tap or click the Data Source list arrow under Data Binding Mode,
and then select ArtistBindingSource to connect the table to the
ComboBox object. Next, tap or click the Display Member list
arrow and then select Artist Name. Tap or click the Value Member
list arrow and then tap or click Artist Name in the list. Do not
change the Selected Value entry
• Tap or click the Start Debugging button on the Standard toolbar
to run the application. After the Windows form opens, tap or click
the list arrow on the Artist Name ComboBox object
• Tap or click Song Li to move directly to the record containing the
information for Song Li’s artwork
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Select Records from a List (3 of 3)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Beyond the Database Wizard
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Fill a DataTable Object (1 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Fill a DataTable Object (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Beyond the Database
Wizard (1 of 4)
• Download the original Access database file Art.accdb again to
overwrite any data you added or deleted from the database.
Open the Gaming Art Windows application. Add a Button object
named btnValue to the Windows Form object and change the
Text property to Total Retail Value. Change the font size to 12
and the ForeColor property to Green on the Web tab. Set the
Size property for the button to 178, 28 and center the Button
object horizontally across the form. Below the Button object, add
a Label object named lblTotalValue with the Text property of 23
“Xs,” and then center the text. Change the font size to 12 points.
Set the Visible property to False for the lblTotalValue Label object
because the Xs should not be displayed when the program
begins
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Beyond the Database
Wizard (2 of 4)
• Double-tap or double-click the Total Retail Value button to create
the btnValue_Click event handler. The first variable, strSql, is
assigned the SQL statement that queries all the fields in the Artist
table. The second variable, strPath, is assigned the database
driver for Access and the path to the Art.accdb file. The third
variable, odaArtist, is an instance of the OleDbDataAdapter
• After the first three variables are initialized, initialize the rest of
the variables needed for the Button object event handler. An
instance named datValue is initialized to represent the DataTable
object. The intCount variable is used to count through a For loop.
The last variable, decTotalValue, will contain the total value of the
Game art & Design collection
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Beyond the Database
Wizard (3 of 4)
• Continuing inside the btnValue_Click event handler, enter
the code to fill the DataTable with the contents of the Artist
table. In the next line of code, use the Dispose method to
close the connection
• Enter the code to create a For loop to increment through
each record in the Artist table. Because the rows are
numbered 0 to 15, the upper range is one less than the
number of rows in the table, making 16 records. The value
in each Retail Price field is added to the value in the
decTotalValue variable
• Enter the code to display the total value of the Game Art &
design collection
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Beyond the Database
Wizard (4 of 4)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Design (1 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Program Design (2 of 2)
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Event Planning Document
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.
Summary
• Create a Windows application that uses database files
and ADO.NET
Copyright © 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part.