Monday, May 18, 2009

Create a database in Visual Basic 2008 Express

One of the biggest things I had to learn was setting up the database. There are LOTS of resources on the web with regard to Visual Basic and how to create databases. But I had great difficulty figuring it out for myself. I now present to you - the result of my searching, trial and effort, and results.

After creating a project, Go to the Solution Explorer. Right-Click on the project name (Bold title above everything else) choose 'Add' then 'New Item'. Choose 'Service-based database' and give it a name (db works, or mydb or something like that). Then click on 'Add'. It will work on it - be patient. It will then bring up the 'Data Source Configuration Wizard' asking you to 'Choose your Database Objects'. Skip this step for now by clicking on 'Cancel'. I learned that I need to setup my database before I create my datasets. This will save steps later. In your solution explorer, you will now see your new database as 'name.mdf'. This is where the information will be stored.

Now to define a Visual Basic database. Click on 'View' 'Database Explorer'. You should see your db file under 'Data Connections'. Click the plus sign next to the database name, then click the plus sign next to Tables. You should note the icon change from a red 'x' to a black plug-in on your database. This means you are now connected to the database. Right-Click on 'Tables' and choose 'Add New Table'. You now define the name of each column and what type of value that column will be and if you will allow that column to be empty on any given row.

Name the column 'CatID' (for Category ID). The Data Type should be 'int' and the Allow Nulls value should be cleared. The second row (which is defining a second column) can be named 'CatName', the type is 'nvarchar(30)' and the Allow nulls should be cleared. By clearing the checkbox under 'Allow Nulls', you are telling the table that some value MUST be in that column for EVERY row. Make one more column named 'CatDesc', with a type of nvarchar(250) and select the 'Allow Nulls' checkbox. This allows the category to have no description, which is ok. Lastly, setup an autoincrement and Primary Key. To do this, first, right click on the StoreID grey block to the left. Chose 'Set Primary Key'. You'll use that later. To setup auto increment, go to the 'Column Properties' and click the plus sign next to 'Identity Specification'. Double click the '(Is Identity) value of 'no' to change it to 'yes'. This will define each row of this table (as you add them) as a new identity with a unique ID number. Essential for later use.

That's it - the table is defined. Now to save it, go to the properties and change the '(Name)' field to 'CatTable'. Close the table definition by clicking the 'x' in the top right corner. Visual Basic will ask you if you want to save changes to 'Table1'. Even though that's not the name you entered, it's talking about the same table. Click 'Yes'. Notice that the new table is listed under your database name.

Now add some data to the new table by right-clicking on the new table name and choose 'Show Table Data'. You will be presented with the column Headings you created. You can't change the ID field because you set it to auto increment. But enter data into each of the other fields, Name and Desc. Note that the Name is required, the Description is not. Tab past the last field and you will enter the next line. Enter a few rows of data. Close the Table data to save.

There you go. The database is created, the columns are defined, and you entered some new information. That wasn't so hard. Now to use that information, to view it, edit it and update/save it - that's a whole 'nother story.

No comments: