myCommand.Parameters.Add("@CategoryName", SqlDbType.NChar, 15);
myCommandParameters["@CategoryName"].Value = "New Category Name";
SqlParameter myOutputParam = new SqlParameter("@NewCategoryID", SqlDbType.Int);
myOutputParam.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(myOutputParam);
myCommand.ExecuteNonQuery();
Int32 rowCount = (Int32)myCommand.Parameters["@RowCount"].Value;
Int32 newID = (Int32)myCommand.Parameters["@NewCategoryID"].Value;
myTrans.Commit();
Console.WriteLine("Both records are written to database.");
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction.");
}
}
Console.WriteLine("An exception of type " + e.GetType() +
" was encountered while inserting the data.");
Console.WriteLine("Neither record was written to database.");
}
finally
{
myConnection.Close();
}