// 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.SqlClient; namespace Demo { public class Program { public static void UpdateF2(DataSet ds, int f1, string f2) { ds.Tables["t1"].Select("f1=" + f1)[0]["f2"] = f2; } public static void Print(DataRow[] drs) { if(drs.Length > 1) Console.Write("{"); bool first = true; foreach(DataRow dr in drs) { if(first) { first = false; } else { Console.Write(","); } Console.Write("[" + (int)dr["f1"] + "," + (string)dr["f2"] + "]"); } if(drs.Length > 1) Console.Write("}"); Console.WriteLine(); } public static void Print(DataSet ds) { Print(ds.Tables["t1"].Select()); } public static void Main(string[] args) { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC4\SQLEXPRESS;Integrated Security=true;Database=Test")) { SqlDataAdapter da = new SqlDataAdapter("SELECT f1,f2 FROM t1", con); DataSet ds = new DataSet(); da.Fill(ds, "t1"); new SqlCommandBuilder(da); Print(ds.Tables["t1"].Select("f1=2")); UpdateF2(ds, 2, "BBB"); Print(ds.Tables[0].Select("f1=2")); UpdateF2(ds, 2, "BB"); Print(ds.Tables[0].Select("f1=2")); Print(ds); Print(ds.Tables["t1"].Select("2 <= f1 AND f1 <= 4")); Print(ds.Tables["t1"].Select("f2 LIKE '%BB%'")); DataRow dr = ds.Tables["t1"].NewRow(); dr["f1"] = 999; dr["f2"] = "XXX"; ds.Tables["t1"].Rows.Add(dr); Print(ds); ds.Tables["t1"].Select("f1=999")[0].Delete(); Print(ds); da.Update(ds, "t1"); } } } }