CT243 lab10
http://geminga.it.nuigalway.ie/~gettrick/courses/CT243/labs/l10.html
Follow the following instructions to connect a new VB .NET project to an
existing MS ACCESS database (in this lab the database has information on
second-hand cars for sale):
1.
On the File menu [top left corner], choose "New Project".
2.
Select the "Windows Application" Template (at the bottom of this window,
enter what you want to call this project, e.g. "dBase1"). Click the OK button.
3.
In the Data menu [top center], choose "Add New Data Source..."
4.
Select Database and click Next.
5.
Click the New Connection button.
6.
Under "Data source:" click the "Change..." button.
7.
Select "Microsoft Access Database File" and click the OK button.
8.
Under "Database file name:" click the "Browse..." button.
9.
Find where you have stored the downloaded [available
here] database file (it should have suffix .mdb: lets suppose
you have named it dba.mdb),
select it and click "Open".
10.
(If you want, you can now click "Test Connection" button at the bottom left: you should
get a "Test Connection Succeeded message".)
11.
Click on OK.
12.
In the middle of the form, to the left of "Connection String" you should click on the
+ sign: It will show you the string that will be used by VB .NET.
13.
Click on "Next>"
14.
Select No to the question that pops up.
15.
Make sure the check-box is ticked to save the connection string, and click the "Next>" button.
16.
On the Choose your database objects page, expand the Tables node, and then select the
check box for the secondHAND table.
17.
Click Finish to finish.
18.
The local database file dba.mdb has been added to your project. Notice that a dbaDataSet object has
been added to the Data Sources window.
19.
On the File menu, choose Save All to save your project.
All of this serves the purpose of identifying which database to connect to
and making the connection: The next step is to add (visual) controls to
actually see the data and edit/delete/add to it:
a.
In Top Right area of the IDE (Interactive Design Environment), select the "Data Sources" tab.
b.
You should experiment with the dbaDataSet, e.g. click on the + sign to show
all the fields in the table.
c.
Click on the Icon to the left of the "secondHAND" table and drag it over to
your form. It will create a "secondHANDDataGridView" object on your form.
d.
(Alternatively: In drop down menu beside table "secondHAND" you can select "details".
This will change the ICON. Dragging this ICON to the form gives a set of labels & text
boxes for each field.)
e.
(Optional: for the properties of the secondHANDDataGridView object (bottom right
of IDE), find the "Dock" property. Select the center panel to set it to "Fill".)
f.
Run your program in the normal way! You can see all the fields, edit them,
delete records, add records, etc.
The object that VB .NET automagically adds towards the top of your form
(when you drag across the icon) is called the BindingNavigator
(in this case secondHANDBindingNavigator). It includes controls
to navigate through records (forward, back), to add a new record (the
+ icon), to delete a record, and to save the
changes (the disc icon).
To actually view the data, you may use (i) the
secondHANDDataGridView as described above, or (ii) a set of text boxes
and labels: The main difference is (i)
shows you everything at one in a table
format, while (ii) shows you only one record at a time. Indeed if you want to
use (ii), you can select (from the "Data Sources" tab) one particular
field from the secondHAND table, and just drag that field across
to get only a label/text box for that particular entry. (Note: mostly
text boxes are used: but in this example, one of the fields is boolean
(whether or not the car has a sun roof), so a check box is the
appropriate control on the form)
©
NUI, Galway