ASP. NET 2.0 data Tutorial: create a Web project and configure database connection

Source: Internet
Author: User
Tags mdb database microsoft sql server 2005 microsoft website

As web developers, we live our lives around data operations. We create databases to store data, write codes to access and modify data, and design webpages to collect and summarize data. This article is the first of a series of tutorials on how to implement these common data access modes in asp.net 2.0. We will start from creating a software framework, which consists of a data access layer (DAL) that uses a strongly typed DataSet ), A business logic layer (BLL) That Implements user-defined business rules and a presentation layer composed of asp.net web pages with shared page la S. After laying the foundation for this backend, we will begin to switch to reports to demonstrate how to display, summarize, collect, and verify web application data. These tutorials are designed to be concise and concise. They use many screens and provide step-by-step guidance to take you through this development process. Each tutorial has C # and VB versions, and is accompanied by a complete download of the involved encoding. (This is a long tutorial, but other tutorials will be available in the future at a much easier time .)

In these tutorials, we will use the Microsoft SQL Server 2005 Express version of the Northwind database in the App_Data directory. In addition to database files, the App_Data directory also contains SQL scripts used to create databases, in case you want to use other database versions. If you want to, you can also download these scripts directly from Microsoft. If you use another SQL Server version of the Northwind database, you need to update the setting of NORTHWNDConnectionString in the Web. config file. The web application in this tutorial is a file system-based website project created using Visual Studio 2005 Professional Edition. However, all the tutorials can be run in Visual Web Developer, a free version of Visual Studio 2005.

In this tutorial, we will create a data access layer (DAL) from the beginning, and then create a business logic layer (BLL) in the second tutorial ), in the third tutorial, design the page layout and navigation. The subsequent tutorials will be based on these three tutorials. In the first tutorial, we will discuss a lot of content, so open Visual Studio and let's get started!

Step 1: create a Web project and configure database connection

Before creating the data access layer (DAL), we need to create a website and a database. We started from creating a file system-based asp.net website. The order is as follows. Open the File menu and select a New website. The system displays a New website dialog box, and select asp.net website template ), set the Location list to File System, select the folder for storing the website, and select C # as the programming language #.

 

Figure 1: create a file system-based website

Visual Studio generates a new website for you and a webpage named Default. aspx and an App_Data folder.

After the website is generated, the next step is to add a reference for your database in the Server Explorer of Visual Studio ). After adding a database to the server resource manager, you can add data tables, stored procedures, views, and so on in the Visual Studio environment. You can also view the data in the database, and create your own Query statements manually or using the Query Builder GUI. In addition, when creating a strongly typed DataSet for the DAL, we need to point Visual Studio to the target database as the DataSet data source. Although we can provide the database connection information when appropriate, if we pre-register these databases in the server resource manager, visual Studio automatically fills these databases in a drop-down list.

The steps to add the Northwind database to the Server resource manager depend on the SQL Server 2005 Express database you want to use in the App_Data folder, you still want to use the SQL Server 2000 or 2005 database Server that has been established.

Use a database in the App_Data folder

If you do not have an accessible SQL Server 2000 or 2005 Server, or you want to avoid adding a database to the database Server, you can use the Northwind database of SQL Server 2005 Express, the database is located in the App_Data folder (NORTHWND. MDF ).

Databases placed in the App_Data folder are automatically added to the server resource manager. Suppose you have installed SQL Server 2005 Express on your machine, you should see a file named NORTHWND in the Server resource manager. MDF node, you can expand this node, browse the data tables, views, stored procedures, and so on (see figure 2 ).

The App_Data folder can also contain Microsoft Access. mdb database files. Similar to SQL Server Data Base files, these Access files are automatically added to the Server resource manager. If you do not want to use any SQL Server database, you can download the Northwind database file of Microsoft Access and place it in the App_Data folder. But remember, the Access database is not as versatile as SQL Server, and it is not designed to be used in website scenarios. In addition, the database-level functions not supported by the Access database will be used in the following tutorials.

Connect to the database in Microsoft SQL Server 2000 or 2005 Database Server

Alternatively, you can connect to the Northwind database installed on the database server. If you have not installed the Northwind database on the database server, you must first run the installation script in this tutorial to add the database to the database server, alternatively, you can directly download the SQL Server 2000 Northwind database and installation script from the Microsoft website.

After the database is installed, go to the server resource manager in Visual Studio, right-click the Data Connection node, and select "Add Connection )". If you cannot see the server resource manager, go to View and click server resource manager, or press Ctrl + Alt + S to open the server resource manager. This will open the Add connection dialog box, where you can set the server to be connected, authentication information, and database name. After you successfully configure the database connection information and press OK, the database will be added to a node under the data connection node. Then, you can expand database nodes to view data tables, views, stored procedures, and so on.

 

Figure 2: Add a connection to the Northwind database on your database server

  1. How to deploy the asp.net mvc program in IIS6.0
  2. Use Winform to build the asp.net mvc Framework
  3. Programming idea of ASP. NET Session failure
  4. ASP. NET Session state storage
  5. Understand ASP. NET Web Application Models

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.