1. Computing

Learn ASP.NET 2.0 - SQL Server and ASP.NET

Creating the database

From , former About.com Guide

Create a new website (the same way we did it in part 1 of this tutorial series) and add a Button and a Textbox component to the Design view of the website. (Just double-click them and they will be added by VWD.) We'll use them after we create the database.

To create the database, select File > New File..., and then select SQL Server Database. Name the database myTestDB. This will trigger a warning message asking if you want to add the database in the App_Data folder. This is a special folder folder used by ASP.NET. Users can't request data directly from App_Data. That makes it a secure location on your website that you can use for sensitive information that you want to be sure is only displayed on the web page under program control. Click Yes to continue.

To add a table to the new database, open the Database Explorer panel. (Look for tab at the bottom of Solution Explorer.) Click the plus sign in the display to show the elements in the database and right-click Tables to display another menu. Select Add New Table. The center section of VWD will change to several panels that we'll use to add the table and the columns in it. (Unlike the database, you don't enter a name for the table until you save it.)

The top panel is used to enter the columns in the database. Each column has a Name, Data Type, and an checkbox to Allow Nulls. This can be a point of confusion for programmers because the data types here are SQL Server datatypes, not VB.NET datatypes and they're not the same. For example, the uniqueidentifier data type we see next isn't a type at all in VB.NET.

Name the table myTestTable. Next, we'll add two columns named myKeyColumn and myDataColumn.

The first column will be our key field. Enter ...

Name - myKeyColumn
Data Type - uniqueidentifier
Allow Nulls - unchecked

Use the tab key to move from one field to the next.

Right-click myKeyColumn and select Set Primary Key. Then in the Column Properties in the next panel, type in the command newId() as the value for Default Value or Binding. I'll explain this after these instructions.

The second column will be our data field. Enter ...

Name - myDataColumn
Data Type - varchar(50)
Allow Nulls - unchecked

To save the table, right click the tab at the top, select the "save" option, use myTestTable as the name.

VWD also has a handy way to enter some test data into your database. Ordinarily, databases are "populated" with special load programs that are written just for that purpose. But in our case, we're just going to key in some sample data.

Right-click myTestTable in Database Explorer and select Show Table Data. This will open a data entry form for you. We'll add a few records by entering some data in the myDataColumn field only.

Recall that we used uniqueidentifier as the data type of myKeyColumn. This is actually what is usually called a GUID or "Globally Unique ID". This is a 128 bit integer that is actually created in such a way that no other exactly like it will ever exist on any computer. Pretty amazing, but there's a way to do that. Only a computer program can actually do it in the real world, however. The newId() command that was added to the table definition asks VWD to do it automatically when a record is entered.

So, click in the myDataColumn field and key in something, then press the tab key to move to the next record. You'll notice that nothing gets filled in to the key field. newId() is a SQL command and VWD needs to be told to execute it. Click the red "Execute SQL" button above the form to make this happen.

When you have entered a few records, you're ready to actually make use of the database.

©2013 About.com. All rights reserved.