static void Main(string[] args) { SqlConnection cn = new SqlConnection(connString); SqlCommand cm = new SqlCommand(); Int32 newID = 0; Object o; cm.Connection = cn; cm.CommandType = CommandType.Text; cm.CommandText = "INSERT INTO miketest VALUES (1); SELECT SCOPE_IDENTITY()"; cn.Open(); newID = Convert.ToInt32(cm.ExecuteScalar()); cn.Close(); Console.WriteLine("New ID: {0}", newID); }The test table looks like this:
CREATE TABLE miketest ( pk int identity(1,1) ,c int)IMO, calling a stored procedure to do the insert and then return the new IDENTITY value is the better approach.