2011-06-30 107 views
0

我想在c#上編寫代碼,比較我對SQL表(表:項目,列「endTime」)和datetime.now以及結果 - 顯示圖像。在SQL表上比較Datetime.now

例如:

如果該列表中的時間是時間之前現在..所以顯示在ASPX image1的,否則顯示圖像2。

我試圖做到這一點的SQL命令:

private DateTime endTime(out int lastDate) 
{ 
    SqlConnection connection = new SqlConnection("Data Source=******;Initial Catalog=******;User ID=*****;Integrated Security=False;"); 
    string commandtext = "SELECT TOP(1) endTime FROM items"; 
    SqlCommand command = new SqlCommand(commandtext, connection); 
    connection.Open(); 
    SqlCommand command2 = new SqlCommand(commandtext, connection); 
    lastDate = (int)command2.ExecuteScalar(); 
    connection.Close(); 
    return ... 
} 

但我有回報的問題,並與方法的執行...:

  int d; 
     Console.WriteLine(endTime(out d)); 
     if (d < DateTime.Now) 
     { 
      image1.Visible = true; 

     } 
     else 
     { 
      image2.Visible = true; 
     } 
     Console.WriteLine(d); 

但我有錯誤,但我相信這是來自回報。

+1

我很抱歉,但'我error'是不是很描述您所遇到的問題。 –

+0

「我得到了錯誤」與你所能得到的一樣模糊,並且帶來了糟糕的英語。什麼錯誤/什麼行/等等。 – Coeffect

+0

這段代碼不完整,沒有意義... – gbn

回答

0

你不應該從你的查詢中拋出DateTime而不是int嗎?另外,堆棧跟蹤/調試器應該爲您提供例外的行號。你可以發佈堆棧跟蹤嗎?

+0

問題出在vwd上:if(d Oshrib

+0

只需使你的簽名私有DateTime'endTime(out DateTime lastDate)'並將其他代碼改爲'lastDate =(DateTime)command2.ExecuteScalar();'另外,爲什麼你的命令運行兩次? – skaz

+0

在鑄造DateTime時要小心,看看SqlDateTime類 – sll

0
  1. 什麼是由您的SQL查詢返回(我相信蜱)?
  2. 你如何轉換intDateTime,顯示using()塊的碼請
  3. 包圍的SqlConnection,如下圖所示:
using (SqlConnection connection = new SqlConnection(...)) 
0

我建議讓數據庫做正確的日期比較sql。

SYSDATE可以在查詢中與EndTime進行比較,並且可以不返回不匹配的行(它允許您平等地處理結果集中的每一行),也可以檢查返回集中的簡單值看看時間是否在正確的時間。

+0

我還有其他問題可能會給我解決方案。我怎麼辦:if(lable.text =「end」)... image1.visble; ?我如何檢查標籤上寫的東西? – Oshrib

+0

我發現...與平等。 – Oshrib

2

代替if (d < DateTime.Now)使用本:if (d < DateTime.Now.Date)