与此Connection关联的C#DataReader必须先关闭

时间:2017-03-14 04:54:06

标签: c# mysql

我正在尝试创建一个尝试连接和查询MySql数据库的c#应用程序。下面是我正在使用的代码

public static void ValidateName(MySqlConnection conn,List<Employee> EmpList, string Group)
 {
 string selectQuery = "Select Name from Employee where Group = @Group  AND @Name in (FirstName, LastName);";
 using (MySqlCommand cmd = new MySqlCommand(selectQuery, conn))
 cmd.Parameters.Add(new MySqlParameter("@Group", MySqlDbType.VarChar));
 cmd.Parameters.Add(new MySqlParameter("@Name", MySqlDbType.VarChar));
 for (int i = 0; i < EmpList.Count; i++)
 {
    cmd.Parameters["@Group"].Value = Group;
    cmd.Parameters["@Name"].Value = EmpList[i].Name;
    var reader = cmd.ExecuteReader();
    List<string> lineList = new List<string>();
    while (reader.Read())
    {
        lineList.Add(reader.GetString(0));
    }
    if (lineList.Count <=0)
    {
       WriteValidationFailure(EmpList[i], "Failed");
    }}

它现在在语句

中抛出错误
    var reader = cmd.ExecuteReader();

enter image description here

0 个答案:

没有答案