C# 比较SQL表上的Datetime.now到date
我试图在c#上编写代码,将SQL表(表:items,列“endTime”)上的日期与datetime.now和结果显示图像进行比较 例如: 如果列表上的时间早于当前时间。。所以显示在aspx图像1上,否则显示图像2 我已尝试通过sql命令执行此操作:C# 比较SQL表上的Datetime.now到date,c#,asp.net,sql,sqlcommand,C#,Asp.net,Sql,Sqlcommand,我试图在c#上编写代码,将SQL表(表:items,列“endTime”)上的日期与datetime.now和结果显示图像进行比较 例如: 如果列表上的时间早于当前时间。。所以显示在aspx图像1上,否则显示图像2 我已尝试通过sql命令执行此操作: private DateTime endTime(out int lastDate) { SqlConnection connection = new SqlConnection("Data Source=******;Initia
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);
intd;
控制台写入线(结束时间(out d));
如果(d
但我有错误,但我相信这是返回的结果。您不应该从查询中去掉日期时间而不是int吗?此外,堆栈跟踪/调试器应该为您提供异常的行号。你能发布堆栈跟踪吗
int
转换为DateTime
,请显示一个代码块中,如下所示:
我建议让数据库在sql中正确地进行日期比较
SYSDATE可以在查询中与EndTime right进行比较,您可以不返回不匹配的行(这允许您平等地处理结果集中的每一行),或者在返回集中检查一个简单的值以查看时间是否在正确的时间段内。而不是
if(d
使用以下命令:if(d
我很抱歉,但是我犯了错误
并不能很好地描述你所遇到的问题。“我犯了错误”大概是你能得到的最模糊的信息,还有一个额外的好处就是英语很糟糕。什么错误/什么行/等等。这个代码不完整,没有任何意义……问题在vwd的线路上:如果(dendTime(out DateTime lastDate)
并将其他代码更改为lastDate=(DateTime)command2.ExecuteScalar();
另外,为什么要让命令运行两次?在转换DateTime时要小心,请查看SqlDateTime类谢谢。我的错误是因为我从其他用于其他事情的代码复制了它。我还有其他问题,可能会给出解决方案。我该怎么办:if(lable.text=“end”)…image1.visble;?我如何检查标签上是否有写的东西?
using (SqlConnection connection = new SqlConnection(...))