This example demonstrates how to retrieve records from a database using Razor in WebMatrix, I have used dummy Patient database and Patients table with some dummy records. I have created a page with named Patient.cshtml.
You have to write a Razor code inside the code block mention below.
@{
}
It is very easy to access a database using Razor syntax; the following code shows how to access a database using WebMartx Razor.
@{
var db = Database.Open("DATABASE NAME");
}
db.Query(“query”) fetch the records from open database
foreach (var p in db.Query("SELECT * FROM PATIENTS")){
}
Above code will retrieve records from database through loop.
Below figure shows the code of entrie page.

Note: You can see the output by clicking on Run button at top of menu or click F12 from WebMatrix.
Conclusion
In this tutorial you have learned, how to retrieve records from database using feature WebMatrix, SQL Server CE and Razor.
Next time I will introduce more feature of WebMatrix, I hope you liked this article. If you have alternative solution, comments here to share with us.
Output

Download
patients.cshtml (1.11 kb)