当前位置: 代码迷 >> Web Service >> MySqlDataReader 为啥一到while (reader.Read())就catch退出
  详细解决方案

MySqlDataReader 为啥一到while (reader.Read())就catch退出

热度:757   发布时间:2011-12-26 23:09:58.0
MySqlDataReader 为什么一到while (reader.Read())就catch退出?
在webService里的:
C# code

using (MySqlConnection conn = CreateConnection())
 {

     MySqlCommand cmd = new MySqlCommand(sql, conn);
     MySqlDataReader reader = cmd.ExecuteReader();
               
     while (reader.Read())
     {
        Depart d1 = new Depart();
        d1.Qsectionnaem = reader["qsectionname"].ToString();
        depart.Add(d1);
     }
}


一到这里就catch退出,报错是说conn must avalid andopen,我用using(conn){}应该不是这个问题吗?求解答
C# code

     while (reader.Read())
     {
        Depart d1 = new Depart();
        d1.Qsectionnaem = reader["qsectionname"].ToString();
        depart.Add(d1);
     }



------解决方案--------------------
using (MySqlConnection conn = CreateConnection())
 {
conn.Open();
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
Depart d1 = new Depart();
d1.Qsectionnaem = reader["qsectionname"].ToString();
depart.Add(d1);
}
}
  相关解决方案