使用dateTimePicker搜索一周中的所有日期

时间:2016-08-03 02:26:22

标签: c# winforms date sql-server-ce datepart

我想要检索一周中的所有日子,选择该周的任何一天。

示例:想要检索2016年7月31日(年/月/日)到2016年6月6日(年/月/日)

通过选择所示周的任何一天来发生。

有可能吗?

我目前的代码:

private void AutoLoadCalendar()
        {

            string constringF = @"Data Source=|DataDirectory|\cadastramentodb.sdf;Persist Security Info=False";
            string QueryF = "select * from Funcionarios  where (status = N'Ativo') and datepart(year, datafimcontrato) = @ano and datepart(month, datafimcontrato) = @mes ";
            SqlCeConnection conDataBaseF = new SqlCeConnection(constringF);
            SqlCeCommand cmdDataBaseF = new SqlCeCommand(QueryF, conDataBaseF);
            cmdDataBaseF.Parameters.Add("@mes", SqlDbType.Int).Value = Convert.ToInt32(monthCalendar1.SelectionStart.Month);
            cmdDataBaseF.Parameters.Add("@ano", SqlDbType.Int).Value = Convert.ToInt32(monthCalendar1.SelectionStart.Year);
            try
            {


                SqlCeDataAdapter sda = new SqlCeDataAdapter();
                sda.SelectCommand = cmdDataBaseF;


                System.Data.DataTable dbdatasetF = new System.Data.DataTable();
                sda.Fill(dbdatasetF);
                BindingSource bSourceF = new BindingSource();

                bSourceF.DataSource = dbdatasetF;
                dataGridView1.DataSource = bSourceF;
                sda.Update(dbdatasetF);
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

1 个答案:

答案 0 :(得分:0)

祝你好运

您可以使用DATEPART功能来了解一周中的女性日期是否具有您选择的日期并且知道该数字很容易解决您的问题n ___ n

试试这个:

private void AutoLoadCalendar()
    {

        string constringF = @"Data Source=|DataDirectory|\cadastramentodb.sdf;Persist Security Info=False";
        string QueryF = 
            "DECLARE @DiaSemana AS INT " +
            "SET @DiaSemana=DATEPART(DW,@Fecha) " +
            " " +
            "SELECT * " +
            "FROM Funcionarios " +
            "WHERE [status] = N'Ativo' AND datafimcontrato >= DATEADD(DD,@DiaSemana*-1,@Fecha) AND datafimcontrato <= DATEADD(DD,7-@DiaSemana,@Fecha) ";
        SqlCeConnection conDataBaseF = new SqlCeConnection(constringF);
        SqlCeCommand cmdDataBaseF = new SqlCeCommand(QueryF, conDataBaseF);
        cmdDataBaseF.Parameters.Add("@Fecha", monthCalendar1.Value);
        try
        {
            SqlCeDataAdapter sda = new SqlCeDataAdapter();
            sda.SelectCommand = cmdDataBaseF;

            System.Data.DataTable dbdatasetF = new System.Data.DataTable();
            sda.Fill(dbdatasetF);
            BindingSource bSourceF = new BindingSource();

            bSourceF.DataSource = dbdatasetF;
            dataGridView1.DataSource = bSourceF;
            sda.Update(dbdatasetF);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }