2011-06-30 86 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)