0

请检查代码中间的注释,它显示问题出在哪里以及我如何尝试解决它......谢谢!

问题出在语句中:“while ((ObjReader.Read()) && (counter <= 5))”。我放了一个断点,但光标只是不想进入while循环!我试图在该语句的上方编写:“ObjReader.Read()”,它可以正常读取它,但它不想在 while 循环中读取它......

    public static void Convert(OleDbConnection MyConn, double strike1_run, double time0_run)
    {

        string value1 = "";
        string value2 = "";
        string value3 = "";
        string T0 = "";
        string T1 = "";
        string VolT0K1 = "";
        string VolT1K1 = "";
        string K0 = "";
        string K1 = "";
        string K2 = "";
        double DwOverDt;
        string VolT0K0 = "";
        string VolT0K2 = "";
        double DwOverDy=0; 
        double DwSqOverDySq = 0;
        double YoverW = 0;
        double VarianceLoc = 0;
        double VolLoc = 0;

        // create command
        string StrCmd = "SELECT * FROM Table1 WHERE ((strike = @strike_value0 AND strike = @strike_value1 AND strike = @strike_value2) AND (time= @time_value0 AND time= @time_value1)) ";

        OleDbCommand Cmd = new OleDbCommand(StrCmd, MyConn);

        Cmd.Parameters.Add("@strike_value0", OleDbType.VarChar);
        Cmd.Parameters["@strike_value0"].Value = (strike1_run - 0.01).ToString();

        Cmd.Parameters.Add("@strike_value1", OleDbType.VarChar);
        Cmd.Parameters["@strike_value1"].Value = strike1_run.ToString();

        Cmd.Parameters.Add("@strike_value2", OleDbType.VarChar);
        Cmd.Parameters["@strike_value2"].Value = (strike1_run + 0.01).ToString();

        Cmd.Parameters.Add("@time_value0", OleDbType.VarChar);
        Cmd.Parameters["@time_value0"].Value = time0_run.ToString();

        Cmd.Parameters.Add("@time_value1", OleDbType.VarChar);
        Cmd.Parameters["@time_value1"].Value = (time0_run + 1).ToString();

        OleDbDataReader ObjReader = Cmd.ExecuteReader(); // I put a breakpoint here - no issue

        if (ObjReader != null) // I put a breakpoint here - no issue
        {
            int counter = 0;
            while ((ObjReader.Read()) && (counter <= 5)) //HERE is the problem: I put a breakpoint here, the cursor just doesn't want to enter the while loop !! I have tried to write just above that statement: "ObjReader.Read()" on its own and it reads it normally, but it doesn't want to read it in the while loop ...
            {
                value1 = ObjReader.GetValue(1).ToString(); //time column value
                value2 = ObjReader.GetValue(2).ToString(); //strike column value
                value3 = ObjReader.GetValue(3).ToString(); //vol column value
                counter++;
                Console.WriteLine("counter= " + counter);

                switch (counter)
                {
                    case 1:
                        K0 = value2;
                        VolT0K0 = value3;
                        break;
                    case 2:
                        T0 = value1;
                        K1 = value2;
                        VolT0K1 = value3;
                        break;
                    case 3:
                        K2 = value2;
                        VolT0K2 = value3;
                        break;
                    case 4:
                        break;
                    case 5:
                        T1 = value1;
                        VolT1K1 = value3;
                        Console.WriteLine("Time: " + T0 + " - " + T1 + "\n" + "Strike: " + K0 + " - " + K1 + " - " + K2 + "\n" + "Vol: " + VolT0K1 + " - " + VolT1K1);
                        break;
                    default:
                        break;
                }
            }
        }
        ObjReader.Close();
    }
4

1 回答 1

1

可能您的查询返回零行。这将导致 Read() 返回 false。在处理时间间隔时,很容易将其倒退并获得零行。问题是您有两个不同的时间值,并且您将它们与同一列进行比较。一列永远不能同时等于两个不同的值!所以,你得到了零行。此外,尽量避免在查询中使用 * - 仅列出您需要的列并节省网络开销。

您的查询可能需要是 RANGE 查询,而不是等于...

string StrCmd = "SELECT time, strike, vol FROM Table1 WHERE ((strike = @strike_value0 AND strike = @strike_value1 AND strike = @strike_value2) AND (time >= @time_value0 AND time <= @time_value1)) ";

请注意,您对“strike”列执行相同的操作 - 要求它同时等于两个不同的值 - 如果这两个值不同,您将始终返回零行。这类似于我们说“where 1=0”强制零行。

于 2013-04-30T18:29:30.520 回答