已经有一个开放的datareader-Mysql-C#I dispose-of-connection

已经有一个开放的datareader-Mysql-C#I dispose-of-connection,c#,mysql,C#,Mysql,这一次让我发疯,我不明白为什么它会抛出这个错误 这是我的程序 MySQLProcessor.Connection = MySQLProcessor.OpenCon(); MySQLProcessor.ThreadingConnection = MySQLProcessor.OpenCon(); DataTable All_Websites_DataTable = MySQLProcessor.DTTable("select BaseURL,subURL from

这一次让我发疯,我不明白为什么它会抛出这个错误

这是我的程序

MySQLProcessor.Connection = MySQLProcessor.OpenCon();
        MySQLProcessor.ThreadingConnection = MySQLProcessor.OpenCon();
        DataTable All_Websites_DataTable =  MySQLProcessor.DTTable("select BaseURL,subURL from CouponExtractor.tblUrls where GroceryStoreBit = b'0'",MySQLProcessor.Connection);
        MySQLProcessor.Connection.Dispose();
Semaphore _pool = new Semaphore(1, 50);
        Parallel.ForEach(All_Websites_DataTable.AsEnumerable(), website_DataRow =>
        {
            _pool.WaitOne();
            //additional_subURL is used to crawl threw additional URLs until unqiue value is hit
            int additional_subURL = 0;
            Catch_Old_Values = false;

            string baseURL = All_Websites_DataTable.ItemArray[0].ToString();
            string subURL = All_Websites_DataTable.ItemArray[1].ToString();
            string complete_url = string.Empty;

            //need a bool to check homepage without crawling subpages
            bool check_homepage = true;
            complete_url = baseURL + "/" + subURL.Replace("@", additional_subURL.ToString());
            string mysqlquery_siteprocesslog = "insert into tbllogs(message,timestampcolumn) Values('Processing: " + complete_url + "',Now())";
            MySQLProcessor.MySQLInsertUpdate(mysqlquery_siteprocesslog,MySQLProcessor.ThreadingConnection);

            //as long as catch_old_values is false, the app will continue crawling the website until it hits an old value
            while (Catch_Old_Values == false)
            {
                additional_subURL++;


                if (additional_subURL >= 7)
                {
                    Catch_Old_Values = true;
                    break;
                }
                else if (check_homepage == true)
                {
                    //set check_homepage to false because we will crawl the homepage and dont want to do it again.
                    check_homepage = false;
                    SiteProcessing.ProcessSite(baseURL, baseURL);


                }
                else
                {
                    complete_url = baseURL + "/" + subURL.Replace("@", additional_subURL.ToString());
                    SiteProcessing.ProcessSite(complete_url, baseURL);
                }

            }
            _pool.Release();
        });
这里是SiteProcessing中访问MysqlProcessor的唯一部分

 if (hotitem == true)
                    {
                        string mysqlquery_InserthotResults = "insert into couponextractor." + targetTable + " (BaseURL,Description,realURL,TimeStampcolumn,uniqueKey,hotkey) Values ('" + baseURL + "','" + description.Replace("'", "") + "','" + realURL + "',Now(),'" + uniqueKey.Replace("'", "") + "','" + hotitemkey + "')";
                        MySQLProcessor.MySQLInsertUpdate(mysqlquery_InserthotResults, MySQLProcessor.ThreadingConnection);
                    }
                    else
                    {
                        string mysqlquery_InsertResults = "insert into couponextractor." + targetTable + " (BaseURL,Description,realURL,TimeStampcolumn,uniqueKey) Values ('" + baseURL + "','" + description.Replace("'", "") + "','" + realURL + "',Now(),'" + uniqueKey.Replace("'", "") + "')";
                        MySQLProcessor.MySQLInsertUpdate(mysqlquery_InsertResults, MySQLProcessor.ThreadingConnection);
                    }
这是Mysqlprocessor

public static MySqlConnection Connection { get; set; }
    public static MySqlConnection ThreadingConnection { get; set; }
    public static MySqlConnection OpenCon()
    {
        MySqlConnection masterOpenCON = new MySqlConnection("removed for privacy");
        masterOpenCON.Open();
        return masterOpenCON;
    }

    public static DataTable DTTable(string mysqlQuery, MySqlConnection MysqlCon)
    {
        DataTable DTTableTable = new DataTable();
        using (MysqlCon)
        {
            using (MySqlDataAdapter DataDTTables = new MySqlDataAdapter(mysqlQuery, MysqlCon))
            {
                DataDTTables.SelectCommand.CommandTimeout = 2500;
                using (DataTable DataDTTablesDT = new DataTable())
                {
                    DataDTTables.Fill(DataDTTablesDT);
                    DTTableTable = DataDTTablesDT;
                    DataDTTablesDT.Dispose();
                }

            }
        }

        return DTTableTable;
    }

    public static void MySQLInsertUpdate(string MySQLCommand, MySqlConnection MysqlCon)
    {
        try
        {
            using (MysqlCon)
            {
                MySqlCommand MySQLCommandFunc = new MySqlCommand(MySQLCommand, MysqlCon);
                MySQLCommandFunc.CommandTimeout = 2500;
                MySQLCommandFunc.ExecuteNonQuery();
            }
        }
        catch (Exception ex)
        {
            if (ex.Message.ToString().Contains("Duplicate entry"))
            {
                //SiteController.Catch_Old_Values = true;
            }
            else if (ex.Message.ToString().Contains("Data too long for column"))
            {


            }
            else
            {
                EventLog.WriteEntry("CouponCrawler", ex.Message.ToString(), EventLogEntryType.Error);
            }
        }
    }
我很困惑,我甚至在进入线程之前就已经在Program.CS中处理掉了MySQLProcessor.Connection。然后我再也不会在应用程序的其余部分调用datareader。欢迎任何帮助我快疯了

正在此处引发异常:

using (MysqlCon)
            {
                MySqlCommand MySQLCommandFunc = new MySqlCommand(MySQLCommand, MysqlCon);
                MySQLCommandFunc.CommandTimeout = 2500;
                MySQLCommandFunc.ExecuteNonQuery();
            }

一旦执行离开using语句主体,“using”语句将处理该对象。在foreach循环中,您将在第一次调用MySQLInsertUpdate时处理连接


另外(以及出现异常的原因),是通过执行并行foreach,您将在同一连接上同时执行两个查询。

我对线程不太熟悉,但您似乎连续调用了两次OpenCon?引发异常的地方在哪里?另外,@roken它被抛出MySqlCommand MySQLCommandFunc=newmysqlcommand(MySqlCommand,MysqlCon)@JohnP我不确定为什么不可以,如果我想的话,我应该能够以这种方式打开100个连接。我真的不想处理连接,我想对我的所有环路使用相同的静态连接。我应该能够在同一个连接上执行多个查询,mysql更新命令是异步的,这是1000个网站,你知道需要多长时间?@Mike“注意,当DataReader打开时,连接被该DataReader独占使用。在原始DataReader关闭之前,你将无法执行任何连接命令,包括创建另一个DataReader。”@迈克,你有没有试过用同步的foreach虎钳,并行foreach来运行这个?