MySqlDataReader 为啥一到while (reader.Read())就catch退出
MySqlDataReader 为什么一到while (reader.Read())就catch退出?在webService里的:C# codeusing (MySqlConn
MySqlDataReader 为什么一到while (reader.Read())就catch退出?
在webService里的:
C# codeusing (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);
}
}