2016-02-09 93 views
0

我有一个访问数据库,我有一个刷新按钮,刷新数据库中的这些列表框。万一有些事情发生了变化,每件事情都很好,除非你多次点击刷新,否则会引发系统资源问题。我关闭了连接,但有没有像垃圾回收器或我缺少的东西?c#和访问数据库系统资源超出

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(); 
       } 
      } 
+3

您应该在处理完所有读者后“处理()”所有读者。事实上'Dispose'实现'IDisposable'的任何东西(Commands,Conenction等)。这可能是也可能不是你的问题,但它肯定没有帮助。 – Liam

+0

写一个函数接受查询和列表框,执行查询并绑定到列表框也是有意义的。我怀疑这会解决你的问题,只是很讨厌。 –

回答

1

您创造了很多有大量的内存以外的垃圾收集小物件。 GC从未开始,因为它没有看到它。

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

+0

作为OP的额外提示,Google搜索“c#使用模式” –

相关问题