// 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 Program { public static void Main(string[] args) { using(SqlConnection con = new SqlConnection(@"Server=ARNEPC3\SQLEXPRESS;Integrated Security=SSPI;Database=Test")) { con.Open(); SqlCommand cre = new SqlCommand("CREATE TABLE autofun (id INTEGER IDENTITY PRIMARY KEY, txt VARCHAR(50))", con); cre.ExecuteNonQuery(); SqlCommand ins = new SqlCommand("INSERT INTO autofun(txt) VALUES(@txt)", con); ins.Parameters.Add("@txt", SqlDbType.VarChar, 50); SqlCommand sel = new SqlCommand("SELECT @@IDENTITY", con); for(int i = 0; i < 10; i++) { ins.Parameters["@txt"].Value = "Text #" + i; ins.ExecuteNonQuery(); int autoid = (int)(decimal)sel.ExecuteScalar(); Console.WriteLine(autoid); } SqlCommand drp = new SqlCommand("DROP TABLE autofun", con); drp.ExecuteNonQuery(); } } } }