1
我有以下代码来获取表格数据,但是出现错误,我不知道自己在哪里出错。指数超出范围。必须是非负的,并且小于集合的大小
public List<ModelGetEmployeeList> GetEmployeeList()
{
List<ModelGetEmployeeList> empList = new List<ModelGetEmployeeList>();
DataTable dt = new DataTable();
string q = "select uid,fname,lname from nworksuser;";
MySqlCommand cmd = new MySqlCommand(q,conn);
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); ;
conn.Open();
adapter.Fill(dt);
conn.Close();
for (int i = 0; i < dt.Rows.Count; i++)
{
foreach (DataRow row in dt.Rows)
{
//Here I am getting following error
// Index was out of range. Must be non-negative and less than the size of the collection.
empList[i].uid = row["uid"].ToString();
empList[i].fname = row["fname"].ToString();
empList[i].lname = row["lname"].ToString();
}
}
return empList;
}
而且ModelGetEmployeeList类就像是这个 -
public class ModelGetEmployeeList
{
public string uid { get; set; }
public string fname { get; set; }
public string lname { get; set; }
}
您好@Kael,感谢纠正。工作得很好。 – Dipak
没问题@DipakAkhade – KaeL
无需在数据适配器处理它时打开和关闭连接 –