Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Masalah MySql dan memasukkan ID terakhir tetap ada

Klarifikasi, driver mySQL .net ODBC tidak mengizinkan banyak perintah dijalankan seperti yang Anda gambarkan. Anda harus membuat dua panggilan terpisah dan menggabungkannya dalam satu transaksi.

// open a new connection using a default connection string I have defined elsewhere
using( OdbcConnection connection = new OdbcConnection( s_connectionString ) )
{
      // ODBC command and transaction objects
      OdbcCommand command = new OdbcCommand();
      OdbcTransaction transaction = null;

      // tell the command to use our connection
      command.Connection = connection;

      try
      {
           // open the connection
           connection.Open();

           // start the transaction
           transaction = connection.BeginTransaction();

           // Assign transaction object for a pending local transaction.
           command.Connection = connection;
           command.Transaction = transaction;

           // TODO: Build a SQL INSERT statement
           StringBuilder SQL = new StringBuilder();

           // run the insert using a non query call
           command.CommandText = SQL.ToString();
           command.ExecuteNonQuery();

           /* now we want to make a second call to MYSQL to get the new index 
              value it created for the primary key.  This is called using scalar so it will
               return the value of the SQL  statement.  We convert that to an int for later use.*/
           command.CommandText = "select last_insert_id();";
           id = Convert.ToInt32( command.ExecuteScalar() );

           // Commit the transaction.
           transaction.Commit();
     }
     catch( Exception ex )
     {
          Debug.WriteLine( ex.Message );

          try
          {
               // Attempt to roll back the transaction.
               transaction.Rollback();
            }
            catch
            {
                 // Do nothing here; transaction is not active.
              }
         }
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Bagaimana mendapatkan nilai dari URL

  2. Tipe data MySQL apa yang harus digunakan untuk Lintang/Bujur dengan 8 tempat desimal?

  3. bagaimana saya bisa menjalankan perintah CMD di aplikasi konsol c #?

  4. UnsupportedOperationException dengan DriverManager.getConnection() di Android

  5. Format pilih MySql, kolom bulat