Created
April 23, 2012 18:09
-
-
Save svick/2472782 to your computer and use it in GitHub Desktop.
async and SQL
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
using System; | |
using System.Data.SqlClient; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
ReadData().Wait(); | |
} | |
static async Task ReadData() | |
{ | |
using (var conn = new SqlConnection("Data Source=(local);Initial Catalog=Dusty;Integrated Security=SSPI;")) | |
using (var cmd = new SqlCommand()) | |
{ | |
cmd.Connection = conn; | |
cmd.CommandText = "SELECT TOP 10 title FROM dbo.articles"; | |
conn.Open(); | |
var rdr = await Task.Factory.FromAsync( | |
cmd.BeginExecuteReader, (Func<IAsyncResult, SqlDataReader>)cmd.EndExecuteReader, null); | |
while (rdr.Read()) | |
Console.WriteLine(rdr[0]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment