For k = 0 To ds2.Tables(0).Rows.Count - 1
                    tagnam = Trim(ds2.Tables(0).Rows(k)("tagnam"))  '循环表里的n个位号                    sqlstr10 = "select * from AVER_VALUE where TAGNAM = '" & tagnam & "' and (DRDATE between to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd') and to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd')) and PLANT='" & j & "' " 'or PART='" & j & "'
                    ds5 = GetDataFromOracle(sqlstr10)   '取k位号里的其他值
                    plant = ds5.Tables(0).Rows(k)("PLANT")
                    descr = ds5.Tables(0).Rows(k)("DESCRIBE")
                    part = ds5.Tables(0).Rows(k)("PART")                    sqlstr8 = "select distinct CLASSNAME from AVER_VALUE where TAGNAM = '" & tagnam & "' and (DRDATE between to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd') and to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd')) and PLANT='" & j & "' " 'or PART='" & j & "'
                    ds4 = GetDataFromOracle(sqlstr8)   '平均值表里不同的班组
                    p = ds4.Tables(0).Rows.Count
                    For h = 0 To ds4.Tables(0).Rows.Count - 1
                        classname = Trim(ds4.Tables(0).Rows(h)("CLASSNAME"))  '循环表里的n个班组
                        sqlstr6 = "select sum(AVER_VALUE) from AVER_VALUE where TAGNAM = '" & tagnam & "' and CLASSNAME = '" & classname & "' and (DRDATE between to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd') and to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd')) and PLANT='" & j & "' "
                        Dim cm2 As New OracleCommand(sqlstr6, conn)
                        sum = cm2.ExecuteScalar                        sqlstr7 = "select * from AVER_VALUE where TAGNAM = '" & tagnam & "' and CLASSNAME = '" & classname & "' and (DRDATE between to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd') and to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd')) and PLANT='" & j & "' "
                        ds3 = GetDataFromOracle(sqlstr7)  '某一位号一段时间内的n个值
                        count2 = ds3.Tables(0).Rows.Count
                        aver = sum / count2                        addstr = "insert into AVER_VALUE_LS values ('" & plant & "','" & descr & "','" & tagnam & "'," & aver & ",'" & part & "','" & classname & "',to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd'),to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd'))"
                        Oraclecommand(addstr)
                    Next
                Next在此位置处 plant = ds5.Tables(0).Rows(k)("PLANT")报异常详细信息: System.IndexOutOfRangeException: 在位置 9 处没有任何行。但是我的数据库里确实有值,不是每个时间段都报错,有的报、有的不报。各位有没有碰到过这种情况的,请回贴啊

解决方案 »

  1.   

    先说你的代码写的很乱......这么多sql语句,咋不用存储过程啊,代码也很不规范啊
      

  2.   

    应该是有的“plant”插入了null所以报错
      

  3.   

    你可以try catch 捕获异常啊,或者强制转换string 有null时也可以取个“”啊
      

  4.   

    For k = 0 To ds2.Tables(0).Rows.Count - 1 
                        tagnam = Trim(ds2.Tables(0).Rows(k)("tagnam"))  '循环表里的n个位号                     sqlstr10 = "select * from AVER_VALUE where TAGNAM = '" & tagnam & "' and (DRDATE between to_date('" & Trim(Tx_SDate.Text) & "','yyyy-mm-dd') and to_date('" & Trim(Tx_EDate.Text) & "','yyyy-mm-dd')) and PLANT='" & j & "' " 'or PART='" & j & "' 
                        ds5 = GetDataFromOracle(sqlstr10)  '取k位号里的其他值 
                        plant = ds5.Tables(0).Rows(k)("PLANT") 你用的控制变量k是ds2里的表0的数据行数,能代表ds5一定有ds2的表0的数据行数?
      

  5.   

    感谢各位热心的解答!特别是“木古口十”就是那个问题,两个数据集ds2和ds5的值,虽然是相同条件sql语句的,但我并不能确定它们一致。我把这个改了就好了。“冬风瘦马”说的也很对,我的sql是乱了点。存储过程怎么用?我理解存储过程是在数据库中建设的,我对oracle不怎么熟悉。请指教。