c#和访问数据库系统资源超出

时间:2016-02-09 16:42:58

标签: c# ms-access

我有一个访问数据库,我有一个刷新按钮,可以从数据库刷新这些列表框。如果某些事情发生了变化并且每件事都很有效,除非你多次点击刷新,否则会引发系统资源问题。我关闭连接但是有没有像垃圾收集器或我遗失的一些东西?

listBox10.Items.Clear();
listBox6.Items.Clear();
listBox5.Items.Clear();
listBox4.Items.Clear();
listBox3.Items.Clear();
listBox11.Items.Clear();
listBox12.Items.Clear();
listBox13.Items.Clear();
listBox14.Items.Clear();
listBox15.Items.Clear();

progressBar1.Value = 0;
// progressBar1.Increment(1);
connection.Open();

for (int i = -10; i < 10; i++)
{
    try
    {
        OleDbCommand command = new OleDbCommand();
        command.Connection = connection;
        string query = $"select * from Table1 where Status='Arches - InProduction' AND Tamp={i}";
        command.CommandText = query;
        OleDbDataReader reader = command.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox15.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command2 = new OleDbCommand();
        command2.Connection = connection;
        string query = $"select * from Table1 where Status='Arches - PreProduction' AND Tamp={i}";
        command2.CommandText = query;
        OleDbDataReader reader = command2.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox10.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }
    progressBar1.Increment(1);

    try
    {
        OleDbCommand command3 = new OleDbCommand();
        command3.Connection = connection;
        string query = $"select * from Table1 where Status='Rock - PreProduction' AND Tamp={i}";
        command3.CommandText = query;
        OleDbDataReader reader = command3.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox6.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command4 = new OleDbCommand();
        command4.Connection = connection;
        string query = $"select * from Table1 where Status='Rock - InProduction' AND Tamp={i}";
        command4.CommandText = query;
        OleDbDataReader reader = command4.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox14.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command5 = new OleDbCommand();
        command5.Connection = connection;
        string query = $"select * from Table1 where Status='TV - PreProduction' AND Tamp={i}";
        command5.CommandText = query;
        OleDbDataReader reader = command5.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox5.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command6 = new OleDbCommand();
        command6.Connection = connection;
        string query = $"select * from Table1 where Status='TV - InProduction' AND Tamp={i}";
        command6.CommandText = query;
        OleDbDataReader reader = command6.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox13.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }

    try
    {
        OleDbCommand command7 = new OleDbCommand();
        command7.Connection = connection;
        string query = $"select * from Table1 where Status='Slab - PreProduction' AND Tamp={i}";
        command7.CommandText = query;
        OleDbDataReader reader = command7.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox4.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command8 = new OleDbCommand();
        command8.Connection = connection;
        string query = $"select * from Table1 where Status='Slab - InProduction' AND Tamp={i}";
        command8.CommandText = query;
        OleDbDataReader reader = command8.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox12.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command9 = new OleDbCommand();
        command9.Connection = connection;
        string query = $"select * from Table1 where Status='Cast Stone - PreProduction' AND Tamp={i}";
        command9.CommandText = query;
        OleDbDataReader reader = command9.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox3.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



    try
    {
        OleDbCommand command10 = new OleDbCommand();
        command10.Connection = connection;
        string query = $"select * from Table1 where Status='Cast Stone - InProduction' AND Tamp={i}";
        command10.CommandText = query;
        OleDbDataReader reader = command10.ExecuteReader();
        progressBar1.Increment(1);
        while (reader.Read())
        {
            listBox11.Items.Add(reader["Description"].ToString());
        }
    }
    catch (Exception ex)
    { MessageBox.Show("error " + ex); }



}

connection.Close();

当我试图关闭它时说希望尝试

            OleDbCommand command = new OleDbCommand();
            OleDbDataReader reader = command.ExecuteReader();
            try
            {
                //OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                string query = $"select * from Table1 where      Status='Arches - InProduction' AND Tamp={i}";
                command.CommandText = query;
                //OleDbDataReader reader = command.ExecuteReader();
                progressBar1.Increment(1);
                while (reader.Read())
                {
                    listBox15.Items.Add(reader["Description"].ToString());
                }

            }
            finally
            {
                if (reader != null)
                {
                    reader.Dispose();
                }
            } 

1 个答案:

答案 0 :(得分:1)

你创建了许多在垃圾收集中有大量内存的小对象。 GC从不开始,因为它没有看到它。

主要原因是您忽略了IDisposable实现对象并且从不处理它们。这样做,问题就消失了。