If you use your component dateTimePicker_pesquisar
to filter the data by month and day passing one by one separately you can do as the example below.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
sqlConnection.ConnectionString =
"Data Source=DESK008;" +
"Initial Catalog=stackoverflow;" +
"User id=sa;" +
"Password=123456;";
}
SqlConnection sqlConnection = new SqlConnection();
private void button1_Click(object sender, EventArgs e)
{
int ano = dateTimePicker_pesquisar.Value.Year;
int mes = dateTimePicker_pesquisar.Value.Month;
int dia = dateTimePicker_pesquisar.Value.Day;
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = ("select * from Hora WHERE year(data) = @ano and month(data) = @mes and day(data) = @dia ");
// Adicione o parâmetro de entrada e definar suas propriedades .
SqlParameter parameterAno = new SqlParameter()
{
ParameterName = "@ano",
SqlDbType = SqlDbType.Int,
Direction = ParameterDirection.Input,
Value = ano
};
SqlParameter parameterMes = new SqlParameter()
{
ParameterName = "@mes",
SqlDbType = SqlDbType.Int,
Direction = ParameterDirection.Input,
Value = mes
};
SqlParameter parameterDia = new SqlParameter()
{
ParameterName = "@dia",
SqlDbType = SqlDbType.Int,
Direction = ParameterDirection.Input,
Value = dia
};
// Adicione o parâmetro para a coleção Parameters.
sqlCommand.Parameters.Add(parameterAno);
sqlCommand.Parameters.Add(parameterMes);
sqlCommand.Parameters.Add(parameterDia);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
sqlCommand.ExecuteNonQuery();
dataGridView_Principal.DataSource = dataTable;
//SomarTotalGrid();
sqlConnection.Close();
}
}
}
Or you can summarize your query
in a single parameter.
private void button1_Click(object sender, EventArgs e)
{
var data = dateTimePicker_pesquisar.Value.Date;
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = ("select * from Hora WHERE convert(date, data) = @data ");
// Adicione o parâmetro de entrada e definar suas propriedades .
SqlParameter parameterdata = new SqlParameter()
{
ParameterName = "@data",
SqlDbType = SqlDbType.Int,
Direction = ParameterDirection.Input,
Value = data
};
// Adicione o parâmetro para a coleção Parameters.
sqlCommand.Parameters.Add(parameterdata);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
sqlCommand.ExecuteNonQuery();
dataGridView_Principal.DataSource = dataTable;
//SomarTotalGrid();
sqlConnection.Close();
}
Or if the user enters the year month and day in fields separately you can use the first option only changes the type of input in the form and capture in the code Behind.
Or even implementing your event picker_ValueChanged
. using the Eventhandler
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
sqlConnection.ConnectionString =
"Data Source=DESK008;" +
"Initial Catalog=stackoverflow;" +
"User id=sa;" +
"Password=123456;";
dateTimePicker_pesquisar.ValueChanged += new EventHandler(button1_Click);
}
SqlConnection sqlConnection = new SqlConnection();
private void button1_Click(object sender, EventArgs e)
{
var data = dateTimePicker_pesquisar.Value.Date;
sqlConnection.Open();
SqlCommand sqlCommand = sqlConnection.CreateCommand();
sqlCommand.CommandText = ("select * from Hora WHERE convert(date, data) = @data ");
// Adicione o parâmetro de entrada e definar suas propriedades .
SqlParameter parameterdata = new SqlParameter()
{
ParameterName = "@data",
SqlDbType = SqlDbType.Date,
Direction = ParameterDirection.Input,
Value = data
};
// Adicione o parâmetro para a coleção Parameters.
sqlCommand.Parameters.Add(parameterdata);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
sqlCommand.ExecuteNonQuery();
dataGridView_Principal.DataSource = dataTable;
//SomarTotalGrid();
sqlConnection.Close();
}
}
}
Friend as I understand it, you want to carry the year first, then you would show everything of that year, then the month to show everything of that month and so to the right day? how are you setting the search parameters? on a single datatimepiker? think Voce will have to have one for year one for month and one for day.
– Thomas Erich Pimentel
Yes @Thomas Erich Pimentel is on a date time Piker... I wanted to do everything in one so I wouldn’t get too much.
– Diogo Sousa
then, until to extract the months or days of only one, however you would have to have 3 buttons, to fetch either by the year or by month or by day. pq if not as you would define it for the system when it is to search by day, or when it is to search by month for example.
– Thomas Erich Pimentel
@Thomas Erich Pimentel I think that if there was a way to know which button was clicked on the date time Piker could do... but finding this way is also not easy
– Diogo Sousa
it is perhaps possible, hoping that someone will answer there.
– Thomas Erich Pimentel