ASP. NET building data layer learning notes (1)

Source: Internet
Author: User
Tags microsoft sql server 2005 table definition sql server express

ASP. NET:
Microsoft ASP. Network 2.0
Microsoft Visual Web Developer 2005 Express Edition

Read ASP. NET to build the data layer. This article will show you the complete process of creating an application from start to end. The Starter Kit to be built is called the Media Share Library Starter Kit. Media Share Library Starter Kit allows you to easily create applications that allow registered users to display Media projects, such as movie DVDs, music CDs, books, and so on. The user can browse the project libraries owned by the group using the application together, and request to borrow some specific projects from the project registration owner. The idea of Media Share Library Starter Kit is that you can use the framework provided by it to quickly organize a collection of Libraries Shared by more people.

Whether you use the starter kit or not, it indicates that ASP is in the latest version. in. NET 2.0, Microsoft ASP. NET has more exciting additional features, which is exactly what it is. The starter kit uses a large number of master pages, new code hiding models, and new server controls. This article focuses on how to use some exciting and new data functions of the beginner's toolkit, for example, you can use the new Microsoft SQL Server Express Edition file and the new data source control to insert, update, and delete data.. NET 2.0.

Let's start to build the data layer of the starter kit by ourselves. First, we will learn how to use the new SQL Server Express Edition to build our own data storage.

Use SQL Server Express

Microsoft SQL Server 2005 Express Edition is a new database product based on the technology used by Microsoft SQL Server 2005's mature version. SQL Server Express Edition is a tailored version of SQL Server 2005. You do not need to use other database files, such as Microsoft Access. mdb File) to build ASP.. NET application data layer.

In the past, many Web developers and amateurs found that using Access files as data storage for applications is very simple. These files are not only easy to install and implement, in addition, it is easy to copy from one location to another with other files of the application. For this reason, Microsoft provides the SQL Server Express Edition file with the new. mdf file extension, which you can process in the way that the preceding Access file is processed. These. mdf files can be easily created with excellent functions integrated with Visual Studio. NET application is easy to use. It is also easy to copy files from one location to another through x-copy, just like ASP. other files in the. NET application can be easily deployed.

In addition, there are a lot of convenience in use. The new SQL Server Express product supports new data types such as XML and VARCHAR (MAX), and even user-defined data types.

First, we will learn about the database files used to store user and role information.

Construct the ASPNETDB. MDF file

The beginner toolkit described in this article processes registered users. Therefore, you may want to know the location where the beginner toolkit stores user and role information. When you first build and run an application, you will notice that Visual Studio 2005 dynamically creates a database file that stores this information.

After you press F5 to start and build the application, ASP. NET dynamically creates the required ASPNETDB. MDF database file, which is used to store user login information and role information for the application.

In the App_Data folder, apart from the ASPNETDB. MDF file and the Library. mdf file, we will briefly introduce this file later. If the file does not exist in the folder, press the "refresh" button in the toolbar of Visual Studio Solution Explorer. Visual Studio enables you to perform any operations you need on this file and other SQL Server Express database files. In Solution Explorer, double-click the ASPNETDB. MDF file to open the database file in Visual Studio Server Explorer.

You can not only use tables contained in a database, but also use database charts, views, stored procedures, functions, synonyms, types, and assemblies. The screen snapshot of the ASPNETDB. MDF file displays all the tables and configuration file information associated with the user when using user logon information and roles.

We will not modify the ASPNETDB. MDF file because it is a file required by the ASP. NET engine to run. You can add only the required new tables to the database file and run the entire application only from the database file. However, the best practice is to take your custom database operations from the default ASPNETDB. the operations performed by the MDF file are separated. Next, we will introduce how to create a Library. mdf database file. The Media Share Library uses this file to store all information about the projects saved by the application, including whether each saved project is in the borrow state.

Build the Library. mdf file

To build the data layer of the next Media Share Library, you can build a simple database file to store all the underlying data that the application needs to store. What is actually stored in this database? Well, in fact, users need to store their own media library DVDs, CD, books, and so on) and information about the borrow state of the project. If you have read the first article about ASP of the beginner's toolkit. NET Jumpstart article, it is not difficult to understand this problem.

To create the required database file, right-click Visual Studio Solution ExplorerApp_DataFolder, selectAdd New Item. This operation starts the "Add new project" dialog box. Select the SQL database in the dialog box as the file type used to create the application. Ensure that the file is named Library. mdf.

Once this file is created, a new Library. mdf file appears in the App_Data folder. Double-click the file to open it in Visual Studio Server Explorer, which is the same as the ASPNETDB. MDF file.

If you try to expand the Tables folder in the server resource manager, there is no response except the plus icon on the left side of the folder. This is because there are no tables in the current Library. mdf file.

To create the first table, right-clickTablesFolder, and selectAdd New TableYou can. You need to create two tables for the application in this article. The first table is named MediaTypes, which processes the custom category used by the Media Share Library application. Remember that every project stored in the library needs to be classified in some way, such as DVD, CD, and books. These categories are dynamically generated based on the content of the MediaTypes table. MediaTypes is a simple table to be created.

When creating a table, you only need to provide brief information about the columns to create these database columns.

When creating a database information table, it is usually appropriate to use a unique keyword to identify each row in the table. Therefore, the MediaTypes table contains a column named MediaTypeId. to define an absolutely unique ID value for each row, the column type isIntAnd does not allowNullValue. However, this does not apply to column definitions. Next, right-click the gray box starting with a line and selectSet Primary Key. A yellow keyword is displayed at the beginning of the line. In addition, make sure that each ID you create is used as an identifier and each ID is unique. Highlight a column to see the column attributes defined in the document window. Set Identity Specification in this attribute pane to indicate that the ID is unique and used for identification.

After setting the MediaTypeId column, the next step is to create the other two columns in the Table: MediaTypeName (nvarchar (50) and MediaComments (nvarchar (MAX )). When browsing this table, you can easily identify the purpose of each column in this application. As mentioned above, MediaTypeId is used as the identifier of each row in the table. MediaType is the category name that the application actually presents to the end user. In fact, the application does not use MediaComments at all. It only allows developers or database administrators to provide comments for each category as needed.

Once the table is ready, pressCTRL + STo save the table definition. A dialog box is displayed, prompting you to name the new table. Make sure that the table is named MediaTypes.

After creating the MediaTypes table, create the next table, which is used to store all the library projects saved in the application. The table also indicates whether the items are borrowed by someone, who the borrower is, and when the items are returned to the owner.

From this table definition, you will find that some data items come from other tables or databases used by the application. For example, the UserName of the registered user stored in the UserName column. This value was initially obtained from the ASPNETDB. MDF file through the ASP. NET 2.0 membership system. In addition, the MediaType column only stores a number, which is related to a unique ID used in the MediaTypes table created earlier.

Now that the two tables are ready, we will introduce how the application extracts and stores data from these tables. Next we will continue to introduce ASP. NET's data layer construction.


Related Article

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.