3

I know how I do this, I have actually done it before, but the following code HANGS (not throw an exception) in this update statement below, and I don't see a reason for it. Can anyone see why?

I don't think is important but id is the primary key of Person and belongs is a foreign key.

Before any sugest concurrence problem, I am the only person working on the tables. The creation of the command is so:

public static String upDatePersonBelonging(Int32 personId,Int32 groupId)
{
    String error;
    if ((error = openConnection()) != "")
        return error;

    OracleCommand command = 
      new OracleCommand("UPDATE person SET belongs = :belongs where id = :id ",
                         connection);

    addParameter(command, "belongs", OracleDbType.Int32, groupId);
    addParameter(command, "id", OracleDbType.Int32, personId);

    return runCommand(command);
}

The execution is so:

private static String runCommand(OracleCommand command)
{
    String error = "";

    try
    {
        command.ExecuteNonQuery(); // here it hangs
    }
    catch (Exception e)
    {
        error = e.Message;
    }
    finally
    {
        connection.Close();
    }

    return error;
}

The opening function is the following:

private static String openConnection()
{
    try
    {
        // create an open the connection          
        connection = new OracleConnection(_connStr);

        // open the connection
        connection.Open();
    }
    catch(Exception e)
    {
        return e.Message;
    }

    return "";
}

add parameter code:

private static void addParameter(OracleCommand command, String name, OracleDbType type, Object value)
{
    command.Parameters.Add(name, type);
    command.Parameters[command.Parameters.Count-1] = new OracleParameter(name, value);
}
ender.an27
  • 703
  • 1
  • 13
  • 35

1 Answers1

1

Code seems to be correct.

I have 3 ideas:

  1. Update can be very long operation if there are many rows in the table and simply you have to wait (you can do this query using some other code/tool and compare times).
  2. Show as addParameter method.
  3. Maybe some other process is working on this table and locks it.

EDIT

Your addParameter method is strange. First you add parameter and then create new one. I will try rather something like this (not tested - I have no possibility):

private static void addParameter(OracleCommand command, String name,
  OracleDbType type, Object value)
{
    OracleParameter p = new OracleParameter(name, value);
    p.DbType = type;
    command.Parameters.Add(p);
}
psur
  • 4,400
  • 26
  • 36
  • 1 and 3 are not and 2 I dont understand it – ender.an27 Apr 11 '13 at 11:38
  • HAHAHAHA the reason is because before I was using other Command not OrcleCommand, I was using OleDBCommand and work a lite bit different. The form is strange, but without parallelism works ;) – ender.an27 Apr 11 '13 at 12:06
  • @user1739342 Have it helped? – psur Apr 11 '13 at 12:10
  • I know it work with other updates sentences, and just for this one not. Any why you want I test it? – ender.an27 Apr 11 '13 at 12:19
  • @user1739342 I just wanted to know wheter my answer was solution to your problem, because from your comments I can't understand it. If it was an solution then you can mark my post as an answer. If no you can post your own solution - final code. – psur Apr 11 '13 at 12:27
  • mmmm... this is strange, I check it with again, just in case, if the all way of doing it, I mean how is in the question, and know dont hang. I just uncomented what was before. Sorry I have no idea what was the problem :/ – ender.an27 Apr 11 '13 at 13:35