Last active
August 29, 2015 14:07
-
-
Save tonyjoanes/3fe534e6f475b1690703 to your computer and use it in GitHub Desktop.
Simple Dapper query and mapping
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 class Person | |
{ | |
public int Id { get; set; } | |
public string FirstName { get; set; } | |
public string LastName { get; set; } | |
} | |
public class PersonService | |
{ | |
public Person GetPerson(int id) | |
{ | |
var connectionString = ConfigurationManager.ConnectionStrings["DefaultDbConnection"].ConnectionString; | |
Person person = null; | |
using (var connection = new SqlConnection(connectionString)) | |
{ | |
connection.Open(); | |
person = connection.Query<Person>("select * from person where id = @id", new { Id = 1 }).FirstOrDefault(); | |
} | |
return person; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment