C# mysql one return last_insert_id

Posted by Bernhard on Stack Overflow See other posts from Stack Overflow or by Bernhard
Published on 2012-04-01T11:21:15Z Indexed on 2012/04/01 11:29 UTC
Read the original article Hit count: 161

Filed under:
|
|
|

I am trying to create a method in which I can exequte mysql UPDATE, DELETE or INSERT query. The method must work when with an INSERT I ask or do not ask the last_insert_id(). Below is the code that I have at the moment:

public int executeUID(MySqlCommand msCommand)
{
  try
  {
    this.Open();

    msCommand.Connection = this.msCon;

    return int.Parse(msCommand.ExecuteScalar().ToString());
  }
  catch (MySqlException ex)
  {
    throw ex;
  }
  finally
  {
    this.Close();
  }
}

The problem with this is is that when I use an insert query that returns a last_insert_id() the method works greatly. But when the query doesn't return an last_insert_id() the method malfunctions. How can I get this method to work?

© Stack Overflow or respective owner

Related posts about c#

Related posts about mysql