C# 多日期时间 查询条件

效果图

使用了 HZH_Controls.UCSwitch 控件。网上有,可自行下载。感谢HZH_Controls作者的分享

日期控件是在HZH-Controls控件基础上修改的日期控件。下载地址:https://blog.csdn.net/gaoxiang19820514/article/details/138313553?spm=1001.2014.3001.5502

form1.cs

private void form1_Load(object sender, EventArgs e)
{
    this.dpeDataEntered_StartTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
    this.dpeDataEntered_StopTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");

    this.dpeInstallDate_StartTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
    this.dpeInstallDate_StopTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");

    this.dpeRemoveDate_StartTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
    this.dpeRemoveDate_StopTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");

    this.dpeBuyDate_StartTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00");
    this.dpeBuyDate_StopTime.CurrentTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59");
         
    //不知为何赋值后,差分钟,手动调整
    TimeSpan timeToSubtract = new TimeSpan(0, 32, 0); // 创建一个表示32分钟的TimeSpan
    this.dpeDataEntered_StartTime.CurrentTime = this.dpeDataEntered_StartTime.CurrentTime - timeToSubtract;
    this.dpeInstallDate_StartTime.CurrentTime = this.dpeInstallDate_StartTime.CurrentTime - timeToSubtract;
    this.dpeRemoveDate_StartTime.CurrentTime = this.dpeRemoveDate_StartTime.CurrentTime - timeToSubtract;
    this.dpeBuyDate_StartTime.CurrentTime = this.dpeBuyDate_StartTime.CurrentTime - timeToSubtract;

    TimeSpan timeToSubtract27 = new TimeSpan(0, 27, 0);//加上27分钟
    this.dpeDataEntered_StopTime.CurrentTime = this.dpeDataEntered_StopTime.CurrentTime + timeToSubtract27;
    this.dpeInstallDate_StopTime.CurrentTime = this.dpeInstallDate_StopTime.CurrentTime + timeToSubtract27;
    this.dpeRemoveDate_StopTime.CurrentTime = this.dpeRemoveDate_StopTime.CurrentTime + timeToSubtract27;
    this.dpeBuyDate_StopTime.CurrentTime = this.dpeBuyDate_StopTime.CurrentTime + timeToSubtract27;

    this.switchDateEntered.Checked = true;

}

查询按钮

private void btnSelect_BtnClick(object sender, EventArgs e)
{
            #region 时间条件
                if (this.switchDateEntered.Checked == false && this.switchInstallDate.Checked == false && this.switchRemoveDate.Checked == false && this.switchBuyDate.Checked == false)
                {
                    MessageBox.Show("请选择时间条件!");
                    return;
                }
                if (this.switchDateEntered.Checked == true)
                {
                    TimeSpan difference = this.dpeDataEntered_StopTime.CurrentTime - this.dpeDataEntered_StartTime.CurrentTime;
                    if (difference.Days > 60)
                    {
                        MessageBox.Show("检索的时间段不得超过60天!");
                        return;
                    }
                }#endregion

                this.btnSelect.Enabled = false;bool bolSwitchDateEntered = this.switchDateEntered.Checked;
                bool bolSwitchInstallDate = this.switchInstallDate.Checked;
                string strDataEntered_StartTime = this.dpeDataEntered_StartTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strDataEntered_StopTime = this.dpeDataEntered_StopTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strInstallDate_StartTime = this.dpeInstallDate_StartTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strInstallDate_StopTime = this.dpeInstallDate_StopTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");

                bool bolSwitchRemoveDate = this.switchRemoveDate.Checked;
                bool bolSwitchBuyDate = this.switchBuyDate.Checked;
                string strRemoveDate_StartTime = this.dpeRemoveDate_StartTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strRemoveDate_StopTime = this.dpeRemoveDate_StopTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strBuyDate_StartTime = this.dpeBuyDate_StartTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");
                string strBuyDate_StopTime = this.dpeBuyDate_StopTime.CurrentTime.ToString("yyyy-MM-dd HH:mm:ss");

}

组合SQL语句

       string strWhere = "";

            #region 时间条件
            if (bolSwitchDateEntered == true)//开始时间 结束时间
            {
                strWhere = strWhere + " and ( date_entered >='" + strDataEntered_StartTime + "' and date_entered <='" + strDataEntered_StopTime + "' )";
            }
            if (bolSwitchInstallDate == true)//开始时间 结束时间
            {
                strWhere = strWhere + " and ( install_date >='" + strInstallDate_StartTime + "' and install_date <='" + strInstallDate_StopTime + "' )";
            }
            //撤机时间
            if (bolSwitchRemoveDate == true)//开始时间 结束时间
            {
                strWhere = strWhere + " and ( remove_date >='" + strRemoveDate_StartTime + "' and remove_date <='" + strRemoveDate_StopTime + "' )";
            }
            //拆机时间
            if (bolSwitchBuyDate == true)//开始时间 结束时间
            {
                strWhere = strWhere + " and ( buy_date >='" + strBuyDate_StartTime + "' and buy_date <='" + strBuyDate_StopTime + "'  and is_buy='拆机')";
            }
            #endregion

 

posted @ 2024-06-14 16:46  海乐学习  阅读(13)  评论(0编辑  收藏  举报