Skip to content

Instantly share code, notes, and snippets.

@kppullin
Created October 17, 2011 16:12
Show Gist options
  • Save kppullin/1292976 to your computer and use it in GitHub Desktop.
Save kppullin/1292976 to your computer and use it in GitHub Desktop.
SimpleData Raw Query Extension Method
public static IEnumerable<dynamic> RawQuery(this Database database, string sql)
{
using (var conn = ((Simple.Data.Ado.AdoAdapter)database.GetAdapter()).ConnectionProvider.CreateConnection())
using (var cmd = new SqlCommand(sql, (SqlConnection)conn))
{
if (conn.State != System.Data.ConnectionState.Open)
conn.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
yield return reader.ToDynamicRecord();
}
}
}
}
public void GetUsers()
{
Database db = CreateDatabase(); // do not type this as 'var' or else the extension method will not be invoked
var users = db.RawQuery("SELECT UserID, Name FROM Users");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment