【发布时间】:2015-03-19 07:40:45
【问题描述】:
如何比较 SQL Server (Date) 和 C# (DateTime.Now.Date),
我想给我这个比较的消息框。
public Form1()
{
DateTime dat1 = new DateTime();
dat1 = DateTime.Now.Date;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (dateTimePicker1.ToString == DateTime.Now.Date)
{
MessageBox.Show("date equla datetime.now.date", "etention", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
// selected this date in SQLserver
}
【问题讨论】:
-
我在这里看不到任何与 SQL 有关的代码。您大概有一个 DateTimePicker,并且您想将 that 与
DateTime.Now.Date(更容易写成DateTime.Todaybtw)进行比较,但我们不知道它与 SQL 的关系,甚至不知道这是什么类型的应用程序(Web、WinForms、WPF 等)? -
我不确定,您是在寻求有关创建 SQL 查询的帮助还是一般数据类型方面的帮助?
-
如果您想比较日期,您可以采取的最糟糕步骤是将它们转换为字符串(正如您目前在
Form1_Load中尝试的那样)。只要它们在适当地类型变量(例如DateTime)比较等,应该就可以工作。转换为字符串后,您突然不得不考虑日期将采用什么格式。
标签: c# sql-server-2008-r2 sql-server-2012-express