// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Data; using System.Data.Common; using System.Collections.Generic; using System.Linq; using Dapper; using Dapper.Contrib.Extensions; namespace DAL { [Table("t1")] public class T1 { [ExplicitKey] public int F1 { get; set; } public string F2 { get; set; } public T1() : this(0, "") { } public T1(int f1, string f2) { F1 = f1; F2 = f2; } public override string ToString() { return string.Format("[{0},{1}]", F1, F2); } } public interface IDAL { T1 GetOneByPK(int f1); IList GetAll(); IList GetRangeOfPK(int f1_start, int f1_end); IList GetContainingF2(string f2); void SaveChanges(T1 o); void SaveNew(T1 o); void Remove(int f1); } } namespace DAL.DapperContrib { public class DapperDAL : IDAL { private string prov; private string constr; private IDbConnection GetConnection() { IDbConnection con = DbProviderFactories.GetFactory(prov).CreateConnection(); con.ConnectionString = constr; con.Open(); return con; } public DapperDAL(string prov, string constr) { this.prov = prov; this.constr = constr; } public T1 GetOneByPK(int f1) { using(IDbConnection con = GetConnection()) { return con.QueryFirst("SELECT f1,f2 FROM t1 WHERE f1 = @f1", new { f1 = f1 }); } } public IList GetAll() { using(IDbConnection con = GetConnection()) { return con.Query("SELECT f1,f2 FROM t1").ToList(); } } public IList GetRangeOfPK(int f1_start, int f1_end) { using(IDbConnection con = GetConnection()) { return con.Query("SELECT f1,f2 FROM t1 WHERE f1 BETWEEN @f1_start AND @f1_end", new { f1_start = f1_start, f1_end = f1_end }).ToList(); } } public IList GetContainingF2(string f2) { using(IDbConnection con = GetConnection()) { return con.Query("SELECT f1, f2 FROM t1 WHERE f2 LIKE @f2", new { f2 = "%" + f2 + "%" }).ToList(); } } public void SaveChanges(T1 o) { using(IDbConnection con = GetConnection()) { con.Update(o); } } public void SaveNew(T1 o) { using(IDbConnection con = GetConnection()) { con.Insert(o); } } public void Remove(int f1) { using(IDbConnection con = GetConnection()) { con.Delete(new T1(f1, "")); } } } } namespace Demo { using DAL; public class Program { public static void UpdateF2(IDAL dal, int f1, string f2) { T1 o = dal.GetOneByPK(f1); o.F2 = f2; dal.SaveChanges(o); } public static void Print(IList lst) { Console.Write("{"); for (int i = 0; i < lst.Count; i++) { if (i > 0) Console.Write(","); Console.Write(lst[i]); } Console.WriteLine("}"); } public static void Test(string prov, string constr) { IDAL dal = new DAL.DapperContrib.DapperDAL(prov, constr); Console.WriteLine(dal.GetOneByPK(2)); UpdateF2(dal, 2, "BBB"); Console.WriteLine(dal.GetOneByPK(2)); UpdateF2(dal, 2, "BB"); Console.WriteLine(dal.GetOneByPK(2)); Print(dal.GetAll()); Print(dal.GetRangeOfPK(2, 4)); Print(dal.GetContainingF2("B")); dal.SaveNew(new T1(999, "XXX")); Print(dal.GetAll()); dal.Remove(999); Print(dal.GetAll()); } public static void Main(string[] args) { Test("System.Data.SqlClient", @"Server=ARNEPC4\SQLEXPRESS;Integrated Security=true;Database=Test"); Test("MySql.Data.MySqlClient", "Server=localhost;Database=Test;User Id=root;Password="); } } }