在SQL Server中保存没有时间的日期

本文关键字:没有时间 日期 保存 SQL Server | 更新日期: 2023-09-27 18:04:48

我在c#中有一个DateTimePicker,在数据库中我有一个datetime列。

我已经设置了dd/MM/yyyy作为DateTimePicker的属性,但它在数据库中保存了MM/dd/yyyy,时间为00:00:00.000。

我想协调这个DateTimePicker与列数据,我不想在数据库中显示时间。

我该怎么做?

在SQL Server中保存没有时间的日期

SQL Server有一个date类型,因此即使时间部分被发送给SQL Server, SQL Server也只会存储日期部分。例如:

// CREATE TABLE DateStorage
// (
//     Dates date
// )
DateTime now = DateTime.Now;
// now.ToString() ->  2013-05-24 09:39:00.0000000+00:00
// ---- INSERT ------
Db.Insert("DateStorage", now);
// Dates
// --------------
// 2013-05-24
// ----- READ ----- 
DateTime date = Db.Read("DateStorage").Rows[0]["Dates"] as DateTime;
// date.ToString() => 2013-05-24 00:00:00.00000 +00:00