If you user your dateTimePicker_pesquisar
component to filter the data by year and day by going 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 types the year month and day in separate fields you can use the first option only change the input type in the form and capture in the code behind.
Or even implementing your picker_ValueChanged
event. 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();
}
}
}