Created
September 15, 2013 10:46
-
-
Save tugberkugurlu/6569633 to your computer and use it in GitHub Desktop.
Async Database Call Sample with Raw ADO.NET
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Data.SqlClient; | |
using System.Configuration; | |
using System.Data; | |
using System.Threading.Tasks; | |
namespace AsyncDatabaseCall.Models { | |
public class GalleryContext { | |
readonly string _spName = "sp$GetCars"; | |
readonly string _connectionString = | |
ConfigurationManager.ConnectionStrings["CarGalleryConnStr"].ConnectionString; | |
public IEnumerable<Car> GetCarsViaSP() { | |
using (var conn = new SqlConnection(_connectionString)) { | |
using (var cmd = new SqlCommand()) { | |
cmd.Connection = conn; | |
cmd.CommandText = _spName; | |
cmd.CommandType = CommandType.StoredProcedure; | |
conn.Open(); | |
using (var reader = cmd.ExecuteReader()) { | |
return reader.Select(r => carBuilder(r)).ToList(); | |
} | |
} | |
} | |
} | |
public async Task<IEnumerable<Car>> GetCarsViaSPAsync() { | |
using (var conn = new SqlConnection(_connectionString)) { | |
using (var cmd = new SqlCommand()) { | |
cmd.Connection = conn; | |
cmd.CommandText = _spName; | |
cmd.CommandType = CommandType.StoredProcedure; | |
conn.Open(); | |
using (var reader = await cmd.ExecuteReaderAsync()) { | |
return reader.Select(r => carBuilder(r)).ToList(); | |
} | |
} | |
} | |
} | |
//private helpers | |
private Car carBuilder(SqlDataReader reader) { | |
return new Car { | |
Id = int.Parse(reader["Id"].ToString()), | |
Make = reader["Make"] is DBNull ? null : reader["Make"].ToString(), | |
Model = reader["Model"] is DBNull ? null : reader["Model"].ToString(), | |
Year = int.Parse(reader["Year"].ToString()), | |
Price = float.Parse(reader["Price"].ToString()), | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment