我有这段代码
string[] GetFaculties()
{
string[] result = { };
SetConnection();
sql_con.Open();
sql_cmd = sql_con.CreateCommand();
sql_cmd.CommandText = "SELECT kyrname FROM tnames";
SQLiteDataReader reader = sql_cmd.ExecuteReader();
int i = 0;
foreach (DbDataRecord record in reader)
{
result[i] = record["kyrname"].ToString();
i++;
}
sql_con.Close();
return result;
}
返回我"没有当前行"对于foreach'的例外情况。我该如何解决?
答案 0 :(得分:1)
使用此:
var ordinal = reader.GetOrdinal("kyrname");
while (reader.Read())
{
result[i] = reader.GetString(ordinal);
i++;
}
read方法将SqlDataReader推进到下一条记录,并且有下一条记录。