2016-06-21 4 views
-4

내 Access 데이터베이스에 여러 사람이 연결되어 있고 두 사람이 동시에 검색하는 경우 오류가 발생합니다. 그래서 그것을 고치기 위해 이것을했는데 오류를 많이 줄였습니다. 그러나 나는 때때로 그것을 때때로 얻습니다. 더 좋은 방법이 있습니까? 나는 이런 식으로 잡으려고해서는 안된다고 확신한다.이 액세스 코드를 수정하는 방법

public partial class Form2 : Form 
{ 
    private OleDbConnection connection = new OleDbConnection(); 
    public Form2() 
    { 
     InitializeComponent(); 
     connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb"; 
     System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); 
     conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb"; 
    } 
    protected override void WndProc(ref Message m) 
    { 
     base.WndProc(ref m); 
     if (m.Msg == WM_NCHITTEST) 
      m.Result = (IntPtr)(HT_CAPTION); 
    } 

    private const int WM_NCHITTEST = 0x84; 
    private const int HT_CLIENT = 0x1; 
    private const int HT_CAPTION = 0x2; 

    private void lRead(string query, ListBox lbox) 
    { 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connection; 
     command.CommandText = query; 
     OleDbDataReader reader = command.ExecuteReader(); 
     try 
     { 
      while (reader.Read()) 
      { 
       lbox.Items.Add(reader["UAS"].ToString()); 
      } 
     } 
     catch (Exception ex) 
     { MessageBox.Show("error LREAD" + ex); } 
     finally 
     { 
      if (reader != null) 
      { reader.Dispose(); } 
     } 
    } 


    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 

     try 
     { 
      connection.Open(); 
      listBox1.Items.Clear(); 
      lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1); 
      lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1); 
      lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1); 
      lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1); 
      //select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use % 
      connection.Close(); 
     } 
     catch 
     { 
      System.Threading.Thread.Sleep(500); 
      try 
      { 
       connection.Open(); 
       listBox1.Items.Clear(); 
       lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1); 
       lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1); 
       lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1); 
       lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1); 
       //select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use % 
       connection.Close(); 
      } 
      catch 
      { 
       System.Threading.Thread.Sleep(500); 
       try 
       { 
        connection.Open(); 
        listBox1.Items.Clear(); 
        lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1); 
        lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1); 
        lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1); 
        lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1); 
        //select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use % 
        connection.Close(); 
       } 
       catch 
       { 

        MessageBox.Show("Error too many People Searching "); 
       } 

      } 

     } 
+0

Google에 오류와 어떤 원인이 있는지 알려줘야합니다. – LarsTech

+0

그것은 디버깅이 필요 없다는 오류가 아닙니다. 너무 많은 사람들이 액세스 할 때 오류가 있습니다. 내 시도를 통해 연결을 처리해야합니다. – AllianceMaterials

+1

"오류가 아닙니다" "오류가 발생했습니다." 확실히 문제가 발생하면 문제가 발생합니다. – Brad

답변

2

이 줄들은 아무 것도 유용하지 않습니다. 새 OleDbConnection()을 할당 한 다음 범위를 벗어납니다. 연결은 제한된 리소스이므로 사용하지 않을 것입니다.

System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(); 
     conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb"; 

선들

private OleDbConnection connection = new OleDbConnection(); 
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb"; 

실제로 데이터베이스에 액세스하는 방법으로 이동한다. 연결은 제한된 리소스이므로 IDisposable을 구현합니다. IDisposable을 구현하는 모든 항목은 using statement을 사용하여 가능한 빨리 관련 리소스를 해제해야합니다. 당신은 여기에서 그렇게하지 않기 때문에 동시성을 제한 할 수도 있습니다. 대신 폼의 수명 동안 연결을 유지하고 가비지 컬렉터가 폼을 정리하는 데 오래 걸립니다.

textBox1_TextChanged에서 동일한 코드를 여러 번 반복합니다. 오류가 발생하기 쉽습니다 (한 곳에서 코드를 변경했지만 다른 곳에서 코드를 변경하는 경우). 이런 종류의 재시도 논리가 필요한 경우 루프를 사용하십시오. 또한 나는 잠자는 시간을 어느 정도 무작위로 지정하여 오류가 발생하는 여러 사용자가 다시 시도하기 전에 정확히 같은 시간 동안 잠을 자지 않도록하는 것이 좋습니다. 가능한 한 가장 짧은 시간 동안 연결을 유지할 수 있도록 연결 할당을이 방법으로 이동하십시오.

의견은 지금까지도 유용합니다. 나는 여기서 그 충고를 반복하지 않을 것이다.

+0

감사합니다. 좋은 시작이었습니다. 모든 사람들의 조언이 도움이되는 것 이상이었습니다. 이걸 시험해보고, 고맙습니다. – AllianceMaterials

관련 문제