在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);
}
}