在访问数据库中插入日期和时间c#

时间:2022-01-21 14:02:17

In the access I'm only getting the date 5/12/2015 but no the time, I need something like this

在访问中我只得到2015年5月12日的日期,但没有时间,我需要这样的东西

5/12/2015 4:56 PM saved in the access database

2015年5月12日下午4:56保存在访问数据库中

  DateTime dtclickdate = DateTime.Now;
  OleDbParameter clickdate = new OleDbParameter("@clickdate ", OleDbType.DBDate);
  clickdate.Value = dtclickdate; 
  cmd.Parameters.Add(clickdate);

3 个解决方案

#1


Have a look at the MSDN description of OleDbType:

看看OleDbType的MSDN描述:

DBDate: Date data in the format yyyymmdd (DBTYPE_DBDATE). This maps to DateTime.

DBDate:格式为yyyymmdd(DBTYPE_DBDATE)的日期数据。这映射到DateTime。

As you can see, DBDate does not contain a time component. I would suggest to use Date instead:

如您所见,DBDate不包含时间组件。我建议使用Date代替:

Date: Date data, stored as a double (DBTYPE_DATE). The whole portion is the number of days since December 30, 1899, and the fractional portion is a fraction of a day. This maps to DateTime.

日期:日期数据,存储为double(DBTYPE_DATE)。整个部分是自1899年12月30日以来的天数,小数部分是一天的一小部分。这映射到DateTime。

According to the following Microsoft Knowledge Base article, this is the correct type to use for Access Date/Time fields.

根据以下Microsoft知识库文章,这是用于访问日期/时间字段的正确类型。

Quoted from INFO: OleDbType Enumeration vs. Microsoft Access Data Types:

引自INFO:OleDbType枚举与Microsoft Access数据类型:

Access Type Name  Database Data Type  OLE DB Type  .NET Framework Type  Member Name
    ...
Date/Time         DateTime            DBTYPE_DATE  System.DateTime    OleDbType.Date
    ...

* should really add support for tables...

*应该真正添加对表的支持......

PS: Note that OLEDB does not like Milliseconds in Date/Time fields. If you get a Data type mismatch in criteria expression error, remove the milliseconds:

PS:请注意,OLEDB不喜欢日期/时间字段中的毫秒数。如果在条件表达式错误中出现数据类型不匹配,请删除毫秒:

dtm = New DateTime(dtm.Year, dtm.Month, dtm.Day, dtm.Hour, dtm.Minute, dtm.Second)

#2


You should use the OleDbType.DBTimeStamp enumeration, which maps to DateTime:

您应该使用OleDbType.DBTimeStamp枚举,它映射到DateTime:

DateTime dtclickdate = DateTime.Now;
OleDbParameter clickdate = new OleDbParameter("@clickdate ", OleDbType.DBTimeStamp);
clickdate.Value = dtclickdate; cmd.Parameters.Add(clickdate);

See this documentation for more information.

有关更多信息,请参阅此文档。

#3


Try OleDbType.DBTimeStamp:

DateTime dtclickdate = DateTime.Now;
OleDbParameter clickdate = new OleDbParameter("@clickdate ", OleDbType.DBTimeStamp);
clickdate.Value = dtclickdate; 
cmd.Parameters.Add(clickdate);

Reason: this also stores time fraction as explained here.

原因:这也存储时间分数,如此处所述。

#1


Have a look at the MSDN description of OleDbType:

看看OleDbType的MSDN描述:

DBDate: Date data in the format yyyymmdd (DBTYPE_DBDATE). This maps to DateTime.

DBDate:格式为yyyymmdd(DBTYPE_DBDATE)的日期数据。这映射到DateTime。

As you can see, DBDate does not contain a time component. I would suggest to use Date instead:

如您所见,DBDate不包含时间组件。我建议使用Date代替:

Date: Date data, stored as a double (DBTYPE_DATE). The whole portion is the number of days since December 30, 1899, and the fractional portion is a fraction of a day. This maps to DateTime.

日期:日期数据,存储为double(DBTYPE_DATE)。整个部分是自1899年12月30日以来的天数,小数部分是一天的一小部分。这映射到DateTime。

According to the following Microsoft Knowledge Base article, this is the correct type to use for Access Date/Time fields.

根据以下Microsoft知识库文章,这是用于访问日期/时间字段的正确类型。

Quoted from INFO: OleDbType Enumeration vs. Microsoft Access Data Types:

引自INFO:OleDbType枚举与Microsoft Access数据类型:

Access Type Name  Database Data Type  OLE DB Type  .NET Framework Type  Member Name
    ...
Date/Time         DateTime            DBTYPE_DATE  System.DateTime    OleDbType.Date
    ...

* should really add support for tables...

*应该真正添加对表的支持......

PS: Note that OLEDB does not like Milliseconds in Date/Time fields. If you get a Data type mismatch in criteria expression error, remove the milliseconds:

PS:请注意,OLEDB不喜欢日期/时间字段中的毫秒数。如果在条件表达式错误中出现数据类型不匹配,请删除毫秒:

dtm = New DateTime(dtm.Year, dtm.Month, dtm.Day, dtm.Hour, dtm.Minute, dtm.Second)

#2


You should use the OleDbType.DBTimeStamp enumeration, which maps to DateTime:

您应该使用OleDbType.DBTimeStamp枚举,它映射到DateTime:

DateTime dtclickdate = DateTime.Now;
OleDbParameter clickdate = new OleDbParameter("@clickdate ", OleDbType.DBTimeStamp);
clickdate.Value = dtclickdate; cmd.Parameters.Add(clickdate);

See this documentation for more information.

有关更多信息,请参阅此文档。

#3


Try OleDbType.DBTimeStamp:

DateTime dtclickdate = DateTime.Now;
OleDbParameter clickdate = new OleDbParameter("@clickdate ", OleDbType.DBTimeStamp);
clickdate.Value = dtclickdate; 
cmd.Parameters.Add(clickdate);

Reason: this also stores time fraction as explained here.

原因:这也存储时间分数,如此处所述。