从数据库获取两个日期之间的日期

时间:2013-10-11 16:08:08

标签: c# select oledb ms-access-2010 datetimepicker

我正在创建一个程序,让用户使用dateTimePicker选择两个日期,我想获得两个日期之间的日期。这是我目前的工作:我希望你理解我的问题..

我想在列表框中填写两个日期之间的日期

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication7
{
    public partial class PrintOptions : Form
    {
        public PrintOptions()
        {
            InitializeComponent();
        }

        OleDbCommand command = new OleDbCommand();                           
        OleDbConnection connectionBilling = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Paul\Documents\Visual Studio 2010\Projects\WindowsFormsApplication7\WindowsFormsApplication7\bin\BillingComputation.accdb;Persist Security Info=False;");

        OleDbConnection connectionOrder = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Paul\Documents\Visual Studio 2010\Projects\WindowsFormsApplication7\WindowsFormsApplication7\bin\OrderData.accdb;Persist Security Info=False;");


       private void PrintOptions_Load(object sender, EventArgs e)
       {
           toolTip1.SetToolTip(dateTimePicker1, "Select the starting date of your desire data");
           toolTip1.SetToolTip(label1, "Select the starting date of your desire data");        
           toolTip1.SetToolTip(dateTimePicker1, "Select the end date of your desire data");
           toolTip1.SetToolTip(label1, "Select the end date of your desire data");

           toolTip1.SetToolTip(groupBox1, "Select the database you want to print");                   
       }

       private void button2_Click(object sender, EventArgs e)
       {
           this.Close();
       }

       private void button1_Click(object sender, EventArgs e)
       {      
           DateTime dt = this.dateTimePicker1.Value.Date;
           DateTime dt2 = this.dateTimePicker2.Value.Date;          

           //Billing Data
           if (radioButton1.Checked) 
           {    
                OleDbCommand  commandqwe =new OleDbCommand ("SELECT DateOfTransaction "+String.Format("{0:dd/MM/yyyy}",dt)+" FROM BillingSystem WHERE((DateOfTransaction " + String.Format("{0:dd/MM/yyyy}", dt2)  +"))",connectionBilling);
                connectionBilling.Open();

                OleDbDataReader reader = null;

                reader = commandqwe.ExecuteReader();

                while (reader.Read())
                {    
                    listBox1.Items.Add("DATE" + reader[4].ToString());
                }
                connectionBilling.Close();          
            }
        }    
    }
}

1 个答案:

答案 0 :(得分:1)

怎么样

DateTime firstDate = ...;
DateTime secondDate = ...;

DateTime smallDate;
DateTime bigDate;

if (firstDate <= secondDate)
{
    smallDate = firstDate;
    bigDate = secondDate;
}
else
{
    smallDate = secondDate;
    bigDate = firstDate;
}

smallDate = smallDate.Date;
bigDate = bigDate.Date;

List<DateTime> dt = new List<DateTime>();

while (smallDate <= bigDate)
{
    dt.Add(smallDate);
    smallDate = smallDate.AddDays(1);
}