无法用c#更新数据库

本文关键字:更新 数据库 | 更新日期: 2023-09-27 18:11:40

我无法更新数据库。我有一个名为Table2的表格,其中有3列:时间,罢工和卷。请检查行语句中的评论。提前感谢您的帮助。

       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

无法用c#更新数据库

除了如other所述缺少对ExecuteNonQuery的调用之外,您的代码还有另一个错误,当您的代码到达ExecuteNonQuery方法时将显示它自己。

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

同样,所有参数都作为字符串值传递。您确定对应的字段具有相同的数据类型(文本)

您需要在OleDbCommand对象上调用Execute方法

尝试添加

    Cmd1.ExecuteNonQuery();