// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.Common; namespace E { public class T1 { public int F1 { get; set; } public string F2 { get; set; } } public interface IDAL { T1 GetOneT1(int f1); IList GetAllT1(); IList GetFilterT1(int? f1, string f2); } public class DataReaderDAL : IDAL { private IDbConnection GetConnection() { DbProviderFactory dbf = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["Test"].ProviderName); IDbConnection con = dbf.CreateConnection(); con.ConnectionString = ConfigurationManager.ConnectionStrings["Test"].ConnectionString; con.Open(); return con; } public T1 GetOneT1(int f1) { using(IDbConnection con = GetConnection()) { string paramprefix = ConfigurationManager.AppSettings["ParamPrefix"];; IDbCommand sel = con.CreateCommand(); sel.CommandText = "SELECT f1,f2 FROM t1 WHERE f1 = " + paramprefix + "f1"; sel.Connection = con; IDbDataParameter p = sel.CreateParameter(); p.ParameterName = paramprefix + "f1"; p.DbType = DbType.Int32; p.Value = f1; sel.Parameters.Add(p); using(IDataReader rdr = sel.ExecuteReader()) { if(rdr.Read()) { return new T1 { F1 = (int)rdr["f1"], F2 = (string)rdr["f2"]}; } else { return null; } } } } public IList GetAllT1() { IList res = new List(); using(IDbConnection con = GetConnection()) { IDbCommand sel = con.CreateCommand(); sel.CommandText = "SELECT f1,f2 FROM t1"; sel.Connection = con; using(IDataReader rdr = sel.ExecuteReader()) { while(rdr.Read()) { res.Add(new T1 { F1 = (int)rdr["f1"], F2 = (string)rdr["f2"]}); } } } return res; } public IList GetFilterT1(int? f1, string f2) { IList res = new List(); using(IDbConnection con = GetConnection()) { string isnull = ConfigurationManager.AppSettings["ISNULL"]; string paramprefix = ConfigurationManager.AppSettings["ParamPrefix"];; IDbCommand sel = con.CreateCommand(); sel.CommandText = "SELECT f1,f2 FROM t1 WHERE f1=" + isnull + "(" + paramprefix + "f1,f1) AND f2 LIKE " + isnull + "(" + paramprefix + "f2, f2)"; sel.Connection = con; IDbDataParameter p1 = sel.CreateParameter(); p1.ParameterName = paramprefix + "f1"; p1.DbType = DbType.Int32; p1.Value = f1 != null ? (object)f1 : (object)DBNull.Value; sel.Parameters.Add(p1); IDbDataParameter p2 = sel.CreateParameter(); p2.ParameterName = paramprefix + "f2"; p2.DbType = DbType.String; p2.Value = f2 != null ? (object)("%" + f2 + "%") : (object)DBNull.Value; sel.Parameters.Add(p2); using(IDataReader rdr = sel.ExecuteReader()) { while(rdr.Read()) { res.Add(new T1 { F1 = (int)rdr["f1"], F2 = (string)rdr["f2"]}); } } } return res; } } public class Program { public static void Test(string dalimpl) { IDAL dal = (IDAL)Activator.CreateInstance(Type.GetType(dalimpl, true, false)); Console.WriteLine("One:"); T1 one = dal.GetOneT1(2); Console.WriteLine(one.F1 + " " + one.F2); Console.WriteLine("All:"); foreach(T1 all in dal.GetAllT1()) { Console.WriteLine(all.F1 + " " + all.F2); } Console.WriteLine("Filter A:"); foreach(T1 filta in dal.GetFilterT1(2, "B")) { Console.WriteLine(filta.F1 + " " + filta.F2); } Console.WriteLine("Filter B:"); foreach(T1 filtb in dal.GetFilterT1(2, null)) { Console.WriteLine(filtb.F1 + " " + filtb.F2); } Console.WriteLine("Filter C:"); foreach(T1 filtc in dal.GetFilterT1(null, "B")) { Console.WriteLine(filtc.F1 + " " + filtc.F2); } Console.WriteLine("Filter D:"); foreach(T1 filtd in dal.GetFilterT1(3, "B")) { Console.WriteLine(filtd.F1 + " " + filtd.F2); } Console.WriteLine("Filter E:"); foreach(T1 filte in dal.GetFilterT1(2, "C")) { Console.WriteLine(filte.F1 + " " + filte.F2); } } public static void Main(string[] args) { Test("E.DataReaderDAL"); } } }