[英]Update statement working in SQL Developer but not in C#
I have a small update query which works in SQL Developer. 我有一个小的更新查询,可在SQL Developer中使用。
UPDATE people
SET months = 8
WHERE number = 599
Fairly straight forward. 非常坦率的。 And it works - this also works in C#.
而且有效-在C#中也有效。 The problem is the moment I want to use Parameters (which works on number but not on months) it will stop working.
问题是我想使用Parameters的那一刻(对数字有效但对月份无效)将停止工作。
I have this code in C#: 我在C#中有以下代码:
using (OracleConnection con = new OracleConnection(connectionString))
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE people " +
"SET months = :months " +
"WHERE number = :number";
command.Parameters.Add(":number", OracleDbType.Int32).Value = number;
command.Parameters.Add(":months", OracleDbType.Int32).Value = months;
command.ExecuteNonQuery();
}
They are both of type Number in oracle, and I've tried changing the OracleDbType to Decimal, and pretty much everything without success. 它们在oracle中都是Number类型,并且我尝试将OracleDbType更改为Decimal,并且几乎所有事情都没有成功。 The odd thing is, that the :number parameters works, but months doesn't get updated (it won't crash, it just doesn't update).
奇怪的是,:number参数可以工作,但是几个月不会更新(它不会崩溃,只是不会更新)。 However if i change the :months parameter, to a static value like 7 - it will work.
但是,如果我将:months参数更改为像7这样的静态值,它将起作用。
You should add parameters without leading ':' (look here ), try this: 您应添加参数时不要以“:”开头(请参见此处 ),请尝试以下操作:
using (OracleConnection con = new OracleConnection(connectionString))
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE people" +
"SET months = :months " +
"WHERE number = :number";
command.Parameters.Add("number", OracleDbType.Int32).Value = number;
command.Parameters.Add("months", OracleDbType.Int32).Value = months;
command.ExecuteNonQuery();
}
Also you are missing space after :months
in query. 另外
:months
在查询:months
之后,您缺少空间。
Alright, i found out why this wasn't working, it wasn't because of the colons (you can add colons in parameters without it being a problem): 好吧,我发现了为什么它不起作用的原因,不是因为冒号(您可以在参数中添加冒号而不会造成问题):
command.Parameters.Add(":months", OracleDbType.Int32).Value = months;
The problem was that i was adding the parameters in a different order than I was using them. 问题是我添加参数的顺序与使用它们的顺序不同。
So if in your SQL statement you are adding parameters in a specific order, you should follow that order when you add your OracleCommand.Parameters, like so: 因此,如果在SQL语句中以特定顺序添加参数,则在添加OracleCommand.Parameters时应遵循该顺序,如下所示:
using (OracleConnection con = new OracleConnection(connectionString))
{
con.Open();
OracleCommand command = con.CreateCommand();
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE people " +
"SET months = :months " +
"WHERE number = :number";
command.Parameters.Add(":months", OracleDbType.Int32).Value = months;
command.Parameters.Add(":number", OracleDbType.Int32).Value = number;
command.ExecuteNonQuery();
}
your command text should be 您的命令文本应为
command.CommandText = "UPDATE people SET months = :months WHERE number = :number";
Note the spaces i have added 注意我添加的空格
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.