我必须读取一个包含大约 100 列的 sql 表,然后检查每个单元格以验证数据是否不为空。使用 SQLReader 它会在第 70 列抛出一个 IndexOutOfBoundsOfArrway 异常。知道我如何实现我的目标吗? 附言我在 C# 中这样做
这是我的代码:
conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand( "select * from tableName where column1='" +
textBox1.Text+"'", conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
for (int i = 3; i < 100; i++)
{
if (reader[i].ToString() != "")
{
listBox2.Items.Add(reader[i].ToString());
}
}
}
编辑:我确信我的表有 100 列,但 reader.FieldCount 等于 70
请您参考如下方法:
DataReader 具有以下属性列 信息:
FieldCount - returns the number of columns in the result set.
使用它并首先检查您是否获得了 100 列。
