WebMatrix tutorial (4) (read database)

Source: Internet
Author: User
Summary

WebMatrix is indeed very helpful for rapid website construction. I used it to build projects based on WordPress, Joomla, DNN, and other platforms, saving a lot of setup troubles, just click Next and enter some basic information.

 

This document describes how to use and read the SQL Server CE database in the WebMatrix project.

If you have heard about WebMatrix for the first time, refer to the first three articles in this series.

 

Body

1. Open WebMatrix and select site from Template, and then select Starter Site Template. Here I name the website Starter Site. Then click Files below to see the initial structure of the website.

 

2. Click Databases to create a database named starter. Select New Table from the menu, change the Table Name to Product, and add the New column Id and Name. Here we set Id as the primary key.

Different from SQL Server, there are very few attributes here.

 

After selecting Table product, click "Data" in the above menu. You can enter some Data to the Table:

3. Click Files to return to the file browsing view. Select About. Cshtml

 

Add the following two lines of code:

Var db = Database. Open ("starter ");
Var selectQueryString = "SELECT * FROM Product order by Name ";

Note that you do not need to set the connection string in web. config.

Add the following code to read all the data and display it on the webpage:

 

@ Foreach (var row in db. Query (selectQueryString ))

{
<Tr>
<Td>
@ Row. Id
</Td>
<Td>
@ Row. Name
</Td>
</Tr>

}

 

4. Click start to view about. cshtml:

 

Click here to download the source code

 

Thank you for reading this article

For more exciting articles, read ASP. NET (Alex Song)

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.