Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/01/2009, 06:30
JhonPierre
 
Fecha de Ingreso: julio-2007
Mensajes: 19
Antigüedad: 17 años, 7 meses
Puntos: 0
Respuesta: Recuperar ID de un Insert

Hola, Weno Lo mas Probable es que quieras Recuperar El ID generado por ese Insert. Si es Asi Hay muchas formas de hacerlo. Trabaja Con Una Transaccion Para Que se vea mejor, Aqui Te Deho Una Forma:
Código javascript:
Ver original
  1. ///.............
  2. using (SqlConnection connection = new SqlConnection(connectionString))
  3.     {
  4.         connection.Open();
  5.         SqlCommand command = connection.CreateCommand();
  6.         SqlTransaction transaction;
  7.         transaction = connection.BeginTransaction("Mi Trans");
  8.  
  9.         command.Connection = connection;
  10.         command.Transaction = transaction;
  11.  
  12.         try
  13.         {
  14.             //Haces el Insert a Tu Tabla
  15.             command.CommandText =
  16.                 "Insert miTabla(.....";
  17.             command.ExecuteNonQuery();
  18.             ///Luego Aqui Recuperas el Id Insertado
  19.             command.CommandText =
  20.                 "SELECT TOP 1 miTabla.Id from miTabla OrderBy miTabla.Id DESC";
  21.             string miId = command.ExecuteScalar().toString();
  22.             //Aqui Haces Lo q Desees con Tu "miID"
  23.            
  24.             transaction.Commit();
  25.            
  26.         }
  27.         catch (Exception ex)
  28.         {
  29.             try
  30.             {
  31.                 transaction.Rollback();
  32.             }
  33.             catch (Exception ex2)
  34.             {              
  35.             }
  36.         }
  37.         finally
  38.         {
  39.             connection.close();
  40.         }
  41.        
  42.     }
  43. ///..........

Weno y consigue Algun Buen Manual De "SQL" Que Te Ayude A programar Bien....
Bytes.
JHONPi.