Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Visual Web Developer™ 2005 Express Edition For Dummies - Alan Simpson

.pdf
Скачиваний:
30
Добавлен:
24.05.2014
Размер:
12.36 Mб
Скачать

250 Part III: Personalization and Databases

Figure 11-28:

Fields that the query should retrieve are selected.

While you’re designing a query, you can test it out at any time using any of these methods:

Right-click some empty space in the Design surface and choose Execute SQL.

Press Ctrl+R.

Click the Execute Query button (red exclamation mark) in the toolbar.

Choose Query Designer Execute Query from the menu bar.

The results of executing the query appear in the Results pane at the bottom of everything else. Figure 11-29 shows the results of executing the query from Figure 11-28. Although it might not look like much, at first glance, it could be the solution to many of your data processing problems.

Figure 11-29:

Results of executing the query.

Chapter 11: SQL Server Crash Course 251

Each record in the view’s results represents a single transaction; the view always shows one record for every transaction made, up to the moment you see the results. In other words, if you ignored all fields except UserId and ItemId, you’d see that the view contains exactly the same records as the

Transactions table.

However, the view offers a big advantage over the Transactions table alone. Each record in the view includes the user name, date of the transaction, paid status, OurItemId, ItemName, and ItemPrice fields. In other words, the view contains a lot more useful information that the Transactions table provides — and offers greater control of your data in these ways:

The way the data looks in the query results is of no importance. Users never see query results like those you’re seeing in Web pages. They see the information in whatever format you present it to them — and you have near-infinite choices there.

Any time you “call upon” a view for information, it returns all the fields shown in the current query results — including current data from the tables — but the view doesn’t “contain” any data at all. Each time it’s executed, it has no choice but to get the specified information from the tables that are currently in the database.

A view can only be used to retrieve data from a table — not to edit data in the underlying table. That’s an advantage because in a Web site, nine times out of ten you want a Web page to show data to the user but not to allow the user to edit the data. Why? Well (for instance), imagine an unscrupulous user editing the page to change the price of an item in your Items table to a penny — and then ordering 100 of them. . . .

Using a view to access data from the tables provides the added security of knowing that a user cannot gain any access whatsoever to data in the tables from which the view retrieves its records. Also, the fact that the Web server doesn’t need to fuss with editing improves overall performance; data moves faster to the user’s page from the server.

To save a view, click the Close button in its upper-right corner and choose Yes when asked about saving your changes. Name the view (I’ll call this one UsersAndItemsView1) and click OK. The view name is added to the list of views in Database Explorer.

A more detailed view

One weakness of the previous view is that it didn’t include the user’s e-mail address. It can’t; the linked aspnet_Users table doesn’t contain the user’s e-mail address. That bit of information is stored in the aspnet_Membership table.

252 Part III: Personalization and Databases

There is, however, a view named vw_aspnet_MembershipUsers already defined under Views in Database Explorer. You can use that view in place of the aspnet_Users table to create a query that includes users’ e-mail addresses.

Creating another view named DetailedUsersItemsView illustrates the point:

1.Right-click the Views folder and choose Add New View.

2.In the Add Tables dialog box, click the Views tab, click vw_aspnet_MemberhsipUsers, then click the Add button.

3.In the Add Tables dialog box, click the Tables tab.

4.Click the Transactions table, then click Add.

5.Click the Items table, then click Add.

6. Click Close in the Add Tables dialog box.

7.In the diagram pane, drag the UserId field in the vw_aspnet_ MembershipUsers field list to the UserId field name in the Transactions table. The two fields will then be connected by a line as shown in Figure 11-30.

In Figure 11-30, I sized and arranged things so you can see all the field lists and the lines joining the tables by their key fields.

Figure 11-30:

The

Detailed

Users

Items

View’s tables and selected fields.

The query can return any fields from any table you see. In Figure 11-30, I chose some fields that might come in handy in the future. However, you can choose any fields you like.

Executing the query returns a set of records similar to those from the lessdetailed query — exactly one record per transaction shows up in the query

Chapter 11: SQL Server Crash Course 253

results. However, this view offers more information about who made each purchase — including each user’s e-mail address, the date he or she created the account, the date of the user’s last login, and more. Figure 11-31 shows the results — but actually the results table is wide enough to see all the columns. Suffice it to say that for every checked field name in Figure 11-30, there’s a field containing data in the query results.

Figure 11-31:

Results of executing the detailed query.

For purposes of future examples, I saved the query shown in Figure 11-30 as DetailedUsersItemsView, so in my own Database Explorer, that name has been added to the list of Views in the current database (as in Figure 11-32). To execute a view to see only the data it retrieves — without seeing its design — you can right-click the view name and choose Show Results.

Figure 11-32:

Two new Views added to the database.

The way the data looks in the view results is of zero importance. Formatting takes place when you display data from a view in a Web page.

254 Part III: Personalization and Databases

In real life, there would rarely be any need to open a view from Database Explorer and see the records it returns, because the way the results are shown in Database Explorer aren’t especially pretty or easy to read. Instead — normally — you’d use the view to extract data from your tables via databound controls in the Web pages you create.

Each data-bound control can choose as much (or as little) information from the view as it needs. For example, one control could display a list of all users who purchased a specific item. Another could list all items purchased by a specific user. Still other controls could organize information from the view in other ways.

Creating a Table of Pictures

Not all database tables need to be linked to other tables. You can use a database table to store things that don’t relate directly to specific users or specific items. For example, if you have pictures you want to show or allow people to download, you can create a table of pictures. Then, as you’ll see in Chapter 12, you can use Data controls to make it easy to display multiple pictures on a page.

You don’t need to put the pictures directly in the database. Rather, you can store all the pictures in a folder (or multiple folders). Then just create a table that identifies the picture’s location within your site. As an example, Figure 11-33 shows where I’ve created a subfolder named FlowerPix within my Images folder in Solution explorer. Each of the filenames, Flower02,jpg, Flower02.jpg, and so forth, is a photo of a flower.

Figure 11-33:

The FlowerPix folder contains ten pictures of flowers.

Chapter 11: SQL Server Crash Course 255

Next, you can create a table that contains three fields. One field will be a primary key that just assigns each record a unique number. Even though this table won’t be on the “one” side of a one-to-many relationship, a table needs to have a primary key if you want to be able to edit it through Web pages. So we’ll add a primary key to the table we’re about to create.

The second field will be a caption or title for each photo. The third field will be the path and filename of a photo. The path to each picture will look something like this:

~/Images/FlowerPix/Flowerxx.jpg

where ~/Images/FlowerPix means “the FlowerPix folder in the Images folder, under the root (~) folder.” The Flowerxx.jpg will be the name of a specific picture within that folder. I’ll name the sample table Photos. You create it as you would any other table. Here are the steps:

 

1.

If you’re in Solution Explorer, click the Database Explorer tab.

 

2.

Right-click on Tables under the database name and choose Add

 

 

New Table.

`

3. Type PhotoId as the first field name, and make it the smallint

 

 

data type.

 

 

If you think you’ll have more than 32,000 pictures, use the int data type

 

 

rather than smallint.

 

4.

Clear the check mark from the Allow Nulls field.

 

5.

In the Column Properties, click the + sign next to Identity Specification.

 

6.

Set the (Is Identity) property to Yes.

 

 

You can leave the Identity Increment and Identity Seed each set to 1, so

 

 

records are just numbered 1, 2, 3, 4, and so forth.

 

7.

Right-click the PhotoId field name and choose Set Primary key.

 

 

Figure 11-34 shows how the table design should look so far.

Figure 11-34:

The

PhotoId field is an autonumbered primary key.

256 Part III: Personalization and Databases

8.Add a second field named PhotoCaption, and set its Data Type to varchar(50).

9.Add a third field named PhotoURL and set its Data Type to varchar(64).

Figure 11-35 shows the final structure of the table.

Figure 11-35:

Completed

Photos table design.

When defining a field that stores any kind of URL or address, use either varchar or nvarchar. Using char or nchar will pad short addresses with blank spaces, which in turn may prevent the link from working properly later when used on a page.

10.Click the Close (X) button in the upper-right corner of the Design surface.

11.Choose Yes when asked about saving your changes.

12.Change the name of the table to Photos, then click OK.

Now you have a new table, named Photos, in the database. Each record in the table can contain a photo caption and the link to a photo. If you already have your pictures in a folder, you can right-click the table and choose Show Table Data.

Because PhotoID is an automatically-numbered field, you want to leave it as Null when entering new records. Just type a caption name and a link to a picture in the PhotoCaption and PhotoURL fields. Then type a caption and link for each photo.

For my example, where each photo is a picture of a flower, I put a (fake) Latin name for each photo into the PhotoCaption field, and a link to each photo in the PhotoURL field. (I don’t know the actual Latin names for the flowers, so I just made some up, borrowing a few names from animal anatomy.) Figure 11-36 shows an example. I’ve opened Solution Explorer in that figure so you can see how the PhotoURL in each record refers to a specific image in the ~/Images/FlowerPix folder.

Chapter 11: SQL Server Crash Course 257

Figure 11-36:

Some sample records in my Photos table.

The table doesn’t look like much with just the text in it. However, keep in mind that a table is always just “raw data” and how things look in the table doesn’t dictate how they’ll look on a page. As you’ll discover in Chapter 12, you can use a DataList control with the Photos table to show the actual pictures and captions on a page. For now it’s sufficient to just close and save the table.

Creating a Table of HyperLinks

If your site has many links to Web sites outside your own site, you can store all those links in a database table. Doing so keeps all the links in one place, where they’re easy to manage. You can then use whatever links you want on any page you want without having to worry about keeping links up-to-date across many different pages within your site.

For a table of links, you’ll need at least two text fields, one for the name or title of the site, and one for the site’s URL (address). If you like, you could add a third field for storing a description of each site. Also, if you want to be able to edit the table’s data through Web pages, the table will need a primary key.

To keep things fairly simple, I’ll show you how to create a table that contains a primary key, a field for storing each site’s name, and a field for storing each site’s address. The steps are:

1.If you’re in Solution Explorer, click the Database Explorer tab.

2.Right-click on Tables under the database name and choose Add New Table.

3.Type SiteId as the first field name, and set its Data Type to smallint.

If you think you’ll have more than 32,000 links, use the int data type rather than smallint.

258 Part III: Personalization and Databases

4.Clear the check box for Allow Nulls.

5.In the Column Properties pane, click + next to Identity Specification.

6.Set the (Is Identity) property to Yes.

You can leave the Identity Increment and Identity Seed fields each set to one.

7.Right-click the SiteId field name and choose Set Primary Key.

Figure 11-37 shows the table structure so far, with just the auto-num- bered primary key SiteId defined.

Figure 11-37:

The primary key for a table of hyperlinks.

Figure 11-38:

Structure of

my sample

Links

table.

8.Add a second field to store the site’s title. For my example, I’ll name that field SiteName and set its Data Type to nvarchar(50).

9.Add a third field for storing the site’s URL. In my example, I’ll name it SiteURL and give it the varchar Data Type with a maximum length of 64 characters.

Figure 11-38 shows my completed table structure.

10.Click Close in the upper-right corner of the Design surface.

11.When asked about saving the table, choose Yes.

12.Name the table Links and click OK.

Chapter 11: SQL Server Crash Course 259

To add data to the table, right-click its name in Database Explorer and choose Show Table Data. When typing in data, remember to leave the SiteId field set to Null, as it will be numbered automatically after you’ve filled the other fields. You can type any text for the site’s name or title. But when specifying the site’s URL, make sure you use the full http://... address.

Figure 11-39 shows an example where I’ve put in some site titles and the links to those sites. The links don’t actually do anything in the table. Remember, the table is just a means of storing the data. But as you’ll discover in Chapter 12, you can use a DataList control to create a page that displays each site title as a link that takes the user directly to the site.

Figure 11-39:

Storing links in a table.

Thus ends the world’s quickest SQL Server/Database Design crash course. There is a great deal more to know about those topics. (But you knew that.) What you’ve discovered here should be enough to get you started. If nothing else, it has covered a lot of things many other VWD information resources will assume you already know. Chapter 12 shows many ways of putting the data to good use within the site.

Соседние файлы в предмете Информатика