C# 无法在具有c的数据库中更新#
我无法更新我的数据库。我有一个名为Table2的表,其中有3列:time、strike和vol。请检查line语句中的注释。提前谢谢你的帮助C# 无法在具有c的数据库中更新#,c#,ms-access,sql-update,C#,Ms Access,Sql Update,我无法更新我的数据库。我有一个名为Table2的表,其中有3列:time、strike和vol。请检查line语句中的注释。提前谢谢你的帮助 VolLoc = Math.Sqrt(Math.Abs(VarianceLoc)); Console.WriteLine("Local Volatility at strike " + strike1_run + " and time " + time0_run + " is: " + VolLoc + "\n"); //
VolLoc = Math.Sqrt(Math.Abs(VarianceLoc));
Console.WriteLine("Local Volatility at strike " + strike1_run + " and time " + time0_run + " is: " + VolLoc + "\n"); // works perfectly at this point, I have a new value for my variable VolLoc
string StrCmd1 = "UPDATE Table2 SET (vol = @vol_value) WHERE ((time = @T0_value) AND (strike = @K1_value))"; // HERE is the problem, when I debug, the cursor steps on it normally but the database is not updated !!
OleDbCommand Cmd1 = new OleDbCommand(StrCmd1, MyConn);
Cmd1.Parameters.Add("@vol_value", OleDbType.VarChar);
Cmd1.Parameters["@vol_value"].Value = VolLoc.ToString();
Cmd1.Parameters.Add("@T0_value", OleDbType.VarChar);
Cmd1.Parameters["@T0_value"].Value = time0_run.ToString();
Cmd1.Parameters.Add("@K1_value", OleDbType.VarChar);
Cmd1.Parameters["@K1_value"].Value = strike1_run.ToString(); //the cursor steps on each of the line statements above, but the database is still not updated
您需要调用对象上的。请尝试添加
Cmd1.ExecuteNonQuery();
除了如other所述缺少对
ExecuteOnQuery
的调用之外,您的代码还有另一个错误,当您的代码到达ExecuteOnQuery方法时,该错误会自动显示出来
单词TIME
是MS Access Jet SQL中的保留关键字。您需要用方括号将其封装
[time]
所以,总结一下
string StrCmd1 = "UPDATE Table2 SET vol = @vol_value WHERE " +
"([time] = @T0_value AND strike = @K1_value)";
OleDbCommand Cmd1 = new OleDbCommand(StrCmd1, MyConn);
.......
cmd1.ExecuteNonQuery();
此外,所有参数都作为字符串值传递。您确定相应字段的数据类型(文本)相同吗?看起来您设置好了。。。。但是我在代码中没有看到执行查询的地方。在上面编写的所有代码之后添加这个。这是对DB实际执行查询的行。我不能在代码末尾添加它,它显示了写入时的错误:“只能将赋值、调用、递增、递减和新对象表达式用作语句”。。。。你是对的,我看到我没有执行查询,你知道如何执行查询吗?非常感谢,它现在可以工作了!括号之间的时间是个问题!顺便问一下,我们是否只执行带有update的“cmd1.ExecuteOnQuery();”?ExecuteOnQuery适用于插入、更新、删除的每个语句,也适用于每个语句,它以某种方式操纵数据库而不返回行集合(创建表等)。相反,ExecuteReader或ExecuteScalar用于从数据库返回数据的语句。