How to select a date in c#?

Asked

Viewed 441 times

2

I intend to select by the current date, like: list all the data that was registered today in my table, I did this way and does not result in anything, does not generate errors or list any of the registered information.

//Método Listar registos cadastrado Actualmente
public List<View_Caixa> Listar_Data()
{
    List<View_Caixa> lis = new List<View_Caixa>();
    SqlConnection conexao = new SqlConnection(caminho);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conexao;
    cmd.CommandType = CommandType.Text;

    string li = "select *from view_caixa where data_registo=@data_registo"; //É aqui onde eu faço o meu select
    cmd.CommandText = li;
    cmd.Parameters.AddWithValue("data_registo", DateTime.Now);
    conexao.Close();
    SqlDataReader dr;
    conexao.Open();
    dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        View_Caixa obj = new View_Caixa();
        obj.id_caixa = Convert.ToInt32(dr["id_caixa"].ToString());
        obj.funcionario = dr["funcionario"].ToString();
        obj.caixa_inicial = Convert.ToDecimal(dr["caixa_inicial"].ToString());
        obj.total_veda_caixa = Convert.ToDecimal(dr["total_veda_caixa"].ToString());
        obj.total_venda_multicaixa = Convert.ToDecimal(dr["total_venda_multicaixa"].ToString());
        obj.total_multipagamento = Convert.ToDecimal(dr["total_multipagamento"].ToString());
        obj.caixa_final = Convert.ToDecimal(dr["caixa_final"].ToString());
        obj.conciliacao = Convert.ToDecimal(dr["conciliacao"].ToString());
        obj.data_registo = Convert.ToDateTime(dr["data_registo"].ToString());
        lis.Add(obj);
    }
    return lis;
}
  • @What is the database?

  • 1

    How the date is recorded in the bank?

  • I am using SQL-Server, I am writing this way taking the date automatically : cmd.Parameters.Addwithvalue("data_register", Datetime.Now);

2 answers

2

The DateTime.Now returns to date and time and in your select you are asking to list only where the date is exactly the same as the one you returned on DateTime.Now.

Change your select to (Using SQL-Server):

select * from view_caixa where data_registo between @data_registo and @data_registro2

In his DateTime.Now do this:

cmd.Parameters.AddWithValue("data_registo", DateTime.Now.ToString("dd/MM/yyyy"+" 00:00:00"));
cmd.Parameters.AddWithValue("data_registo", DateTime.Now.ToString("dd/MM/yyyy"+" 23:59:59"));

This way he will get all records from the bank within that specified period. Even if your bank is recording without the time, it will work, but note that depending on the BD, you need to pass the parameter to date. If it is oracle, for example, it would be select:

select * from view_caixa where data_registo between 
   TO_DATE(@data_registo, 'DD/MM/YYYY HH24:mi:ss') and 
   TO_DATE(@data_registro2, 'DD/MM/YYYY HH24:mi:ss')

Check how your date format is in the BD and how the query is being mounted.

  • I am using Sql-Server 2014..

  • I did so and nothing: cmd.Parameters.Addwithvalue("@data_record", Datetime.Now.Tostring("dd/MM/yyyy" + "23:59:59"); does not result in anything

  • @Antoniomateta you changed your select too and added the 2 parameters?

  • Yes, but I already managed to solve this problem, the problem is in "Time", replace "Time" for "Today" I look like this : cmd.Parameters.Addwithvalue("@data_registo", Datetime.Today);

  • @Antoniomateta good that solved. But beware of using Today, because it also returns the time. But this can be solved so if necessary: DateTime.Today.ToString("d")

  • Okay, thanks for all your help!

Show 1 more comment

1

In the call of the method AddWithValue, the parameter name must contain the arroba.

Substitute:

cmd.Parameters.AddWithValue("data_registo", DateTime.Now);

For:

cmd.Parameters.AddWithValue("@data_registo", DateTime.Now);

Edit and P.S.: George Wurthmann noticed the biggest problem in the code. His answer is the right one. The date/time comparison with Datetime.Now does not work because it is accurate up to the millisecond. You should only compare the date.

  • I’ve done something like that before, and it didn’t list or make a mistake.

  • @Antoniomateta may have other reasons why nothing is listed. We would need an example record of your base to know for what reasons it would not be listed.

  • I think it should be because it does not convert the date into string, because Datetime.Now brings the date along with the time, if I replace my parameter : cmd.Parameters.Addwithvalue("@data_record", Datetime.Now); to: it lists normally cmd.Parameters.Addwithvalue("@data_register", "2017-04-03");

  • Does it have as coverter the Datatime Format in Date only?

  • Good people, it came up but another doubt, I intend to do but a select, but to view only the data registered in a given week or month, as it would be my select?

  • I suggest creating a new specific question for this.

  • I created it, I’m just waiting for the answers.

Show 2 more comments

Browser other questions tagged

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