I am trying to translate some old C# code to emply the WebMatrix way of accessing Databases but I am struggling. Can someone please help me out ?
private static string connectionString = "Data Source=ASHIT\\SQLEXPRESS;Initial Catalog=amit;Integrated Security=True";
public static List<Item> method(DateTime start, DateTime end)
{
List<Item> events = new List<Item>();
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT event_id, description, title, event_start, event_end FROM event where event_start>=@start AND event_end<=@end", con);
cmd.Parameters.AddWithValue("@start", start);
cmd.Parameters.AddWithValue("@end", end);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//perform functions
}
}
return events;
}
So far, I have this much, and I am not able to complete past the “Using” line – where should I go from here?
public static List<Item> method(DateTime start, DateTime end)
{
List<Item> events = new List<Item>();
var db = Database.Open("plan");
var result = db.Query("SELECT event_id, description, title, event_start, event_end FROM event where event_start>= "+ start + " AND event_end<= "+ @end);
//not sure what to do from here
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//perform functions
}
}
return events;
}
I’ve never used WebMatrix’s database helpers, but based on the documentation you should be able to do something like:
Looking at the docs again, it looks like
db.Queryis going to return anIEnumerable<Object>so the code above won’t work unless you change it to (C#4 only):This may not be necessary based on the example here.