Use Datetimepicker as parameter

Asked

Viewed 16 times

-2

Good Afternoon! Personal, I’m new in C# and would like to know how I make a query using Datetimepicker in the BETWEEN of a select, below is my code.

public static DataTable ListarPrevisoesGrid()
    {
        DataTable dt = new DataTable();

        try
        {
            var vcon = ConnDataBase();
            var cmd = vcon.CreateCommand();

            {
                cmd.CommandText = "SELECT p.nome,v.vencimento,v.valor FROM pessoa p INNER JOIN pedido pd ON p.idpessoa = pd.fornecedor INNER JOIN pedvenc v ON v.idpedido = pd.idpedido WHERE v.vencimento BETWEEN '2021-07-01' AND '2021-07-30' AND pd.cnsstatus = 'PREVFIN' AND pd.cnscanmom IS NULL ORDER BY nome ASC";

                MySqlDataAdapter da = new MySqlDataAdapter(cmd.CommandText, ConnDataBase());
                da.Fill(dt);
                vcon.Close();
                return dt;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

1 answer

0

The value of Datetimepicker is Datetime type, so you just need to convert it to string.

Your code would look something like this:

//assumi que você tenha dois DateTimePicker para realizar a consulta
DateTime data1 = dateTimePicker1.Value; 
DateTime data2 = dateTimePicker2.Value;    

cmd.CommandText = "SELECT p.nome,v.vencimento,v.valor FROM pessoa p INNER JOIN pedido pd ON p.idpessoa = pd.fornecedor INNER JOIN pedvenc v ON v.idpedido = pd.idpedido WHERE v.vencimento BETWEEN '" + data1.ToString("yyyy-MM-dd") + "' AND '" + data2.ToString("yyyy-MM-dd") + "' AND pd.cnsstatus = 'PREVFIN' AND pd.cnscanmom IS NULL ORDER BY nome ASC";

Browser other questions tagged

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