C# Datareader包含看似随机的行数,而不考虑Select语句条件

C# Datareader包含看似随机的行数,而不考虑Select语句条件,c#,ado,oledbdatareader,C#,Ado,Oledbdatareader,我已经进行了广泛的搜索,但大多数Datareader问题/答案对都涉及越过第一行、不返回任何内容、使用Datareader获取单个值等。。没有什么能像我现在遇到的那样 很明显,这是我夜校的作业,虽然只是其中的一小部分 函数的大小为int;该表有两个colmun:col1和col2,其中col1将索引值保存为double,col2保存随机生成的double。表是工作簿中的excel工作表,不知道是否相关 表是使用ADO命令对象执行的insert语句填充的,没有问题 现在,datareader对象不

我已经进行了广泛的搜索,但大多数Datareader问题/答案对都涉及越过第一行、不返回任何内容、使用Datareader获取单个值等。。没有什么能像我现在遇到的那样

很明显,这是我夜校的作业,虽然只是其中的一小部分

函数的大小为int;该表有两个colmun:col1和col2,其中col1将索引值保存为double,col2保存随机生成的double。表是工作簿中的excel工作表,不知道是否相关

表是使用ADO命令对象执行的insert语句填充的,没有问题

现在,datareader对象不再向我提供查询中size/@size指定的行数(因为它在本例中扮演index/UID的双重角色),而是获取看似随机的行数。我这么说似乎是因为这个数字似乎是固定在“size”的值上的(例如,size=10->datareader包含3行在.executeReader()之后;size=2->datareader包含113行;size=5->446行)

在调试过程中,我一直跟踪查询的@size参数保持在10.0 我无法确定reader.Read()何时/为什么变为False

我还用一个文本(5.0)替换了查询字符串中的参数;这导致条件表达式异常中的类型不匹配。但都是双打,还是我错过了什么?!我猜这会是个好机会,但我现在不知所措

正如你可能猜到的那样,我对编程相当陌生,所以请容忍我

是什么导致我的代码以这种方式运行

private Double[] getSelection(int size, string table)
    {
        List<Double> list = new List<Double>();
        Double[] toSort;

        OleDbConnection connect = new OleDbConnection(cntstring);
        connect.Open();
        OleDbCommand command = connect.CreateCommand();
        command.CommandType = CommandType.Text;
        command.Parameters.Add("@size", OleDbType.Double).Value = Convert.ToDouble(size);
        command.CommandText = String.Format("SELECT * FROM [{0}$] WHERE col1 < @size;", table);

        try
        {
            OleDbDataReader reader = command.ExecuteReader();
            Double outputReader;
            while (reader.Read())             
            {                                 
                outputReader = Convert.ToDouble(reader.GetValue(1));  /for some reason (which is not my main concern at the moment) the reader.getDouble() method returned an invalid cast exception
                list.Add(outputReader);
            }

            toSort = new double[list.Count()];
            foreach (double d in list)
            {
                toSort[list.IndexOf(d)] = d;
            }
            string output = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", toSort[0], toSort[1], toSort[2], toSort[3], toSort[4], toSort[5], toSort[6], toSort[7], toSort[8], toSort[9]);
            //to check for values; the String.Format is where i first encountered the index out of bounds exception
            MessageBox.Show(output);
            reader.Close();
            reader.Dispose();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }
        finally
        {
            connect.Close();
            connect.Dispose();
            return toSort;
        }
    }
private Double[]getSelection(整数大小,字符串表)
{
列表=新列表();
双[]排序;
OleDbConnection connect=新的OleDbConnection(cntstring);
connect.Open();
OleDbCommand=connect.CreateCommand();
command.CommandType=CommandType.Text;
command.Parameters.Add(“@size”,OleDbType.Double).Value=Convert.ToDouble(size);
command.CommandText=String.Format(“从[{0}$]中选择*其中col1<@size;”,表格);
尝试
{
OleDbDataReader=command.ExecuteReader();
双输出放大器;
while(reader.Read())
{                                 
outputReader=Convert.ToDouble(reader.GetValue(1));/出于某种原因(这不是我目前主要关心的问题),reader.getDouble()方法返回了无效的强制转换异常
列表。添加(输出头);
}
toSort=newdouble[list.Count()];
foreach(列表中的双d)
{
toSort[list.IndexOf(d)]=d;
}
字符串输出=string.Format(“{0},{1},{2},{3},{4},{5},{6},{8},{9}”,toSort[0],toSort[1],toSort[2],toSort[3],toSort[4],toSort[5],toSort[6],toSort[7],toSort[8],toSort[9]);
//检查值;String.Format是我第一次遇到索引越界异常的地方
MessageBox.Show(输出);
reader.Close();
reader.Dispose();
}
捕获(例外e)
{
MessageBox.Show(e.Message);
}
最后
{
connect.Close();
connect.Dispose();
返回排序;
}
}

您是否在select语句中尝试了@size左右的单引号,即

'@size'

参数从不在引号内。这样它们就变成了文字字符串。你能显示你的连接字符串吗?我想检查一下你的IMEX值。那么,检查Excel列的格式是否真的是double而不是string呢?我了解到Excel默认情况下将数字存储为double,因此使用double类型。连接字符串:@“Provider=Microsoft.ACE.OLEDB.12.0;数据源=C:\Users******\Documents\Visual Studio 2013\Projects\prog3Les2EigenMakelijADO\prog3Les2EigenMakelijADO\EigenMakelij.xlsx;扩展属性='Excel 12.0 Xml';“没有可以看到的IMEX值。这是强制性的吗?这是通过OleDb读取Excel数据的痛点。同时:添加的HDR=是;IMEX=1到扩展属性和用单引号包围的路径>相同结果。或阅读此处