Last active
December 10, 2015 11:29
-
-
Save taogawa/4427965 to your computer and use it in GitHub Desktop.
Dapper&PetaPoco
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.Collections.Generic; | |
using System.Data.SqlClient; | |
using System.Diagnostics; | |
using Dapper; | |
namespace Foo | |
{ | |
// Dapper.Rainbowでは主キーの列名はIdが前提になっている | |
public class User | |
{ | |
public int? Id { get; set; } | |
public string UserName { get; set; } | |
public string Email { get; set; } | |
} | |
public class FooDatabase : Database<FooDatabase> | |
{ | |
public Table<User> Users { get; set; } | |
} | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
IEnumerable<User> users; | |
using (var cnn = new SqlConnection(@"ConnectionString")) | |
{ | |
cnn.Open(); | |
var db = FooDatabase.Init(cnn, 10); | |
users = db.Users.All(); | |
} | |
foreach (var u in users) | |
{ | |
Console.WriteLine(u.UserName); | |
Console.ReadLine(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.WriteLine(e.ToString()); | |
} | |
} | |
} | |
} |
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.Diagnostics; | |
using PetaPoco; | |
namespace Foo | |
{ | |
public class User | |
{ | |
public int? Id { get; set; } | |
public string UserName { get; set; } | |
public string Email { get; set; } | |
} | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
var db = new Database(@"ConnectionString", "ProviderName"); | |
var sql = Sql.Builder | |
.Select("*").From("Users"); | |
var users = db.Fetch<User>(sql); | |
foreach (var u in users) | |
{ | |
Console.WriteLine(u.UserName); | |
Console.ReadLine(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.WriteLine(e.ToString()); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment