我搜索的范围很广,但是大多数Datareader问题/答案对都涉及到第一行,没有返回任何内容,使用datareader等获取单个值。没有什么比我现在遇到的更好。< / p>
要说清楚,这是我晚班的作业,虽然只是其中的一小部分。
该函数将size作为int;该表有两个colmuns:col1和col2,其中col1将索引值保存为double,col2保存随机生成的double。 table是工作簿中的excel工作表,不知道是否相关。
表已使用ADO命令对象执行的insert语句填充而没有问题。
现在,不是向我提供查询中size / @ size指定的行数(因为它在这种情况下扮演索引/ UID的双重角色),而是datareader对象获取看似随机的行数。我说貌似,因为数字似乎固定为“大小”的值(例如,size = 10 - &gt; datareader包含3行.executeReader(); size = 2 - &gt; datareader包含113行;大小= 5 - > 446行。
在调试时我跟踪查询的@size参数仍为10.0 我不能把手指放在何时/为什么reader.Read()变为假。
我还用一个文字(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;
}
}
答案 0 :(得分:0)
您是否在select语句中尝试围绕@size的单引号,即
'@size'