Created
January 21, 2012 16:37
-
-
Save jasondentler/1653230 to your computer and use it in GitHub Desktop.
Simple.Data.RawSql blog code
This file contains hidden or 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
const string sql = @" | |
-- Data Query | |
SELECT TOP 5 Album.AlbumId, Artist.Name, Album.Title | |
FROM Artist | |
INNER JOIN Album | |
ON Album.ArtistId = Artist.ArtistId | |
WHERE Album.Title LIKE @filter | |
ORDER BY Album.Title, Artist.Name; | |
-- Count Query | |
SELECT COUNT(*) AS [Count] | |
FROM Artist | |
INNER JOIN Album | |
ON Album.ArtistId = Artist.ArtistId | |
WHERE Album.Title LIKE @filter"; | |
Database db = Database.Open(); | |
var data = db.ToResultSets(sql, new { filter = "a%" }).ToArray(); | |
var rows = data.ElementAt(0).ToArray(); | |
var count = (long)data.ElementAt(1).Single().Count; | |
Console.WriteLine("Displaying rows {0} through {1} of {2}", rows.Any() ? 1 : 0, rows.Length, count); | |
Console.WriteLine(); | |
foreach (var row in rows) | |
{ | |
Console.WriteLine("{0} by {1}", row.Title, row.Name); | |
} |
This file contains hidden or 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
const string sql = @" | |
-- Data Query | |
SELECT TOP 5 Album.AlbumId, Artist.Name, Album.Title | |
FROM Artist | |
INNER JOIN Album | |
ON Album.ArtistId = Artist.ArtistId | |
WHERE Album.Title LIKE @filter | |
ORDER BY Album.Title, Artist.Name"; | |
Database db = Database.Open(); | |
var rows = db.ToRows(sql, new {filter = "a%"}).ToArray(); | |
Console.WriteLine("Displaying rows {0} through {1}", rows.Any() ? 1 : 0, rows.Length); | |
Console.WriteLine(); | |
foreach (var row in rows) | |
{ | |
Console.WriteLine("{0} by {1}", row.Title, row.Name); | |
} |
This file contains hidden or 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
const string sql = @" | |
-- Data Query | |
SELECT TOP 1 Album.AlbumId, Artist.Name, Album.Title | |
FROM Artist | |
INNER JOIN Album | |
ON Album.ArtistId = Artist.ArtistId | |
WHERE Album.Title LIKE @filter | |
ORDER BY Album.Title, Artist.Name"; | |
Database db = Database.Open(); | |
var row = db.ToRow(sql, new {filter = "a%"}); | |
Console.WriteLine("Displaying a single row"); | |
Console.WriteLine(); | |
Console.WriteLine("{0} by {1}", row.Title, row.Name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment