MySql and inserting last ID problem remains

后端 未结 3 1174
南旧
南旧 2020-12-20 06:45

Ok after much reading and trying I still cant seem to get this to work:

      OdbcCommand cmd = new OdbcCommand(\"INSERT INTO User (Email) VALUES (\'rusty@ms         


        
相关标签:
3条回答
  • 2020-12-20 07:05

    A workaround:

    1. Add an out param to your query:
    2. inside the query use SET @OpParam = SELECT LAST_INSERT_ID();
    3. In your code get it as if ((int)@OpParam.Value > 0){ pull out }.
    4. Also, if run select then instead NonQuery, use ExecuteScalar().

    Hope it might help you.

    0 讨论(0)
  • 2020-12-20 07:07

    Clarification, the mySQL .net ODBC driver doesn't allow multiple commands to be run like you are describing. You have to make two separate calls and wrap them in a transaction.

    // 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.
                  }
             }
    }
    
    0 讨论(0)
  • 2020-12-20 07:25

    If your Email is unique you can use

    Select ID From User where Email='rusty@msn.com'
    

    you are sure that always you get the answer right.

    If your Email is not unique you should use lock to make sure that only on thread is executing query and then use this query after your insert

    Select Max(ID) From User
    
    0 讨论(0)
提交回复
热议问题