0

i am making a program that will let the user pick two dates using dateTimePicker and i want to get the dates between the two dates. here is my current work: I hope you understand my problem..

i want to fill the listbox with the dates between the two dates

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();          
            }
        }    
    }
}
4

1 回答 1

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);
}
于 2013-10-11T16:16:06.600 回答