将数据库中的数据传输到dataGridView中出现问题

大鱼啊大鱼 2016-11-01 11:15:06
dal层:
#region 查询所有数据
//查询所有数据
public List<PatientModel> SelectAll()
{
List<PatientModel> list = new List<PatientModel>();
string sql = "select count(*) from TBPatients";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, System.Data.CommandType.Text))
{
if (reader.HasRows)
{
while (reader.Read())
{
PatientModel model = new PatientModel();
model.PatientsID = reader.GetInt32(0);
model.BookTime = reader.GetDateTime(1);
model.UserID = reader.GetString(2);
model.Sex = reader.GetString(3);
model.Age = reader.GetInt32(4);
model.MedicalHis = reader.IsDBNull(5)?null:reader.GetString(5);
model.DrugAllergy = reader.IsDBNull(6) ? null : reader.GetString(6);
list.Add(model);
}
}
}

return list;
}
#endregion
BLL层: #region 查询所有数据
public List<PatientModel> SelectAll()
{
return dal.SelectAll();
}
#endregion
在ui层中,想要在dataGridView1中显示数据,使用dataGridView1的编辑列,出现错误。调试的时候,主页面上什么都没有。抛出异常:索引超出了数组界限。
...全文
138 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
大鱼啊大鱼 2016-11-04
  • 打赏
  • 举报
回复
引用 4 楼 Chinajiyong 的回复:
[quote=引用 3 楼 qq_35150804 的回复:] [quote=引用 2 楼 Chinajiyong 的回复:] 很明显是sql语句不对 string sql = "select count(*) from TBPatients"; 改成 string sql = "select * from TBPatients";(最好把字段写出来)
改成string sql = "select * from TBPatients"之后仍然没有用,而且还抛出异常:指定转换没有作用。但是将所有字段一一标出来之后就可以了。只是很疑惑为什么?[/quote] 自己调试看看哪里转换类型不对[/quote] 使用select * from表名的时候,要确保model类中字段的排序,dal层中使用的时候排序与表中列名的排序一致。这样就对啦~ 谢谢大家
EnForGrass 2016-11-02
  • 打赏
  • 举报
回复
很明显是sql语句不对 string sql = "select count(*) from TBPatients"; 改成 string sql = "select * from TBPatients";(最好把字段写出来)
Justin-Liu 2016-11-02
  • 打赏
  • 举报
回复
debug看看是哪个越界了
EnForGrass 2016-11-02
  • 打赏
  • 举报
回复
引用 3 楼 qq_35150804 的回复:
[quote=引用 2 楼 Chinajiyong 的回复:] 很明显是sql语句不对 string sql = "select count(*) from TBPatients"; 改成 string sql = "select * from TBPatients";(最好把字段写出来)
改成string sql = "select * from TBPatients"之后仍然没有用,而且还抛出异常:指定转换没有作用。但是将所有字段一一标出来之后就可以了。只是很疑惑为什么?[/quote] 自己调试看看哪里转换类型不对
大鱼啊大鱼 2016-11-02
  • 打赏
  • 举报
回复
引用 2 楼 Chinajiyong 的回复:
很明显是sql语句不对 string sql = "select count(*) from TBPatients"; 改成 string sql = "select * from TBPatients";(最好把字段写出来)
改成string sql = "select * from TBPatients"之后仍然没有用,而且还抛出异常:指定转换没有作用。但是将所有字段一一标出来之后就可以了。只是很疑惑为什么?

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧