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

タイマー間隔 = 1000

(クリックすると大きく表示されます)
http://i.stack.imgur.com/2g8QA.png

私は午後ずっとそれを修正しようとしていますが、できなかったので、ここに助けを求めています

4

1 に答える 1

2

asawyerのコメントは正しかったと思います。問題は、オブジェクトを正しく処理していないという事実にあると思います。クラスオブジェクトを取り除き、usingステートメントを操作します

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

また、最初の行の最初の列しか読み取っていないという事実から、ExecuteScalar代わりに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);
    }
   }
}

また、パラメータ化されたクエリを使用する必要があります。

  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);
     //....
于 2013-11-13T22:48:38.537 に答える