2014-01-19 2 views
0

У меня возникла проблема с MySQL в программе. Я не профессионал в этом. сообщение
Ошибка MySQL в структуре C#

Ошибка:

«MySql.Data.MySqlClient.MySqlException: Существует уже открытая DataReader, связанные с этим соединением, которое должно быть закрыто первым на System.Void MySql.Data. .MySqlClient.MySqlCommand.CheckState() на MySqlDataReader MySql.Data.MySqlClient.MySqlCommand.ExecuteReader (поведение System.Data.CommandBehavior) на MySqlDataReader MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()»

код ниже:

public static class MySQL 
{ 
    private static string strConnection; 

    static void dbConnection_StateChange(object sender, StateChangeEventArgs ev) 
    { 
     if (ev.CurrentState == ConnectionState.Broken) 
     { 
      System.Threading.Thread.Sleep(1000); 
      dbConnection.Close(); 
     } 

     if (ev.CurrentState == ConnectionState.Closed) 
     { 
      System.Threading.Thread.Sleep(1000); 

      dbConnection = new MySqlConnection(strConnection); 
      dbConnection.StateChange += new System.Data.StateChangeEventHandler(dbConnection_StateChange); 
      System.Threading.Thread.Sleep(1000); 
      dbConnection.Open(); 
     } 
    } 

    private static MySqlConnection dbConnection; 
    public static bool openConnection(string dbHost, int dbPort, string dbName, string dbUsername, string dbPassword) 
    { 
     try 
     { 
      strConnection = "server=" + dbHost + ";" + "database=" + dbName + ";" + "uid=" + dbUsername + ";" + "password=" + dbPassword; 
      dbConnection = new MySqlConnection(strConnection); 
      dbConnection.StateChange += new System.Data.StateChangeEventHandler(dbConnection_StateChange); 
      dbConnection.Open(); 
      Console.WriteLine(" Connected to MySQL."); 
      return true; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(" Couldn't connect to MySQL! The error:"); 
      Console.WriteLine(ex.Message); 
      return false; 
     } 
    } 

    public static void closeConnection() 
    { 
     try 
     { 
      dbConnection.Close(); 
     } 
     catch 
     { 
      Console.WriteLine("tasdsdaa fodaa"); 
     } 
    } 

    public static void checkConnection() 
    { 
     try 
     { 
     if (dbConnection.State != ConnectionState.Open) 
     { 
      closeConnection(); 
      openConnection(Config.dbHost, Config.dbPort, Config.dbName, Config.dbUsername, Config.dbPassword); 
     } 
     } 
     catch { 
      Console.WriteLine("ta fodaa"); 
     } 
    } 

    public static void runQuery(string Query) 
    { 
     checkConnection(); 

     try { new MySqlCommand(Query, dbConnection).ExecuteScalar(); } 
     catch { } 
    } 

    public static int insertGetLast(string Query) 
    { 
     checkConnection(); 
     return int.Parse((new MySqlCommand(Query + "; SELECT LAST_INSERT_ID();", dbConnection).ExecuteScalar()).ToString()); 
    } 

    public static string runRead(string Query) 
    { 
     checkConnection(); 

     try { return new MySqlCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar().ToString(); } 
     catch 
     { 
      return ""; 
     } 
    } 

    public static int runRead(string Query, object Tick) 
    { 
     checkConnection(); 

     try { return Convert.ToInt32(new MySqlCommand(Query + " LIMIT 1", dbConnection).ExecuteScalar()); } 
     catch 
     { 
      return 0; 
     } 
    } 

    public static string[] runReadColumn(string Query, int maxResults) 
    { 
     checkConnection(); 

     MySqlCommand Command = null; 
     MySqlDataReader Reader = null; 

     if (maxResults > 0) 
      Query += " LIMIT " + maxResults; 

     try 
     { 
      Command = dbConnection.CreateCommand(); 
      Command.CommandText = Query; 
      Reader = Command.ExecuteReader(); 
      if (Reader.HasRows) 
      { 
       ArrayList columnBuilder = new ArrayList(); 
       while (Reader.Read()) 
       { 
        try { columnBuilder.Add(Reader[0].ToString()); } 
        catch { columnBuilder.Add(""); } 
       } 
       return (string[])columnBuilder.ToArray(typeof(string)); 
      } 
      else 
      { 
       return new string[0]; 
      } 
     } 
     catch(MySqlException Ex) 
     { 
      if (Command != null) 
      { 
       Console.WriteLine("[MySQL] Error!\n\r" + Command.CommandText, Ex); 
      } 
      else 
      { 
       Console.WriteLine("[MySQL] Error!", Ex); 
      } 
     } 
     finally 
     { 
      // Close the reader/command if they are active. 
      if (Reader != null) 
      { 
       Reader.Close(); 
       Reader.Dispose(); 
      } 
      if (Command != null) 
      { 
       Command.Dispose(); 
      } 
     } 
     return new string[0]; 
    } 

    public static int[] runReadColumn(string Query, int maxResults, object Tick) 
    { 
     checkConnection(); 

     MySqlCommand Command = null; 
     MySqlDataReader Reader = null; 

     if (maxResults > 0) 
      Query += " LIMIT " + maxResults; 

     try 
     { 
      Command = dbConnection.CreateCommand(); 
      Command.CommandText = Query; 
      Reader = Command.ExecuteReader(); 
      if (Reader.HasRows) 
      { 
       ArrayList columnBuilder = new ArrayList(); 
       while (Reader.Read()) 
       { 
        try { columnBuilder.Add(Reader.GetInt32(0)); } 
        catch { columnBuilder.Add(0); } 
       } 
       return (int[])columnBuilder.ToArray(typeof(int)); 
      } 
      else 
      { 
       return new int[0]; 
      } 
     } 
     catch (MySqlException Ex) 
     { 
      if (Command != null) 
      { 
       Console.WriteLine("[MySQL] Error!\n\r" + Command.CommandText, Ex); 
      } 
      else 
      { 
       Console.WriteLine("[MySQL] Error!", Ex); 
      } 
     } 
     finally 
     { 
      // Close the reader/command if they are active. 
      if (Reader != null) 
      { 
       Reader.Close(); 
       Reader.Dispose(); 
      } 
      if (Command != null) 
      { 
       Command.Dispose(); 
      } 
     } 
     return new int[0]; 
    } 

    public static string[] runReadRow(string Query) 
    { 
     checkConnection(); 

     MySqlCommand Command = null; 
     MySqlDataReader Reader = null; 

     try 
     { 
      Command = dbConnection.CreateCommand(); 
      Command.CommandText = Query + " LIMIT 1"; 
      Reader = Command.ExecuteReader(); 
      if (Reader.HasRows) 
      { 
       ArrayList rowBuilder = new ArrayList(); 
       while (Reader.Read()) 
       { 
        for (int i = 0; i < Reader.FieldCount; i++) 
        { 
         try { rowBuilder.Add(Reader[i].ToString()); } 
         catch { rowBuilder.Add(""); } 
        } 
       } 
       return (string[])rowBuilder.ToArray(typeof(string)); 
      } 
      else 
      { 
       return new string[0]; 
      } 
     } 
     catch (MySqlException Ex) 
     { 
      if (Command != null) 
      { 
       Console.WriteLine("[MySQL] Error!\n\r" + Command.CommandText, Ex); 
      } 
      else 
      { 
       Console.WriteLine("[MySQL] Error!", Ex); 
      } 
     } 
     finally 
     { 
      // Close the reader/command if they are active. 
      if (Reader != null) 
      { 
       Reader.Close(); 
       Reader.Dispose(); 
      } 
      if (Command != null) 
      { 
       Command.Dispose(); 
      } 
     } 
     return new string[0]; 
    } 

    public static int[] runReadRow(string Query, object Tick) 
    { 
     checkConnection(); 

     MySqlCommand Command = null; 
     MySqlDataReader Reader = null; 

     try 
     { 
      Command = dbConnection.CreateCommand(); 
      Command.CommandText = Query + " LIMIT 1"; 
      Reader = Command.ExecuteReader(); 
      if (Reader.HasRows) 
      { 
       ArrayList rowBuilder = new ArrayList(); 
       while (Reader.Read()) 
       { 
        for (int i = 0; i < Reader.FieldCount; i++) 
        { 
         try { rowBuilder.Add(Reader.GetInt32(i)); } 
         catch { rowBuilder.Add(0); } 
        } 
       } 
       return (int[])rowBuilder.ToArray(typeof(int)); 
      } 
      else 
      { 
       return new int[0]; 
      } 
     } 
     catch (MySqlException Ex) 
     { 
      if (Command != null) 
      { 
       Console.WriteLine("[MySQL] Error!\n\r" + Command.CommandText, Ex); 
      } 
      else 
      { 
       Console.WriteLine("[MySQL] Error!", Ex); 
      } 
     } 
     finally 
     { 
      // Close the reader/command if they are active. 
      if (Reader != null) 
      { 
       Reader.Close(); 
       Reader.Dispose(); 
      } 
      if (Command != null) 
      { 
       Command.Dispose(); 
      } 
     } 
     return new int[0]; 
    } 


    public static bool checkExists(string Query) 
    { 
     checkConnection(); 
     try { return new MySqlCommand(Query + " LIMIT 1", dbConnection).ExecuteReader().HasRows; } 
     catch 
     { 
      return false; 
     } 
    } 

    public static List<List<string>> readArray(string Query) 
    { 
     MySqlCommand Command = null; 
     MySqlDataReader Reader = null; 
     try 
     { 
      // Create the command. 
      Command = MySQL.dbConnection.CreateCommand(); 
      Command.CommandText = Query; 

      // Read the result. 
      Reader = Command.ExecuteReader(); 

      // Store the incomming fields. 
      List<List<string>> fieldValues = new List<List<string>>(); 

      // Read all the data. 
      while (Reader.Read()) 
      { 
       // Create a new field values to hold the data. 
       List<string> Buffer = new List<string>(); 

       // Add the field values. 
       for (int i = 0; i < Reader.FieldCount; i++) 
       { 
        Buffer.Add(Reader[i].ToString()); 
       } 

       // Add it too our overall data. 
       fieldValues.Add(Buffer); 
      } 
      return fieldValues; 
     } 
     catch (MySqlException Ex) 
     { 
      if (Command != null) 
      { 
       Console.WriteLine("[MySQL] Error!\n\r" + Command.CommandText, Ex); 
      } 
      else 
      { 
       Console.WriteLine("[MySQL] Error!", Ex); 
      } 
      return null; 
     } 
     finally 
     { 
      // Close the reader/command if they are active. 
      if (Reader != null) 
      { 
       Reader.Close(); 
       Reader.Dispose(); 
      } 
      if (Command != null) 
      { 
       Command.Dispose(); 
      } 
     } 
    } 


    public static string Stripslash(string Query) 
    { 
     try { return Query.Replace(@"\", "\\").Replace("'", @"\'"); } 
     catch { return ""; } 
    } 
} 
+1

просто укажите соответствующий код! не вся ваша программа –

ответ

0

Вы должны вызвать Close на ваших Reader объектов, чтобы освободить подчеркнутый ресурсы, что-то вроде этого:

try 
    { 
     Command = dbConnection.CreateCommand(); 
     Command.CommandText = Query; 
     using (Reader = Command.ExecuteReader()) // 'using' forces a call to `Dispose`/`Close` at the end of the block 
     { 
      if (Reader.HasRows) 
      { 
       ArrayList columnBuilder = new ArrayList(); 
       while (Reader.Read()) 
       { 
        try { columnBuilder.Add(Reader.GetInt32(0)); } 
        catch { columnBuilder.Add(0); } 
       } 
       return (int[])columnBuilder.ToArray(typeof(int)); 
      } 
      else 
      { 
       return new int[0]; 
      } 
     } 
    } 
0

очевидный вопрос для меня здесь:

public static bool checkExists(string Query) 
{ 
    checkConnection(); 
    try { return new MySqlCommand(Query + " LIMIT 1", dbConnection).ExecuteReader().HasRows; } 
    catch 
    { 
     return false; 
    } 
} 

Вы ar здесь закрываем читателя, УТЕЧИТЕ.

Смежные вопросы