Building Your First DataSet in Visual Basic .NET
A DataSet can contain all the basic elements of a database: tables, keys, indexes, and even relations between tables. So by creating a DataSet, you'll be discovering the structure of a database at the same time. The fundamental differences between a DataSet and a database are that a database generally resides on a hard drive in one or more files and is usually larger. A DataSet usually holds a subset of the data in a full database.
A DataSet can be stored on a hard drive, but it can also simply be pulled out of an existing database — and therefore may merely reside in the computer's memory while someone manipulates or views it. Then, if changes are made, the DataSet can be merged back into the database from which it was extracted.
Creating a DataSet
You can create and manipulate DataSets using VB.NET data controls and Server Explorer in many ways. However, to get off to a good start, you use the simplest approach of all: dropping a DataSet control from the Toolbox onto a form. In this example, you are not extracting a DataSet by extracting a subset of an existing database (technically, a subset is referred to as a query or result). Instead, you create a DataSet yourself, defining its structure (technically, schema). This is virtually identical to the way that you would create an actual database, but a DataSet is simply smaller than an ordinary database.
To create a DataSet, follow these steps:
1. Choose File --> New --> Project.
The New Project dialog box appears.
2. In the Name column, type AddressBook.
Did you know that it's very easy to put your Windows-style projects in any directory that you want? Just click the Browse button in the New Project dialog box and choose a directory. You can even type in the name of a directory that doesn't exist, and VB.NET creates it for you.
3. Double-click the Windows Application icon.
The dialog box closes and you see an empty form.
4. Open the Toolbox (press Ctrl+Alt+X or click its tab).
5. Click the Data tab in the Toolbox.
You see a set of database-related controls.
6. Double-click the DataSet icon in the Toolbox.
The Add Dataset dialog box appears.
7. Choose the Untyped Dataset option by clicking that radio button.
You have no DataSet in this project yet, so you can't use the Typed Dataset option.
8. Click OK.
The dialog box closes and a new DataSet object icon appears in the tray below your form. The tray is where VB.NET puts controls that are never made visible to the user, such as a Timer.
Adding a table to a DataSet
Now it's time to define the structure, or schema, of your new DataSet. It's time to add a table to it.
Inside that table, define three columns: one for the last name of each person in your address book, the second for the first name, and the third for an autoincrementing primary key.
1. Right-click the DataSet1 icon from the previous example in the tray, and choose Properties.
The Properties window appears, showing the properties of DataSet1.
2. In the Properties window, change the Name property (not the DataSetName property) of DataSet1 to dsAddresses.
The DataSet icon in the tray changes to display its new name. (Behind the scenes, VB.NET also changes the name in the source code that it writes automatically to define the contents of your form.)
3. In the Properties window, click the Tables property and then click the ellipsis (...).
The Tables Collection Editor appears.
4. In the Tables Collection Editor, click the Add button.
The table's properties appears.
5. Change the Name property (not the TableName property) to Friends.
VB.NET again changes the source code behind the scenes. You don't have to worry about these details — just relax and know that VB.NET knows how to write the code that defines your new DataSet's schema.
6. In the Properties list of the Tables Collection Editor, click Columns and then click the ellipsis.
The Columns Collection Editor dialog box appears.
7. In the Columns Collection Editor, click the Add button.
You can now define a new column and its properties. Note that the DataType property for all columns defaults to the string (text) type, although you can change it. This is the data type that you want for the LastName and FirstName columns.
8. Change the Name property (not the ColumnName property) to LastName. (The name defaults to DataColumn1.)
9. Click the Add button.
Column2 is now created.
10. Change this column's Name property to FirstName.
11. Click the Add button.
Column3 is now created.
12. Change this column's Name property to Key, and its ReadOnly property to True.
With the ReadOnly property True, nobody can write (change) any of the data in this column. That's what you want; it's supposed to be looked at (read) only.
13. Double-click the Unique property.
The property changes from False to True. Now the DataSet refuses to permit two rows to contain identical data in the Key column. In addition, as long as this property is True, you can't use the Remove button in the Columns Collection Editor dialog box to delete the column.
14. Double-click the AutoIncrement property.
The property changes from False to True. Now the DataSet automatically increments (increases) the number in this column by one for each row. Notice that when you double-clicked this property, VB.NET was wise enough to change this column's DataType property from String to Integer. After all, you want ordinary numbers (1, 2, 3, 4, and so on) in this column, not text. Text can't be incremented.
Also notice that you can change the AutoIncrementSeed (starting number) property and the AutoIncrementSeedStep (amount of increase in each step) property. However, the defaults are what you want: start from zero and go up by one each time. This way, the first record you add to this table is automatically given a 0 in the Key column, the next record you add is given a 1, and so on up.
15. Click the Close button twice.
The Columns Collection Editor and the Tables Collection Editor close.
There is no actual data in your dataset, but you have created its schema.
Viewing a DataSet's code
While you've been sitting on your fancy perch using dialog boxes and controls to create a DataSet, VB.NET has been busy in the code window doing all the grunt work to write the programming. Just for a good scare, open the code window by double-clicking Form1 in the design window. Now click the + symbol next to Windows Form Designer generated code to reveal the tons of code that VB.NET doesn't think you need to bother your pretty head about.