简体   繁体   English

如何执行更新查询?

[英]How to execute an update query?

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. 我知道我是怎么做到的,我之前已经实际做过,但是下面的代码HANGS(不会抛出异常)在下面的这个更新语句中,我没有看到它的原因。 Can anyone see why? 谁能明白为什么?

I don't think is important but id is the primary key of Person and belongs is a foreign key. 我认为不重要,但idPerson的主键, belongs外键。

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);
}

Code seems to be correct. 代码似乎是正确的。

I have 3 ideas: 我有3个想法:

  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. 显示为addParameter方法。
  3. Maybe some other process is working on this table and locks it. 也许其他一些进程在这个表上工作并锁定它。

EDIT 编辑

Your addParameter method is strange. 你的addParameter方法很奇怪。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM