我正在调用静态函数来替换.accdb文件中的值;找到oldOA的值,然后用newOA替换它。
public static bool updateOA(string oldOA, string newOA)
{
...
command.Parameters.AddWithValue("@oldOA", oldOA);
command.Parameters.AddWithValue("@newOA", newOA);
command.CommandText = "SELECT [OA_ID] FROM [Operating_Authority_Table] WHERE [OA_Name] = [@oldOA]";
long value = long.Parse(command.ExecuteScalar().ToString());
command.Parameters.AddWithValue("@OA_ID", value);
command.CommandText = "UPDATE [Operating_Authority_Table] SET [OA_Name] = [@newOA] WHERE [OA_ID] = " + value;
try
{
command.ExecuteNonQuery();
message.show(1, "", "true");
return true;
}
catch
{
message.show(1, "", "false");
return false;
}
}
最初我只是使用:
command.CommandText = "UPDATE [OA_Table] SET [OA_Name] = [@newOA] WHERE [OA_Name] = [@oldOA]";
但是我被告知我应该获取OA_ID,这是PK并从那里更新它,导致上面的代码,但是虽然函数仍然返回true,但数据库中的值仍然没有更新。
command.CommandText = "UPDATE [Operating_Authority_Table] SET [OA_Name] = @newOA WHERE [OA_Name] = @oldOA";
我已经更新到此语句,虽然它仍然返回true,但值不会更新。我是否犯了任何语法错误?
答案 0 :(得分:1)
update Operating_Authority_Table set OA_Name = @newOA where OA_ID = (select OA_ID from Operating_Authority_Table where OA_Name = @oldOA)
我猜这可以解决你的问题
答案 1 :(得分:0)
从参数中删除方括号后会发生什么?
也许还要将value
作为参数包含在内?
UPDATE [Operating_Authority_Table]
SET [OA_Name] = @newOA
WHERE [OA_ID] = @myValue;
command.Parameters.AddWithValue("@newOA", newOA);
command.Parameters.AddWithValue("@myValue", value);