// 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.Data; using System.Data.SqlClient; namespace E { public class SqlServer2 { public static void Main(string[] args) { SqlConnection con = new SqlConnection("server=ARNEPC3\\SQLEXPRESS;Integrated Security=SSPI;database=Test"); con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE j(id INTEGER NOT NULL, txt VARCHAR(50), PRIMARY KEY(id))", con); cre.ExecuteNonQuery(); SqlCommand ins = new SqlCommand("INSERT INTO j VALUES(@id, @txt)", con); ins.Parameters.Add("@id", SqlDbType.Int); ins.Parameters.Add("@txt", SqlDbType.VarChar, 50); for(int i = 0; i < 5; i++) { ins.Parameters["@id"].Value = i+1; ins.Parameters["@txt"].Value = "Test #" + (i+1); ins.ExecuteNonQuery(); } SqlCommand sel = new SqlCommand("SELECT id,txt FROM j WHERE id > @lim", con); sel.Parameters.Add("@lim", SqlDbType.Int); sel.Parameters["@lim"].Value = 3; SqlDataReader rdr = sel.ExecuteReader(); while(rdr.Read()) { int id = rdr.GetInt32(0); String txt = rdr.GetString(1); Console.WriteLine(id + " : " + txt); } rdr.Close(); SqlCommand drp = new SqlCommand("DROP TABLE j", con); drp.ExecuteNonQuery(); con.Close(); } } }