Skip to content

Instantly share code, notes, and snippets.

@taogawa
Last active December 10, 2015 11:29
Show Gist options
  • Save taogawa/4427965 to your computer and use it in GitHub Desktop.
Save taogawa/4427965 to your computer and use it in GitHub Desktop.
Dapper&PetaPoco
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());
}
}
}
}
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