【问题标题】:How to insert only time into the database如何仅将时间插入数据库
【发布时间】:2019-04-04 06:03:24
【问题描述】:

db中的数据类型是time(7)

模型类:

public DateTime intime { get; set;}
public DateTime out_time { get; set;}

控制器:

 DateTime newintime = DateTime.Parse(Request["intime"]);

 DateTime newouttime = DateTime.Parse(Request["out_time "]);
 model =  db.bookm.Where(x=>x.intime>= newintime && x.out_time <= newouttime ).First();

如果我转换为 string 它可以工作。 我想将它插入为例如(18:23:44)

【问题讨论】:

  • 你需要从数据库中插入或查询什么?
  • HH:MI:SS 格式的时间

标签: c# sql-server asp.net-mvc


【解决方案1】:

我猜你的数据库是 MS SQL Server。

SQL Server 以HH:MI:SS 格式存储时间。 在模型中使用TimeSpan 来存储/获取数据。

这是另一种可能的解决方案:How to save time only without the date using ASP.NET MVC 5 Data Annotation "DataType.Time?

【讨论】:

  • 其实,SQL Server 显示时间在HH:MI:SS,但是它存储为从午夜开始的滴答数,滴答数为100纳秒。
【解决方案2】:
DateTime newintime = DateTime.Parse(Request["intime"].ToLongTimeString()); //change here
DateTime newouttime = DateTime.Parse(Request["out_time"].ToLongTimeString());// here also
model =  db.bookm.Where(x=>x.intime>= newintime && x.out_time <= newouttime ).First();

用户 ToLongTimeString();

【讨论】:

  • 不,它不会工作。我们无法将字符串转换为 system.datetime
  • 对不起我的错误....DateTime newintime = DateTime.Parse(Request["intime"]).ToLongTimeString();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-18
  • 2017-02-08
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多