Querying the database
This example shows how to access the database in Razor. You can use the Database object available to perform ExecuteNonQuery, ExecuteScalar and ExecuteReader calls. They are convenient shortcuts that will give you access to the Smartsite database.
If you need to access a different database or prefer to use the more common way to create a SqlConnection and SqlCommand instance, that is also possible. You will need to add a using statement for using the System.Data.SqlClient.
Smartsite SXML | Copy Code |
---|---|
@using Smartsite.Data @using System.Data 1. @(Database.ExecuteScalar<int>("select 12345").Value) 2. @(Database.ExecuteScalar<int>("select ?:myparam", 54321).Value) 3. @Database.ExecuteNonQuery("update Contents set Nr=2 where 1=0") 4. @Database.ExecuteNonQuery("update Contents set Nr=?:mynr where 1=0", 555) 5. @{ using (IDataReader reader = Database.ExecuteReader("select 111 union select 222")) { while (reader.Read()) { Write(reader.GetInt32(0)); } } } |