ASP.NET Web Pages - Databases
This chapter is about working with databases.
What We Will Do
In this chapter we will:
- Create a web page to list data from a database
Displaying Data from Database
With Web Pages, you can easily display data from a database.
You can connect to an existing database, or create a new database from scratch.
In this example we will connect to an existing SQL Server Compact database.
If you want to learn how to create a database for your web, please go to the chapter Web Database.
Adding a Customers Page
In the "DemoWebPages" folder, create a new CSHTML file named "Products.cshtml".
Replace the code in the file with the code from the example below:
Products.cshtml
@{
var db = Database.Open("SmallBakery");
var selectQueryString = "SELECT * FROM Product ORDER BY Name";
}
<html>
<body>
<h1>Small Bakery Products</h1>
<table>
<tr>
<th>Id</th>
<th>Product</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach(var row in db.Query(selectQueryString))
{
<tr>
<td>@row.Id</td>
<td>@row.Name</td>
<td>@row.Description</td>
<td align="right">@row.Price</td>
</tr>
}
</table>
</body>
</html>
var db = Database.Open("SmallBakery");
var selectQueryString = "SELECT * FROM Product ORDER BY Name";
}
<html>
<body>
<h1>Small Bakery Products</h1>
<table>
<tr>
<th>Id</th>
<th>Product</th>
<th>Description</th>
<th>Price</th>
</tr>
@foreach(var row in db.Query(selectQueryString))
{
<tr>
<td>@row.Id</td>
<td>@row.Name</td>
<td>@row.Description</td>
<td align="right">@row.Price</td>
</tr>
}
</table>
</body>
</html>
Run example »
No comments:
Post a Comment