在 SQL Server CE 中表示时间跨度的最佳方式是什么?
具体来说,我只需要小时:分钟,但假设我有一个 .NET TimeSpan 对象,我应该如何将其存储在 SQL(CE) 数据库中?
Specifically speaking I only need hours:minutes but say I have a .NET TimeSpan object, how should I store that in a SQL(CE) database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 long 来表示 刻度数< /a>. 这就是 TimeSpan 使用的内部表示形式。 这使您可以使用 Timespan.FromTicks() 轻松重构对象 方法,并使用 Timespan 输出到数据库.Ticks 属性。 .NET中最小的时间单位是tick,等于100纳秒
I'd recommend using a long to represent the number of ticks. That's what TimeSpan uses as it's internal representation. This lets you easily reconstitute your object with the Timespan.FromTicks() method, and output to the database using the Timespan.Ticks property. The smallest unit of time in .NET is the tick, which is equal to 100 nanoseconds
存储为 varchar。 使用
TimeSpan.ToString()
保存到sql。 从sql中读取如下:Store as a varchar. Save to sql using
TimeSpan.ToString()
. Read from sql as:SQL CE 没有时间类型或用户定义类型,因此您的选择是日期时间或表示分钟的 int。 如果您需要存储的最大时间是 23:59 = 23 * 60 + 59 = 1439 = 一天中从第 0 分钟开始的分钟数,则smallint 是能够容纳该范围的最小整数类型。
抵制将小时和分钟存储在单独的列中作为tinyint的诱惑。 这将使用与单个smallint相同的空间,但是每次计算时间都需要将小时乘以60并添加分钟,并且每个
order by
将需要两列而不是一列。存储为分钟; 在显示时,您可以将分钟分为小时和分钟,
如果您想区分,我会将列命名为
分钟
,或absminutes
(绝对分钟)一天的1439分钟从一小时的0到59分钟。要将数据库值转换为 Timespan 对象,请使用构造函数
Timespan(int, int, int)
,如下所示new TimeSpan( Floor(absminutes / 60 ), absminutes % 60, 0)
或(更好)构造函数Timespan(long)
和new Timespan( absminutes * TimeSpan.TicksPerMinute )
。要从 Timespan 对象插入或更新数据库,请将ABSMINUTES 设置为
someTimespan.TotalMinutes % 1440
。SQL CE doesn't have a time type, or user defined types, so your choices are datetime or an int representing minutes. If the largest time you need to store is 23:59 = 23 * 60 + 59 = 1439 = the number of minutes in a day starting from minute 0, a smallint is the smallest integral type that will accommodate that range.
Resist the temptation to store hours and minutes in separate columns as tinyints. That would use the same space as a single smallint, but then every calculation of times will require multiplying hours by 60 and adding minutes, and every
order by
will require two columns instead of one.Store as minutes; on display, you can separate the minutes into hours and minutes with
I'd name the column(s)
minutes
, orabsminutes
(for absolute minutes) if you want to distinguish the 1439 minutes in a day from the 0-59 minutes in a hour.To convert from the database value to a Timespan object, use the ctor
Timespan(int, int, int)
like thisnew TimeSpan( floor(absminutes / 60 ), absminutes % 60, 0)
or (better) the ctorTimespan(long)
withnew Timespan( absminutes * TimeSpan.TicksPerMinute )
.To insert or update the database from a Timespan object, set absminutes to
someTimespan.TotalMinutes % 1440
.我目前正在考虑使用 SQL Time 来处理这个问题,但它不能工作 >= 24 小时。
好处包括易于阅读、易于在 SQL 和代码中使用,但缺点是您只有很短的时间跨度可以使用。
和
I'm currently considering using SQL Time to handle this, but it won't work for >= 24 hours.
The benefits include easily readable, easily usable in both SQL and in code, but the downfall is that you only have a small timespan to play with.
and