Domanda

I am trying to execute an INSERT query in a mysql DB, but it doesn't happen anything except that the code executions stops and nothing gets inserted. Here is the code (the connection is made at another point and is working):

            query = string.Format("INSERT INTO users (username, settings) VALUES('{0}', '{1}')", userName, sw.ToString());
            myCommand = new MySqlCommand();
            myCommand.CommandText = query;
            myCommand.Connection = con;
            myCommand.ExecuteNonQuery();

If I step the code, it stops after executenonquery, so obvisously something is wrong there. God I hate that it doesn't throw an error at me :(

È stato utile?

Soluzione

Have you checked the connection is actually open and tried executing the method by assigning the result to an int in a try catch block.

int result;

try 
{
    if (conn.State != ConnectionState.Open)
                    conn.Open();

    result = Convert.ToInt32(dbComm.ExecuteNonQuery());
 }
 catch (Exception ex)
 {
    logger.Error(ex);
 }
 finally
 {
     if (conn.State != ConnectionState.Closed)
                    conn.Close();
 }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top