Lecture and Tute 7: Databases Pt. 1

advertisement
Monash University
Caulfield School of Information Technology
IMS1906: Introduction to Business Computing
Lecture 7: Databasing
Introductory
You all should have installed a database management system (BDMS) when you first
installed Visual Basic Express Edition. If you did not do this, you will have to uninstall
and reinstall VB.
That database is called SQL Server Express Edition. Microsoft sells three different
DBMSs: Access (which is part of Office Professional), FoxPro (another desktop system)
and SQL Server. SQL Server runs on both desktops and servers, and is Microsoft’s major
DBMS. Its competitors are Oracle (from Oracle), DB2 and Informix (both from IBM)
and Apache (Linux open source, from Apache and free).
You can use any DBMS with Visual Basic, but SQL Server is the “native” DBMS for all
the .NET products. Microsoft has been trying (and failing) to kill off Access almost since
it was released.
One of the many major reasons why we are using Visual Basic Express Edition in this
subject is that its integration with SQL Server is really good.
Getting Started
Open VB. The Start Page should show. Click on the How do I…? Then click on Data
Access (How Do I in Visual Basic Express). Click on Storing and Accessing Data
and scan the overview of database operations.
There are two pieces of jargon here that are critical: dataset and bound.
A dataset is just like the array of a structure we covered last week. Well, almost. The
differences are that, at least in theory, the dataset can have more that one array. And the
arrays are called tables.
Binding means that you can tie a control like a textbox to a field in a table in a dataset.
Well, drat, there are two sets of words that are used to describe things in a database. A
record (also called a row) is a complete entry. For example, each entry in last week’s
array of a structure is called a row. Each item in a record is called a field (also called a
column). LastName, FirstName, Address, PhoneNo and MobileNo can be the
fields (or columns) in the MyFriends array. The information about one friend is all in
the same record (or row).
IMS1906
Lecture & Tute 6: Databasing
Page 1 of 4
You need to know about rows, columns, tables and datasets because these are the words
that are always used by VB 2006 (and all other Microsoft programming languages).
Remember:
1. A dataset can be put in your VB program. It is built automagically
(computerese indicating that you don’t have to worry about most of the
details).
2. A dataset contains 1 or more tables.
3. Each table has rows and columns (like a spreadsheet), which are more usually
called records and fields.
4. There is a database that’s being used by your program. It can be used by many
other programs. It will almost always have more than one table, but we won’t
worry about that for now.
5. You can link a field from the current record to a textbox a form. You can also
display data from a table in a combobox, or show everything in the table in a
special control called a DataGridView (which looks a lot like a chunk of
spreadsheet).
Define Your Database
You can link your program to an existing database, or create a new database using a set
of tools built into VB.
At the bottom of the database page you’ve been reading, there’s a link to Creating Your
First Database. Click on that link.
We are going to follow these instructions with one major exception. In the section Adding
a Key, we’re going to do it differently. Complete the creation of your database and the
table in it following the instructions in the tutorial.
We’re going to let SQL Server create the Primary Key for us. We’ll add another field
(row) called FriendNo. Its Data Type should be int. Right click on the FriendID and
select Set Primary Key. Now, click on the little + box to the left of Identity
Specification in the Columns Properties here. Set Is Identity to Yes. Leave the
Identity Increment and Identity Seed at 1.
What was all that about? There has to be something unique in each record (row) in a
table. The way the Microsoft tutorial uses is fine as long as you don’t have two friends
with the same names. You’ve set it up so SQL Server generates a unique number
automatically for each new record. This is a much better (and indeed standard) way to do
IMS1906
Lecture & Tute 6: Databasing
Page 2 of 4
it, but we’ll have to be careful later on. We do now have a different Primary Key than the
tutorials expect, and a field (column) that the tutorials don’t know about..
Finish off the exercise, but enter your own friends, not Microsoft’s suggestions. And it’s
a really good idea not to use any “funny” names!
Adding an Existing Database to a New Project
This is the next tutorial in the How Do I? series. It has a serious omission. There needs to
be a replacement for step 4, as follows:
4. On the Data menu, select Show Data Sources. A new window opens in the
Solution Explorer.
Showing Information to the User
This is the next tutorial. Note that you should change the Dock property for the
DataGridView back to None after changing this property in step 5.
There is some housekeeping that you need to do.
1. Change the name of the new control to dgvAddresses
2. Check out the Columns collection. You can change the appearance of headings.
Make the following changes:
a. The FriendID column’s Visible property should be False. The number is
automatically calculated and inserted by VB. There’s no need for the users
to even know it’s there.
b. Change the HeaderText properties to First Name, Last Name, Address
and Phone Number
I will continue working through the Microsoft tutorials, posting extensions to this lecture.
I’ll point out pitfalls and quagmires so that you’ll be able to finish the material over the
next two weeks.
On Tuesday the 18th I’ll be posting Assignment 5, worth 6 points. It will be due at the
next lecture, on April 25. Please don’t miss this.
I also want all outstanding assignments finished and handed in to me on the 25th.
IMS1906
Lecture & Tute 6: Databasing
Page 3 of 4
IMS1906
Lecture & Tute 6: Databasing
Page 4 of 4
Download