// 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.Collections.Generic; using System.Data; using System.Data.SqlClient; namespace DAL { public interface IDAL { DataSet GetOneByPK(int f1); DataSet GetAll(); DataSet GetRangeOfPK(int f1_start, int f1_end); DataSet GetContainingF2(string f2); void Update(int f1, string f2); void Insert(int f1, string f2); void Delete(int f1); } public class DataSetDAL : IDAL { private string constr; public DataSetDAL(string constr) { this.constr = constr; } public DataSet GetOneByPK(int f1) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1 WHERE f1 = @f1", con); da.SelectCommand.Parameters.Add("@f1", SqlDbType.Int); da.SelectCommand.Parameters["@f1"].Value = f1; DataSet ds = new DataSet(); da.Fill(ds, "t1"); return ds; } } public DataSet GetAll() { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1", con); DataSet ds = new DataSet(); da.Fill(ds, "t1"); return ds; } } public DataSet GetRangeOfPK(int f1_start, int f1_end) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1 WHERE f1 BETWEEN @f1_start AND @f1_end", con); da.SelectCommand.Parameters.Add("@f1_start", SqlDbType.Int); da.SelectCommand.Parameters.Add("@f1_end", SqlDbType.Int); da.SelectCommand.Parameters["@f1_start"].Value = f1_start; da.SelectCommand.Parameters["@f1_end"].Value = f1_end; DataSet ds = new DataSet(); da.Fill(ds, "t1"); return ds; } } public DataSet GetContainingF2(string f2) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1 WHERE f2 LIKE @f2", con); da.SelectCommand.Parameters.Add("@f2", SqlDbType.VarChar, 50); da.SelectCommand.Parameters["@f2"].Value = "%" + f2 + "%"; DataSet ds = new DataSet(); da.Fill(ds, "t1"); return ds; } } public void Update(int f1, string f2) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1 WHERE f1 = @f1", con); da.SelectCommand.Parameters.Add("@f1", SqlDbType.Int); da.SelectCommand.Parameters["@f1"].Value = f1; DataSet ds = new DataSet(); da.Fill(ds, "t1"); new SqlCommandBuilder(da); ds.Tables["t1"].Rows[0]["f2"] = f2; da.Update(ds, "t1"); } } public void Insert(int f1, string f2) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1", con); DataSet ds = new DataSet(); da.Fill(ds, "t1"); new SqlCommandBuilder(da); DataRow dr = ds.Tables["t1"].NewRow(); dr["f1"] = f1; dr["f2"] = f2; ds.Tables["t1"].Rows.Add(dr); da.Update(ds, "t1"); } } public void Delete(int f1) { using(SqlConnection con = new SqlConnection(constr)) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1 WHERE f1 = @f1", con); da.SelectCommand.Parameters.Add("@f1", SqlDbType.Int); da.SelectCommand.Parameters["@f1"].Value = f1; DataSet ds = new DataSet(); da.Fill(ds, "t1"); new SqlCommandBuilder(da); ds.Tables["t1"].Rows[0].Delete(); da.Update(ds, "t1"); } } } } namespace Demo { using DAL; public class Program { public static void UpdateF2(IDAL dal, int f1, string f2) { dal.Update(f1, f2); } public static void Print(DataSet ds) { if(ds.Tables["t1"].Rows.Count > 1) Console.Write("{"); bool first = true; foreach(DataRow dr in ds.Tables["t1"].Rows) { if(first) { first = false; } else { Console.Write(","); } Console.Write("[" + (int)dr["f1"] + "," + (string)dr["f2"] + "]"); } if(ds.Tables["t1"].Rows.Count > 1) Console.Write("}"); Console.WriteLine(); } public static void Main(string[] args) { IDAL dal = new DAL.DataSetDAL(@"Server=ARNEPC4\SQLEXPRESS;Integrated Security=true;Database=Test"); Print(dal.GetOneByPK(2)); UpdateF2(dal, 2, "BBB"); Print(dal.GetOneByPK(2)); UpdateF2(dal, 2, "BB"); Print(dal.GetOneByPK(2)); Print(dal.GetAll()); Print(dal.GetRangeOfPK(2, 4)); Print(dal.GetContainingF2("B")); dal.Insert(999, "XXX"); Print(dal.GetAll()); dal.Delete(999); Print(dal.GetAll()); } } }