Download Chapter 10

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Data Protection Act, 2012 wikipedia , lookup

Data center wikipedia , lookup

Operational transformation wikipedia , lookup

Data model wikipedia , lookup

Data analysis wikipedia , lookup

Versant Object Database wikipedia , lookup

Information privacy law wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SAP IQ wikipedia , lookup

Concurrency control wikipedia , lookup

3D optical data storage wikipedia , lookup

Business intelligence wikipedia , lookup

Database wikipedia , lookup

Data vault modeling wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
Chapter 9
Working with Databases
Introduction
• In this chapter you will learn:
– Basic database concepts
– How to write Visual Basic applications that
interact with databases
– How to use a DataGridView control and display
the data in a database
– How to sort and update database data
– To create an application that displays database
data in list boxes, text boxes, labels, and combo
boxes
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 2
Section 10.1
DATABASE MANAGEMENT SYSTEMS
Visual Basic applications use database management systems to
make large amounts of data available to programs.
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
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 4
Visual Basic Supports Many DBMS’s
• Visual Basic can interact with many DBMS’s
– Microsoft SQL Server
– Oracle
– DB2
– MySQL
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 5
Section 10.3
DATAGRIDVIEW CONTROL
The DataGridView control allows you to display a database table
in a grid. The grid can be used at runtime to sort and edit the contents
of a table.
Connecting to a Database
• Visual Basic uses a technique called Data binding to link tables to controls
on forms
• We will use these data-related components:
– A Data source is usually a database
• Can include text files, Excel spreadsheets, XML data, and Web services
– A Binding source connects data bound controls to a dataset
– A Table adapter pulls data from the database and passes it to your
program
• Uses Structured Query Language (SQL) is used to select data, add new
rows, delete rows, and modify existing rows
– A Dataset is an in-memory copy of data pulled from database tables
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 7
Connecting to a Database
• The flow of data from database to application
– Data travels from data source to application
– Application can view/change dataset contents
– Changes to dataset can be written back to the
data source
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 8
Section 10.4
DATA-BOUND CONTROLS
Some controls can be bound to a dataset. A data-bound control
can be used to display and edit the contents of a particular row
and column.
Advantages of Data-Bound Controls
• Can bind fields in a data source to controls:
– Text boxes
– Labels
– List boxes
• Contents of data-bound controls change
automatically when moving from row to row
• Data-bound controls also allow the contents
of a database field to be changed
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 10
Adding a New Data Source
• Open the Data Sources window and click the Add New Data
Source link
• Follow the steps in the Data Source Configuration Wizard to
create a connection to the database
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 11
Deleting a Data Source
• A data source named Employees for example
would be defined by a file named
Employees.xsd
• To delete this data source:
– Select Employees.xsd file in Solution Explorer
– Press Delete on the keyboard
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 12
Binding the Data Source to a DataGridView
Control
• Drag and drop an existing dataset from the Data
Sources window to an open area on the form
– For example:
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 13
Binding the Data Source to a DataGridView
Control
• At the same time Visual Studio builds a DataGridView on the
form, it adds a number of important objects to the form’s
component tray:
–
–
–
–
–
The BindingNavigator creates a ToolStrip at the top of the form
The DataSet is an in-memory copy of the table
The BindingSource connects the DataGridView to the DataSet
The TableAdapter pulls data from the database into the DataSet
The AdapterManager is a tool for saving data in related tables
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 14
Binding Individual Fields to Controls
• Use the dataset in the Data Sources window
– Select Details from the table drop-down list
– Drag table to an open area of a form
– Creates a separate control for each field
– Can also drag columns individually
•
•
•
•
Text and numeric fields added as text boxes
Yes/No fields added as checkboxes
DateTime fields use DateTimePicker controls
May wish to change some control properties
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 15
Adding Rows to a Database Table
• A TableAdapter provides an easy
way to add a row to a database
table
• To find the TableAdapter you
must open a data set’s Schema
Definition
• A schema definition file (.xsd)
was automatically created in
Tutorial 10-5 for the Members
table Dataset
– Displays the names and data
types of fields in the table
Copyright © 2011 Pearson Addison-Wesley
• To edit the schema definition file:
– Double-click its name in the
Solution Explorer window
– An editor window will open
• A TableAdapter object was
automatically created for the
Members DataTable
• Each DataTable has a
TableAdapter associated with it
Chapter 10 – Slide 16
Adding Rows to a Database Table
• A TableAdapter object has an Insert method
– Used to add a new row to the database table
– Each column is an argument of the method
– Just provide the values for each argument
– For example:
MembersTableAdapter.Insert(10, "Hasegawa", "Adrian",
"305-999-8888",#5/15/2010#)
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 17
Identity Columns
• Some database tables have an identity column
– Assigned a unique number by the database
– Occurs automatically for identity columns
– No need to manually supply a value for this column
• Payments table uses an identity column
– Omit ID column value
– Only supply Member_Id, Payment_Date, and Amount
PaymentsTableAdapter.Insert(5, #5/15/2010#, 50D)
Copyright © 2011 Pearson Addison-Wesley
Chapter 10 – Slide 18