Created
October 17, 2011 16:12
-
-
Save kppullin/1292976 to your computer and use it in GitHub Desktop.
SimpleData Raw Query Extension Method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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