我正在尝试这个
string query = "SELECT * FROM teams ORDER BY name";
using(MySqlConnection dbConn = new MySqlConnection(conn))
{
MySqlCommand cmd = new MySqlCommand(query, dbConn);
MySqlDataReader dataReader = cmd.ExecuteReader();
但是它在MysqlCommand行上返回一个错误,说Connection必须有效并且打开。任何人都知道我做错了什么?
答案 0 :(得分:10)
您尚未在代码中打开连接,请致电
dbConn.Open();
它与using
语句无关。
string query = "SELECT * FROM teams ORDER BY name";
using(MySqlConnection dbConn = new MySqlConnection(conn))
{
MySqlCommand cmd = new MySqlCommand(query, dbConn);
dbConn.Open();//here **
MySqlDataReader dataReader = cmd.ExecuteReader();
using
语句仅确保您的连接对象将在范围之后处置,它不会打开连接本身。