Geeks With Blogs
Carmel's Blog Independent Software Professional

The following code enables you to retrieve identity after insert to table in the database:

            string connectionString = "Data Source=.;Initial Catalog=Northwind;Integrated Security=True";
            string commandText = "Insert into tblID (Name) values (N'name') SELECT SCOPE_IDENTITY()";
            object id;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
               using(SqlCommand command = new SqlCommand(commandText, connection))
               {
                    command.CommandType = CommandType.Text;
                               
                    connection.Open();
                    id = command.ExecuteScalar();
                    connection.Close();
                }
            }

Posted on Tuesday, November 11, 2008 6:20 AM | Back to top


Comments on this post: Retreiving Identity after insert to table

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Carmel Halabe | Powered by: GeeksWithBlogs.net