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

Beginning ASP.NET 2.0 With CSharp (2006) [eng]

.pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
20.33 Mб
Скачать

Appendix D

2.Add tables as desired and close the Add Table dialog box (see Figure D-9).

Figure D-9

The diagram can be so large that it is easy to miss tables. Note the Windows-standard horizontal and vertical scroll bars to navigate the diagram (see Figure D-10).

Figure D-10

678

VWD Database Explorer

3.You can rearrange the tables (the relationship lines will automatically adjust). Start by changing the zoom size to 30% or so to be sure you see all of the tables and then do some rearranging. The zoom window is on the diagram toolbar, shown in Figure D-11 set to 30%.

Figure D-11

4.Increase the zoom size to be able to read the data. For example, after rearrangement, 70% shows all the tables for Wrox United, as shown in Figure D-12.

5.Right-click the title bar of a table (for example, Players) and select Properties from the menu. In the Properties box (in the lower right of the monitor), you can view parameters such as the Identity column. Right-click one field (see Figure D-13) of a table and select Properties to see how that column has been configured, including the data type and length.

6.Carefully place the tip of the mouse arrow on a relationship line (for example, between Fixtures and MatchReports) so that the mouse arrow changes to a horizontal double-ended arrow. Rightclick this relationship and select Properties to view data on the relationship, including enforcement rules, as shown in Figure D-14.

679

Appendix D

Figure D-12

Figure D-13

680

VWD Database Explorer

Figure D-14

7.You can also print the Database Diagram or save it to include in documentation for the site. Note that there are tools to position page breaks at specific axes of the diagram, as well as a tool that allows you to add a text box of notes.

Exploring a Table’s Structure

In addition to the Database Diagram, you can look at tables through the Table node of the Database Explorer, as follows:

1.Starting in Database Explorer, expand the list of databases and then the database of interest. Expand the Tables node and select a table name (for example, MatchReports). In the Properties window, shown in Figure D-15, you will see the approximate number of records.

Figure D-15

681

Appendix D

2.Double-click a table name to open a list of the columns (fields) and populate the Properties windows with data on the table as a whole, as shown in Figure D-16.

Figure D-16

These metadata can be changed in this view, assuming that you have the rights to make changes to the database structure.

Observing and Editing Data of Existing Tables

If you right-click a table name in the Tables node of Database Explorer, you’ll see an option to Show Table Data. This is a quick way to find out if your data-enabled web pages are actually carrying out their tasks. You can also add, modify, or delete records to test results in data-reading pages.

Be extremely careful about changing data with the Show Table Data tool. For example, deletions or changes of customers may divorce them from their orders. In some databases, the addition of a record may require the addition of a partner record in another table. A simple correction to a spelling mistake may cause a failure to properly look up data that is based on the old spelling. If you thoroughly understand the schema and are changing independent data, you may avoid trouble. But in most cases, it is better to make revisions based on the interface that ensconces proper validation, limits, and controls.

682

VWD Database Explorer

Creating a New Database

It is rare that you will use VWD to create a database from scratch. Good databases require the kind of planning and implementation tools that come with your database management system. However, in the case of a small and simple database, VWD does offer the necessary planning and implementation tools. You can add a new database in two ways: directly in the Database Explorer or by adding a SQL Database through the Add New Item menu in Solution Explorer.

Follow these steps to create a database in Database Explorer:

1.In the Database Explorer, right-click Data Connections and select Create New SQL Server Database.

2.Select a server. If you’re using SQL Server Express, use this exact syntax for the server name: (local)\SQLExpress. Use Windows authentication unless you have created an authentication scheme internal to SQL Server Express. Type the name of the new database into the bottom text box.

Database Explorer will automatically add the database to its list and you are ready to create tables and add data.

You can also create a new database directly in the Solution Explorer. This technique is very similar to the preceding procedure except for the first few steps:

1.Start in the Solution Explorer and right-click the root of the web site. Select Add New Item and select the template of type SQL Database.

2.When prompted, agree to add to the App_Data folder.

VWD will now roll you over to the Database Explorer where you will see the new database and folders for its objects (albeit empty). You can now create tables, add data, and perform other tasks.

Creating a New Table and Adding Data

Tables can be added and populated in the Database Explorer, as follows:

1.Expand the database, right-click the Tables node, and select Add New Table from the menu shown in Figure D-17.

Figure D-17

683

Appendix D

2.In the resulting list of columns, enter the names and data types. For example, as shown in Figure D-18, a new table that holds a schedule of reporter’s interviews with the players would start with a column (field) for InterviewID.

Figure D-18

Note that under Data Type, you can scroll down to find the UniqueIdentifier type, which is similar to AutoNumber in Access.

3.In the panel below the columns list, you will find additional properties for the current column such as default value and length.

4.Set the primary key by selecting a field and clicking the key tool in the table’s toolbar.

5.Finish by choosing Menu File Save Table.

Examining and Creating Views

Views provide a set of data. Instead of returning information directly from a table, a view returns only certain fields, modified fields, or records. In many databases, the security rules will allow a web site to request a view but not directly request data from a table — this provides a level of control for the database administrator. For example, a table of employees may hold a field for salary level. A view that

684

VWD Database Explorer

is authorized for the accounting department would include that field, but other users only have access to views that do not include the salary data. Views are also useful for creating hybrid data, such as combining a NameFirst field and a NameLast field to show both names in a single field of a list box.

You can do a basic exercise where you create a view that would support a selection ListBox of Players. You want the view to create two fields — the first is the player’s ID and the second is a combination of the player’s last and first names, separated by a comma. Follow these steps:

1.In Database Explorer, expand your database, right-click the Views node, and select Add a New View. This opens the designer for a new view in the background, and in the foreground, the Add Table dialog (see Figure D-19) will open.

Figure D-19

2.In the Add Table dialog box, select Players and click Add. Close the dialog box.

3.Before you go on, experiment with turning on and off the panels to display a view. From left to right, they are the Diagram, Query, SQL Statement, and Results. Because you are making a small view, you can display all four. Figure D-20 shows the four panels. On the top is the pane of tables, showing just the Players table with the single field of PlayerID selected. The next panel down is the query designer, which shows the single field selected. The next panel shows the very simple SQL statement: SELECT PlayerID FROM dbo.Players. The bottom panel shows the results of choosing Query Designer Run to produce the results table.

685

Appendix D

Figure D-20

4.Click the check box next to the PlayerID field in the Player table of the Diagram View (the top panel) and note that the field is added to the Query and SQL Statement panels.

5.In the Query panel, go to the second row and type FullName into the alias cell. In the left column, type LastName + ‘, ‘ + FirstName.

6.Click the execute SQL tool (the red exclamation point) to see the results, displayed in Figure D-21.

686

VWD Database Explorer

Figure D-21

7.Order the players’ names by last name. SQL does not like to order on a created (or alias) field, so go to the third row of the Query panel and add the LastName field. Turn off its Output option. Set the Sort Order column to 1, meaning this is the first criterion for sorting. In the Sort Type, VWD will automatically set the order to Ascending. These four changes are shown in Figure D-22. This step tells SQL to sort according to the last name, but not to output the LastName as its own field. Execute the SQL.

687