NDP - ADO.NET 2.0
Autogenerated keys and the dataset

 

UpdateRowSource value Description
Both Both the output parameters and the first returned row are mapped to the changed row in the DataSet.
FirstReturnedRecord The data in the first returned row is mapped to the changed row in the DataSet.
None Any returned parameters or rows are ignored.
OutputParameters Output parameters are mapped to the changed row in the DataSet.

 

// Assumes that connection is a valid SqlConnection object.
SqlDataAdapter adapter = new SqlDataAdapter(
  "SELECT CategoryID, CategoryName FROM dbo.Categories", connection);

adapter.InsertCommand = new SqlCommand("InsertCategory", connection);
adapter.InsertCommand.CommandType = CommandType.StoredProcedure;

adapter.InsertCommand.Parameters.Add(
  "@CategoryName", SqlDbType.NChar, 15, "CategoryName");

SqlParameter parameter = adapter.InsertCommand.Parameters.Add(
  "@Identity", SqlDbType.Int, 0, "CategoryID");
parameter.Direction = ParameterDirection.Output;

connection.Open();

DataSet categories = new DataSet();
adapter.Fill(categories, "Categories");

DataRow newRow = categories.Tables["Categories"].NewRow();
newRow["CategoryName"] = "New Category";
categories.Tables["Categories"].Rows.Add(newRow);

adapter.Update(categories, "Categories");

connection.Close();