2013-10-11 115 views
0

我正在製作一個程序,讓用戶使用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

如何

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); 
} 
相關問題