Performing a SELECT through C# for an Access database

Asked

Viewed 685 times

1

private void button1_Click(object sender, EventArgs e)
{
    OleDbConnection Con = new OleDbConnection();
    Con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='|DataDirectory|\CALL_CENTER.accdb';Persist Security Info=True";


    try
      {
           Con.Open();
           OleDbCommand Cmm = new OleDbCommand();

           Cmm.CommandText = "SELECT Cliente, DATA_AGENDA FROM Agendamento WHERE Cliente LIKE  '%"+textBox1.Text+"%' AND DATA_AGENDA BETWEEN '"+dateTimePicker1.Value+"' AND DATA_AGENDA '"+dateTimePicker2.Value+"' ";
           Cmm.CommandType = CommandType.Text;
           Cmm.Connection = Con;

           OleDbDataReader DR;
           DR = Cmm.ExecuteReader();

           listBox1.Items.Clear();
           while (DR.Read())
           {
               listBox1.Items.Add(DR.GetString(0) + " / " + DR.GetDateTime(1));
           }

           Con.Close();

      }
      catch (Exception)
      {
           MessageBox.Show("impossível localizar registro! Verifique o código digitado");
      }
}
  • 1

    And do you have a problem, in addition to capturing all possible exceptions to say that you didn’t locate the record?

  • I have a problem with this SELECT. Cmm.Commandtext = "SELECT Client, DATA_AGENDA FROM Scheduling WHERE Client LIKE '%"+textBox1.Text+"%' AND DATA_AGENDA BETWEEN '"+dateTimePicker1.Value+"' AND DATA_AGENDA '"+dateTimePicker2.Value+"' ";

  • I am performing a SELEC in the Access Database, where I am consulting by the client name and the problem is in the rule that consults a period between dates, I believe that the syntax is incorrect is this help that I am needing.

  • Friend, enter the exceptions launched because so it is not possible to find out if there is an error in this dateTimePicker.Value. Try to capture these dates and check which string format is generated, so it is easier to find out. Another tip is to check if it is necessary to convert this way dateTimePicker.Value.toString().

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.