-2

我无法更新我的数据库。我有一个名为 Table2 的表,其中包含 3 列:时间、罢工和 vol。请检查行语句中的注释。在此先感谢您的帮助。

       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
4

3 回答 3

2

Apart from the missing call to ExecuteNonQuery as stated by other, your code has another error that will show itself when your code will reach the ExecuteNonQuery method.

The word TIME is a reserved keyword in MS-Access Jet SQL.
You need to encapsulate it with square brackets [time]

So, summarizing

   string StrCmd1 = "UPDATE Table2 SET vol = @vol_value WHERE " + 
                    "([time] = @T0_value  AND strike = @K1_value)"; 

    OleDbCommand Cmd1 = new OleDbCommand(StrCmd1, MyConn);
    .......
    cmd1.ExecuteNonQuery();

Also, all the parameters are passed as string values. Are you sure that the corresponding fields are of the same datatype (text)

于 2013-04-30T21:30:17.480 回答
1

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

于 2013-04-30T21:21:08.367 回答
1

尝试添加

    Cmd1.ExecuteNonQuery();
于 2013-04-30T21:24:27.020 回答