dapper dot net
•Dapper.NETとも書かれる
– .NET向けのMicro-ORM
• オープンソース
– ApachとMITのデュアルライセンス
• サイトはこの辺
– https://code.google.com/p/dapper-dot-net
– https://github.com/SamSaffron/dapper-dot-
net
41.
dapper dot net
•作った人: Sam Saffron
– Stack Overflowの中の人
• 早い
– 生SQL実行(DataReader)とほぼ互角
– Stack Overflowのパフォーマンス改善のため
に作られた(らしい)
42.
dapper dot net
•どのDBで使える?
– ADO.NET準拠のプロバイダが使えるなら大抵
いけるんでは?
• IDbConnectionの実装なら、多分
• 確認した範囲
– SQL Server / SQL Server CE
– Oracle
– MySQL
– postgreSQL
ADO.NETの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var sql = "select ID, Name , Age , Email From Employee;";
var cmd = new SqlCeCommand(sql, cn);
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
dr["ID"],
dr["Name"],
dr["Age"],
dr["Email"]);
}
}
}
50.
dapperの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var sql = "select ID, Name , Age , Email From Employee;";
var result = cn.Query(sql);
foreach (var d in result)
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
d.ID,
d.Name,
d.Age,
d.Email);
}
}
ADO.NETの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var cmd = new SqlCeCommand("select ID, Name , Age , Email From Employee;", cn);
var result = new List<EmployeeEntity>();
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
result.Add(new EmployeeEntity()
{
ID = (int)dr["ID"],
Name = (string)dr["Name"],
Age = (int)dr["Age"],
Email = (string)dr["Email"]
});
}
}
foreach (var d in result)
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
d.ID,
d.Name,
d.Age,
d.Email);
}
}
56.
dapperの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var sql = "select ID, Name , Age , Email From Employee;";
var result = cn.Query<EmployeeEntity>(sql);
foreach (var d in result)
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
d.ID,
d.Name,
d.Age,
d.Email);
}
}
ADO.NETの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var cmd = new SqlCeCommand("select ID, Name , Age , Email From Employee where Age = @age;", cn);
var param = cmd.CreateParameter();
param.SqlDbType = System.Data.SqlDbType.Int;
param.Direction = System.Data.ParameterDirection.Input;
param.ParameterName = "age";
param.Value = 25;
cmd.Parameters.Add(param);
var result = new List<EmployeeEntity>();
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
result.Add(new EmployeeEntity()
{
ID = (int)dr["ID"],
Name = (string)dr["Name"],
Age = (int)dr["Age"],
Email = (string)dr["Email"]
});
}
}
foreach (var d in result)
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
d.ID,
d.Name,
d.Age,
d.Email);
}
62.
dapperの場合
using (var cn= new SqlCeConnection(connectionString))
{
cn.Open();
var sql = "select ID, Name , Age , Email From Employee where Age = @Age;";
var result = cn.Query<EmployeeEntity>(sql, new { Age = 25 });
foreach (var d in result)
{
Console.WriteLine("ID:{0} , Name:{1} , Age:{2} , Email:{3}",
d.ID,
d.Name,
d.Age,
d.Email);
}
}