Skip to content

Instantly share code, notes, and snippets.

@svick
Created April 23, 2012 18:09
Show Gist options
  • Save svick/2472782 to your computer and use it in GitHub Desktop.
Save svick/2472782 to your computer and use it in GitHub Desktop.
async and SQL
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