Database usage in App_Data

Source: Internet
Author: User

The original Published time: 2008-07-24--from my Baidu article [imported by moving tools]

Asp. NET using VWD to manipulate the database

Establishing a local database

You can easily establish a local database in Visual Studio's Web Application project. Because Visual Studio has SQL Server Express installed, you can use the following steps to build a local database, add a data table, and populate the data. [Newsad] [/newsad]

To establish a local database:

1. Right-click on the Solution browser and select the "Add New Item ..." option.

2. Select the SQL database item and specify a file name, such as "Database.mdf".

3. Visual Studio prompts to add this file to the App_Data directory. Click "Yes".

4. Visual Studio adds this file and automatically connects to the database using the database browser.

To add a data table to a local database:

1. Right-click on the Tables (data table) node in the database browser and select the Add New Table option.

2. Enter the name and type of the column in the database and optionally set some other column properties in the attribute table. To establish the sample contacts database, follow the steps below.

3. Set the name of the first column to "ContactID" and set the data type to "int". Cancels the check box for allow nulls.

4. Right-click on the gray square to the left of the ContactID column and select the "Set as primary" option.

5. In the Column Properties table below, expand the Identifier specification node and set whether is identifier to Yes.

6. Set the name of the second column to "ContactName" and set the data type to "varchar (50)". Leave the Allow Nulls check box selected.

7. Press Ctrl-s to save the table and set the table name to "Contacts". Click OK to save the table.

8. Close the table Definition window.

Populate the table with data:

1. Right-click the data Table node in the database browser (for example, "Contacts") and select the "Show Table data" option.

2. Enter data for the data table row in the displayed table. If you use the contacts example above, you can enter a value in the ContactName column, and the database will automatically generate the corresponding ContactID value.

3. Close the table window.
Bind to local Database

To bind to a local database, you need to configure the ASP. NET data source control to connect to the file by using a relative path connection. To easily bind a SQL Server Express database to the SqlDataSource and GridView controls, you can use the following steps:

1. Double-click a page in the solution browser (for example, "default.aspx"). Visual Studio will open this page.

2. Select the Design View tab at the bottom of the page window to switch to Design view.

3. Double-click the database file in the solution browser (for example, "Database.mdf"). Visual Studio opens the database browser for the connection.

4. Expand the Tables node to display the tables in the database.

  5. Drag and drop the table in the database browser into the open page in Design view. Visual Studio establishes a GridView that is bound to the SqlDataSource Control.

6. Expand the Smart transactions panel for the GridView control, and check pagination, sorting, and editing.

7. Press CTRL-F5 to run the page (no debugging).

The following example shows a GridView and SqlDataSource control that is connected to a local database. In order to run this example, ASP. NET process account must have read/write permissions to the MDF and LDF files in the ~/app_data directory. This permission setting process is as follows:

1. Select the ~/app_data/database.mdf file in Windows Explorer and choose Properties.

2. Select the "Security" tab and click "Add".

3. Click "Location ...", select your computer name (at the top of the list), and click "OK".

4. In the object Name text area, enter the name of the ASP. By default, the name in IIS 6.0 is "Network Service", IIS 5. In X is "ASPNET".

5. Select "Read" and "write" in the "Allow" column and click "OK".

6. If an LDF file exists in the App_Data directory, you will need to repeat the above steps to set the properties of the LDF file.

If you are using Visual Studio to build an application on your local computer, by default you have the above permissions for the App_Data directory. If a database file needs to change permissions after it is attached, you must first close the application domain (application domains) before the new permissions take effect.








Database usage in App_Data

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.