简体   繁体   English

在 C# 中存储时间的类型和在 T-SQL 中的相应类型

[英]Type to store time in C# and corresponding type in T-SQL

I would like to know how to store time in C# and T-SQL .我想知道如何在 C# 和 T-SQL 中存储时间 I know that both of them provide a DateTime type but I just need to store a time .我知道它们都提供了DateTime类型,但我只需要存储一个time For instance:例如:

var startTime = 9PM;
var endTime = 10PM;

And then store/retrieve this values from a database.然后从数据库中存储/检索这些值。 Thanks in advance.提前致谢。

Francesco弗朗切斯科

C# C#

Whether to use a DateTime or TimeSpan type in C# to store 9 PM is up to taste.是否使用 C# 中的DateTimeTimeSpan类型来存储9 PM取决于您的喜好。 Personally, I'd use DateTime , leaving the date component empty, since that's semantically closer to what you want.就个人而言,我会使用DateTime ,将日期组件留空,因为这在语义上更接近您想要的。 (A TimeSpan is designed to hold time intervals , such as "21 hours".) TimeSpan旨在保存时间间隔,例如“21 小时”。)

The documentation supports both options.该文档支持这两个选项。 This is from the documentation of TimeSpan :这是来自TimeSpan 的文档

The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. TimeSpan 结构也可用于表示一天中的时间,但前提是时间与特定日期无关。

On the other hand, the MSDN article Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo mentions the following:另一方面,MSDN 文章在 DateTime、DateTimeOffset 和 TimeZoneInfo 之间选择提到以下内容:

The DateTime structure is suitable for applications that do the following: DateTime 结构适用于执行以下操作的应用程序:
* Work with dates only. * 仅使用日期。
* Work with times only. * 仅使用时间。
[...] [...]


T-SQL T-SQL

SQL Server has a time data type. SQL 服务器具有time数据类型。

In C# there is not a type to hold only a time.在 C# 中,没有一个类型只能保存一次。 There is TimeSpan , but it's intended to keep a period of time and not really a component of a DateTime (ie hours and minutes) only.TimeSpan ,但它旨在保留一段时间,而不是DateTime的真正组成部分(即小时和分钟)。

Starting with SQL Server 2008 there is a time type ( Using Date and Time Data ) that does only store a time component.从 SQL Server 2008 开始,有一个time类型(使用日期和时间数据只存储一个时间组件。

EDIT: Misread your question at first.编辑:起初误读了您的问题。 TimeSpan is exactly what you're looking for and it can be stored in a time type with SQL 2K8. TimeSpan正是您正在寻找的,它可以使用 SQL 2K8 存储在time类型中。

In C# you'd probably want to use a TimeSpan structure if you just wanted to store a time interval .在 C# 中,如果您只想存储时间间隔,您可能想要使用TimeSpan结构。 However, you seem to want to appear to store a start-time and an end-time, which would require storing two values.但是,您似乎希望存储一个开始时间和一个结束时间,这需要存储两个值。 You could, therefore, use two TimeSpans (based on, say, number of minutes from midnight to represent the time) or you could just use two DateTime values and throw away the date component.因此,您可以使用两个TimeSpans (例如,基于从午夜开始的分钟数来表示时间),或者您可以只使用两个DateTime值并丢弃日期组件。

As has been noted, SQL Server 2008 has a Time datatype, but this isn't available in earlier versions which only have DateTime .如前所述,SQL Server 2008 具有Time数据类型,但这在只有DateTime的早期版本中不可用。 You could also just store an Int representing number of minutes past midnight which can be easily converted to a TimeSpan ( TimeSpan interval = TimeSpan.FromMinutes(60) ).您也可以只存储一个表示午夜过后的分钟数的Int ,它可以轻松转换为TimeSpanTimeSpan interval = TimeSpan.FromMinutes(60) )。

Timespan in c# is how you manipulate time intervals. c# 中的时间跨度是您操纵时间间隔的方式。 Contrary to what other posters are saying i don't think the Time data type is correct for storing time intervals in SQL, unless you actually want to store the start time and end time and not the time interval (ie 1 hour in you example).与其他海报所说的相反,我认为时间数据类型对于在 SQL 中存储时间间隔是不正确的,除非您实际上想要存储开始时间和结束时间而不是时间间隔(即在您的示例中为 1 小时) . It is for storing a time of day, a bit like a DateTime but with no date.它用于存储一天中的时间,有点像 DateTime 但没有日期。 When i want to store a time interval in SQL I just use an int and then have it represent a unit of time appropriate to what I am trying to do (egminutes, seconds, milliseconds etc. )当我想在 SQL 中存储一个时间间隔时,我只使用一个 int 然后让它代表一个适合我想要做的时间单位(例如分钟、秒、毫秒等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM