Create ASP.net data storage layer (2)

Source: Internet
Author: User
Asp.net| Create | Data use Visual Studio. NET to define database tables

The easiest way to define a database table in Visual Studio. NET is to create a database diagram. In this way, you can define all the details in an editor similar to WYSIWYG. You can simply expand the appropriate tree in Server Explorer, right-click on the database diagrams item, and then select New Database Diagram from the context-related menu ... (New database Diagram ...) ), you can start a new, blank diagram. The first time you start a diagram, the system will ask you to select a table from the database. Because you have not created any tables, you can ignore the dialog box. Now you're ready to start defining your table.

In the generated ASP. NET application planning and design the user scenario document contains the information that is required to define the table. Learning a scenario that defines how new records are added to a system is often the best way to understand what data needs to be stored. Sometimes you need to look at other scenarios, such as record updates or even reports, to make sure that no other fields are missing. In this example, the add a record scenario is a good reference scenario.

For example, the following is a scenario for adding a problem:

Add a new problem

Add a new problem record to the system, including the title, date/time, the topic ID that indicates the category to which the issue belongs, and the question body. It is also sometimes necessary to provide the name of the person who raised the question and its e-mail address. After you add a new question, a unique integer problem ID is returned to the calling function.

Important nouns are expressed in bold. When reading a scenario description, these nouns or table names (for example, "Problem history" is a good example) can often indicate what data needs to be stored. Using the information above, you can add a new table to the database diagram and define the columns you want. The following example details how to add a table in a database diagram.

Right-click on the diagram "surface" and select New Table from the context-related menu ... (New Table ...) )。 Enter Questions as the table name, and then click OK to add it to the diagram.

In the Questions Table dialog box, type the field information that is provided in the scenario described above. For example, column name (column name) = ID, data type (datatype) = int, length (length) = 4, and deselect Allow Nulls (allow null) check box. Repeat for the remainder of the table (see Figure 3).


Figure 3:questions (problem) Table

You will see that there is a small gold key next to the first column (ID). It indicates that the field is the primary key field for the table. To set the primary key field, you can right-click the column name in the list and select Primary key from the context-related menu. In addition, you should set this ID field to an identity column that is automatically incremented incrementally. This allows SQL Server to automatically generate a unique integer value for each record that is added to the table. To do this, right-click on the column, select Properties from the context-related menu, and then select the Columns (column) tab in the Properties Pages dialog box. Other operations are easy (see Figure 4).


Figure 4:property the Columns (columns) tab in the Pages (Property pages) dialog box

You can create additional two tables by using the information in the Add subject record and add answer records scenarios. Be sure to create an ID column for each table and mark these columns as identity columns and primary keys. Figure 5 below shows three completed tables.


Figure 5: Three completed tables

You will find that these tables are connected to the database diagram through a connector. These connectors indicate that there is a foreign key relationship between the tables. For example, the TopicID column in the Questions (problem) table is associated with the ID column in the topics (subject) table. By storing this relationship in a database, you can develop rules that prevent illegal data from being saved in the database. The relationship rule in this example is that the valid value allowed by the Questions.topicid column can only be the value of a record that already exists in the Topics.id column.

You can define these relationships by dragging the TopicID columns in the Questions table to the ID columns in the topics (subject) table. A dialog box appears, displaying the details of the rule definition and asking you to confirm it by pressing the OK button (see Figure 6).


Figure 6:create Relationship (Create Relationship) dialog box

You may find that a relationship is also defined between Responses.questionid and questions.id.

Note: You may have noticed that we have not defined any tables for the experts. I decided to store information about the experts in an XML file rather than in a database. The main reason for this is that we can discuss how to read and write XML data in order to fuse XML and relational data in the same application. We will discuss the issue of expert data in the next section.

At this point, both the database and the table have been defined. The above describes the actual data stored procedures of the solution. However, we also need to know how to read and write information in a table. To do this, we will define the stored procedures in SQL Server.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.