Download Databases, Part 1 - personal.kent.edu

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

Oracle Database wikipedia , lookup

IMDb wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Ingres (database) wikipedia , lookup

Concurrency control wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

ContactPoint wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
Database
Overview of Relational Databases
Most modern computerized database management systems are implemented as relational
databases. Relational databases use the concept of tables to store all data of a specific type of
entity (student, patient, classroom, employee, etc). Each table has a set of attributes (sometimes
called fields or columns) which are the things that are going to be collected about each entity
(first name, last name, address, birthDate, salary, startDate, etc). The table consists of zero or
more records which are the values for a particular instance of the entity (“Charlene”, “Fox”,
“317 Pine Ave”, etc).
One (or a set) of the attributes is designated as each table’s primary key, which uniquely
identifies each record (for instance, a student id uniquely identifies each student)
Two or more tables can be linked by making the primary key of one table an attribute of
another table. For instance a Customer table might have Salesperson ID as one of its attributes.
Salesperson ID would link to a specific record in a Salesperson table, which would have many
more details about each salesperson.
Questions, or queries, can be created to get data from the database in an organized manner.
For instance: “What are the names and addresses of all students who have a GPA greater than
3.5?” Queries specify the attributes to be returned (names and addresses in the example), and the
records to be returned (only students with GPAs greater than 3.5). Queries can be simple, or
very complex, and can refer to attributes from a single table, or from multiple linked tables.
There are many companies that sell computerized database management systems, for
example Oracle for large companies. For personal use, and for small to moderate sized
companies, Microsoft Access is often used, because of its availability with the Office Suite.
Microsoft also sells SQL Server, which is a dbms that runs on a server. MySQL is an open
source dbms.
Data Source Configuration Wizard
In many of the scenarios that follow, you will reach a point where you work with the Data
Source Configuration Wizard. Here are the steps for using the Wizard:
1. Choose a Data Source Type: the icons are Database, Service, and Object. For our
purposes, click the Database icon. Click Next.
2. Choose Your Data Source Connection:
a. If you have added data sources to projects, they will be listed in the drop down box. You
can choose one of those, if appropriate.
b. If you have not added data sources before, or if the listed sources aren’t what you want,
click New Connection. The New Connection dialog box appears
i.
The Data Source will list the available data sources, such as Microsoft Access
Database File, Microsoft SQL Server, Oracle Database, etc. For our purposes,
we want Access, so select Microsoft Access Database File, then click Continue
(alternatively, you may get a dialog box with a default data source displayed. If it is
not what you want, click the Change button beside it, and select the correct source)
D:\582764510.doc
Page 1
3.
4.
5.
6.
c. An Add Connection dialog box will appear. Click the Browse… button beside
Database File Name, and browse through your system to open the desired database file.
d. Back in Add Connection again, if the database has been protected with a username and
password, enter them; otherwise leave those textboxes alone.
e. Click Test Connection. If you get the message Test Connection Succeeded, click OK,
then OK again to finish with the Add Connection dialog. If the test connection did not
succeed, redo previous steps until the connection succeeds.
f. Click the Next button in the Data Source Connection Wizard
A popup box informs you that the connection uses a local file that is not part of the current
project, and asks if you would like to copy the database file into your project. For our
purposes, choose Yes. For a real application, your answer depends on the work environment
for your project. Here’s the choices:
a. Yes: the database file is copied into your project and made a resource, like the graphics
files. If you copy your project folder, the database file goes along with it. However, any
changes you make to the database are made to the copy and not to the original. This is a
problem if your program is intended to update a database used by other users and
applications.
b. No: the database is accessed at its place on disk. Any updates are made to this original.
This is what you want, if the database is one used by other users and/or applications.
However, if you move or copy your VB files somewhere else, they may not have the
correct connection link to the database.
Save the Connection String…: make sure the Yes checkbox is checked. The default
connection string name is fine. Click Next.
Choose Your Database Objects: expand the items using the plus signs beside them, and
check the items you want to include in your project. Click Finish.
After a period of time, which depends on the size of the database, the objects you selected
will appear in the Data Sources window, which is in the same location as the Solution
Explorer, but a separate tab. If the Data Source tab isn’t there, select the menu item Data,
Show Data Sources.
Different ways to use data from a database
The following instructions are each different ways to include database data in an application. If
you are trying these out, start a new application for each set of instructions. The instructions
progress from the easiest (and most ugly interface!) to the more difficult (and best looking
interface).
Note: in the following instructions, the database used is Northwind.accdb, a Microsoft Access
database that is available on the web site. If you are using a different database, adapt the
instructions accordingly.
DataGridView
1. Start a new application, and enlarge the form to fill the work area.
2. Add a DataGridView control to the form, and enlarge the DataGridView to nearly fill the
form.
D:\582764510.doc
Page 2
3. Click the Smart Tag of the DataGridView (the small arrow in the upper right corner of the
DataGridView) to open the DataGridView Tasks dialog box. This may already be open
when you add the control to the form.
4. In the drop down Choose Data Source of DataGridView Tasks, click on Add Project
Data Source
5. Follow the directions above in the section Data Source Configuration Wizard to add the
Northwind database. In step 5, select the Customers table.
6. After finishing the Wizard, you should see three items that were added to the component
tray: NorthwindDataSet, CustomersBindingSource, CustomersTableAdapter. They are
objects used as follows:
a. A DataSet is a copy of the data made in RAM. It contains all table and queries you
selected. Any changes made to data are only made to the in-memory DataSet until they
are committed to the database on the disk.
b. A TableAdapter is an object with methods and properties used to pull the data from the
database into the DataSet
c. A BindingSource is an object with methods and properties used to connect controls to
the DataSet
7. The DataGridView now has column header names that match the names of the attributes in
the Customers table. The columns can be edited by clicking the Smart Tag and selecting
Edit Columns… In the resulting dialog box, you can click on a column and set its
properties, such as width, header text, etc. You can also delete and rearrange the columns.
8. Run the application. All the data from the Customers table appears in the DataGridView.
Alternative way to set up a DataGridView
1. Start a new application, and enlarge the form to fill the work area.
2. Click on the Data Sources tab below the Solution Explorer window. (If the Data Source tab
isn’t there, select the menu item Data, Show Data Sources.)
3. Click Add New Data Source…
4. The Data Source Configuration Wizard will appear. Follow the directions above in the
section Data Source Configuration Wizard to add the Northwind database. In step 5,
select the Customers table.
5. When you finish the wizard, the Data Source window will contain the NorthwindDataSet.
Expand it by clicking the plus sign beside it (if it’s not already expanded).
6. Single-click on the Customers table, then drop down the arrow that appears to its right
7. Click DataGridView in the drop down list
8. Drag the Customers table to the form. A DataGridView control appears, already bound to
the Customers table. You can re-size it, and make changes to its columns as before.
9. As in the above technique, a NorthwindDataSet, a CustomersBindingSource, and a
CustomersTableAdapter are added to the component tray.
10. This technique differs from the one above in that a navigation bar is also added to the form.
It has a visual element on the form and also two components in the component tray: the
TableAdapterManager and the CustomersBindingNavagator
11. Run the application. With the Navigator strip, the user can move through large tables easily.
Also, if a user makes changes, then clicks on the Save button in the Navigator strip, the
changes are saved to disk. You can look at the code used by the Save button to see how to
save changes.
D:\582764510.doc
Page 3
Details View
Instead of displaying the records all at once in a DataGridView, you can show the values for one
record at a time.
1. Start a new application, and enlarge the form to fill the work area.
2. Click on the Data Sources tab below the Solution Explorer window. (If the Data Source tab
isn’t there, select the menu item Data, Show Data Sources.)
3. Click Add New Data Source…
4. The Data Source Configuration Wizard will appear. Follow the directions above in the
section Data Source Configuration Wizard to add the Northwind database. In step 5,
select the Customers table.
5. When you finish the wizard, the Data Source window will contain the NorthwindDataSet.
Expand it by clicking the plus sign beside it (if it’s not already expanded).
6. Single-click on the Customers table, then drop down the arrow that appears to its right
7. Click Details in the drop down list
8. Drag the Customers table to the form.
9. A set of label/textbox pairs appears. Each label contains the name of one of the table’s
attributes, and each corresponding textbox is bound to that attribute. The properties of the
labels and textboxes can be changed as desired, such as moving or resizing them, or changing
the font.
10. The five items appear in the component tray, as in the second method of creating a
DataGridView, and the Navigator Bar appears on the form.
11. Run the application. As with the DataGridView, the user can use the Navigator Bar to move
through the records, and add, update, or delete them; and can save any changed.
Alternative to Details View
Using the above technique to show the Details View places each attribute value in a text box.
You may wish to have the value placed in a different kind of control, such as a Label, a Radio
Button, a Masked Text Box, etc. To do so:
1. Follow steps 1-7 in the Details View instructions above.
2. Single-click on one of the attributes in the Data Source window. A drop-down arrow will
appear.
3. Click the drop-down arrow, and select the type of control you want to use for that attribute.
If the type of control you want does not appear, click on Customize… at the bottom of the
list. In the new dialog box, select the control you want, and click OK. The control will be
added to the drop down lists of all the attributes. You still need to select it from that list.
4. Repeat steps 2 and 3 for each attribute for which you want to choose a specific control.
5. Drag the customer table to the form.
6. The controls for the various attributes will be as you selected.
D:\582764510.doc
Page 4