2011-08-10 61 views
0

我有一個包含兩列的表(start_timeend_time)。我從用戶那裏獲取開始和結束時間的信息並將其添加到表中。一旦用戶進入下一個開始並結束時間,我必須將其與數據庫進行比較。C#日期和時間比較

假設在表格中有一行的開始時間爲2011-08-10 16:00:00,結束時間爲2011-08-10 16:30:00。 假設用戶輸入值​​(start_time)和2011-08-10 16:25:00end_time)我能夠通過使用捕捉

 String getConflictTimeInBetween = string.Format("select       question_id,question_text from " + data_variables.RES_TXT_STRING_QUESTION_TABLE + " where start_time<='{0}' and end_time>='{1}'", start_full, end_full);//question_text='DFS'"2011-06-23 14:55);// 
         com = new SqlCommand(getConflictTimeInBetween, myConnection); 
         dr = com.ExecuteReader(); 

         if (dr.HasRows) 
         { 
          while (dr.Read()) 
          { 
           //Assign to your textbox here 
           conflictQuestionIdAtBetween = dr["question_id"].ToString(); 
           conflictQuestionTextAtBetween=dr["question_text"].ToString(); 
          } 
         } 

下面是一些樣本重疊,我想阻止

  1. START_TIME從2011-08-10 15:55:00和END_TIME 2011-08-10 16:05:00從0(5分鐘與已存在的數據重疊)

  2. START_TIME和END_TIME 2011-08-10 17:00:00(與已存在的數據5分鐘重疊)

  3. 2011-08-10 15:00:00和END_TIME 2011-08-10 17:00:00 START_TIME(與已存在的數據30分鐘重疊)

誰能幫我如何解決這三個問題。

+3

這是微不足道的。請向我們提供您已經嘗試了什麼,並解釋爲什麼它不作品。 –

+0

我覺得你使用了錯誤的術語。你的意思是要防止重複/重疊的情況發生在同一時間範圍內的數據? –

+0

你想看到重疊?你想只次啓動之間並結束?(在這種情況下,你會事件ually的時間只有1分鐘),究竟你想達到什麼目標? – mtijn

回答

0

既然你似乎有SQL語句組成部分,下面是發現在輸入時間和行時間之間蜱重疊的算法。

public long GetTimeOverlap(long inputStart, long inputEnd) 
    { 
     // I assume you can get the data yourself so heres only the algorithm. 
     long rowStart = new DateTime().Ticks, rowEnd = new DateTime().Ticks; 

     if (inputStart < rowStart) 
      if (inputEnd >= rowEnd) 
       // case 3 
       return rowEnd - rowStart; 
      else if (inputEnd > rowStart) 
       // case 1 
       return inputEnd - rowStart; 
      // Input time is before row time. 
      else return 0; 
     else if (inputStart >= rowEnd) 
      // Input time is after row time. 
      return 0; 
     else if (inputEnd >= rowEnd) 
      // case 2 
      return rowEnd - inputStart; 
      // case 0 
     else return inputEnd - inputStart; 
    } 
+0

這將工作,如果我3個查詢和查詢了合併,但隨後他會得到多少數據從數據庫返回的,做在客戶端計算。如果我是他,我會使用更好的查詢讓數據庫執行數學計算。儘管如此,這仍然是一個選擇。 – mtijn

0

我相信你想要做的相交之日起正確的範圍是什麼是一樣的東西:

String getConflictTimeInBetween = string.Format("select question_id,question_text from " + data_variables.RES_TXT_STRING_QUESTION_TABLE + "where (start_time<='{0}' and end_time>='{0}') or ((start_time<='{1}' and end_time>='{1}')", start_full, end_full); 
0

不知道你在你的問題是什麼意思,但在這裏要好得多代碼:

String getConflictTimeInBetween = string.Format("select question_id,question_text from {0} where start_time<[email protected] and end_time>[email protected]", data_variables.RES_TXT_STRING_QUESTION_TABLE); 
using (com = new SqlCommand(getConflictTimeInBetween, myConnection)) 
{ 
    com.Parameters.AddWithValue("@start", Convert.ToDateTime(start_full)); 
    com.Parameters.AddWithValue("@end", Convert.ToDateTime(end_full)); 
    using (dr = com.ExecuteReader()) 
    { 
     if (dr.HasRows) 
     { 
      while (dr.Read()) 
      { 
       //Assign to your textbox here 
       conflictQuestionIdAtBetween = dr["question_id"].ToString(); 
       conflictQuestionTextAtBetween=dr["question_text"].ToString(); 
      } 
     } 
    } 
} 
通過使用參數,而不是直銷

  1. 防止可能發生的SQL注入攻擊:

    它做同樣的事情加tly注入文本。

  2. 使用它們,以防止剩餘的開放和崩潰數據庫連接後處置的對象(命令和讀寫器)。這由using塊完成。
1

沒有你提到會顯示與你現在正在使用的查詢的3種重疊的情形。這是從您的文章並不清楚你的意思是要達到什麼,但我可以給你,將顯示每個場景查詢:

1)+ data_variables.RES_TXT_STRING_QUESTION_TABLE +「,其中START_TIME>」{0「從選擇question_id,QUESTION_TEXT」 }」和START_TIME < '{1} 「',start_full,end_full); // QUESTION_TEXT = 'DFS'」 2011-06-23 14時55分);

2) 「選擇question_id,從QUESTION_TEXT」 + data_variables .RES_TXT_STRING_QUESTION_TABLE + 「其中END_TIME> '{0}' 和END_TIME < '{1}'」,start_full,end_full); // QUESTION_TEXT = 'DFS'「2011-06-23 14時55分);

3) 「選擇question_id,從QUESTION_TEXT」 + data_variables。RES_TXT_STRING_QUESTION_TABLE +「where start_time>'{0}'and end_time <'{1}'」,start_full,end_full); // question_text ='DFS'「2011-06-23 14:55);

+0

我知道上面我提供之間有沒有這3個問題。但如何處理ü提供這些3 issues.the 3查詢所有相同如何捕捉它。 – bharathi