简体   繁体   中英

Connection error C#

private OleDbConnection conexao;
private Timer time = new Timer();

public void Conexao() //Conexão
{
   string strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DB.accdb";
   conexao = new OleDbConnection(strcon);
}

void tabela()
{
   Conexao();
   conexao.Open();
   label1.Text = DateTime.Now.ToString();
   string bn = "select D2 from Planilha where D2='" + label1.Text + "'";
   textBox1.Text = label1.Text;
   OleDbCommand Queryyy = new OleDbCommand(bn, conexao);
   OleDbDataReader drr;
   drr = Queryyy.ExecuteReader();
   if (drr.Read() == true)
   {
      try
      {
         MessageBox.Show("Hi");
      }
      catch (OleDbException ex)
      {
         MessageBox.Show("" + ex);
      }
   }
}

private void timer1_Tick(object sender, EventArgs e)
{
   tabela();
}

Timer Interval = 1000

(click for larger view)
http://i.stack.imgur.com/2g8QA.png

I'm all afternoon trying to fix it but could not so I come here for help

I think asawyer's comment had it right I bet the problem is from the fact you are not handling your objects correctly, get rid of your class objects and work with using statements

public OleDbConnection Conexao() //Conexão
{
   string strcon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|DB.accdb";
   return new OleDbConnection(strcon);
}

void tabela()
{
    try
    {    
        timer1.Enabled = false;
        using(var conexao = Conexao())
        {   
           conexao.Open();
           label1.Text = DateTime.Now.ToString();
           string bn = "select D2 from Planilha where D2='" + label1.Text + "'";
           textBox1.Text = label1.Text;
           using(OleDbCommand Queryyy = new OleDbCommand(bn, conexao))
           using(OleDbDataReader drr = Queryyy.ExecuteReader())
           {
               if (drr.Read() == true)
               {
                  try
                  {
                     MessageBox.Show("Hi");
                  }
                  catch (OleDbException ex)
                  {
                     MessageBox.Show("" + ex);
                  }
               }
           }
        }
    }
    finally
    {
        timer.Enabled = true;
    }
}

private void timer1_Tick(object sender, EventArgs e)
{
   tabela();
}

Also from the fact that you are only reading the first column of the first row, you should use ExecuteScalar instead of ExecuteReader .

using(OleDbCommand Queryyy = new OleDbCommand(bn, conexao))
{
   try
   {
       var result = Queryyy.ExecuteScalar();
       if (result != null)
       {
          MessageBox.Show("Hi");
       }
    }
    catch (OleDbException ex)
    {
       MessageBox.Show("" + ex);
    }
   }
}

You also should be using parametrized queries.

  label1.Text = DateTime.Now.ToString();
  string bn = "select D2 from Planilha where D2=@param1";
  textBox1.Text = label1.Text;
  using(OleDbCommand Queryyy = new OleDbCommand(bn, conexao))
  {
     Queryyy.Parameters.AddWithValue("@param1", label1.Text);
     //....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM