[英]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. 我认为不重要,但id
是Person
的主键, 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个想法:
addParameter
method. 显示为addParameter
方法。 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.