在代码的一部分中,我使用DataTable和DataTableReader从SQLite数据库获取信息并将其添加到列表中。当程序到达reader.GetValue行时,程序将引发ArgumentOutOfRangeException。据我所知,没有任何理由会发生这种情况。
DataTable dt = db.GetDataTable(Program.CONN, "SELECT ID FROM table WHERE column LIKE 'False%'");
using (DataTableReader dtr = dt.CreateDataReader())
{
while (dtr.Read())
{
int rt = 0;
foreach (DataRow dr in dt.Rows)
{
string line = dtr.GetValue(rt).ToString();//Arguement out of range exception being thrown here
idList.Add(line);
rt++;
}
}
}
最佳答案
您正在遍历行,而不是列。你应该做 :
DataTable dt = db.GetDataTable(Program.CONN, "SELECT ID FROM table WHERE column LIKE 'False%'");
int count = dt.Columns.Count;
using (DataTableReader dtr = dt.CreateDataReader())
{
while (dtr.Read())
{
for (int rt = 0 ; rt < count ; rt ++)
{
string line = dtr.GetValue(rt).ToString();
idList.Add(line);
}
}
}
要么 :
DataTable dt = db.GetDataTable(Program.CONN, "SELECT ID FROM table WHERE column LIKE 'False%'");
using (DataTableReader dtr = dt.CreateDataReader())
{
while (dtr.Read())
{
foreach (DataColumn col in dt.Columns)
{
string line = dtr[col.ColumnName].ToString();
idList.Add(line);
}
}
}