在SQL Server和Java之间具有一致的日期时间格式的最佳方法VB网 [英] Best approach to having consistent datetime formats between SQL Server & VB.Net

查看:150
本文介绍了在SQL Server和Java之间具有一致的日期时间格式的最佳方法VB网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发VB.Net应用程序,该应用程序的操作严重依赖日期和时间;次。如果应用程序日期格式与服务器日期格式不匹配,则日期格式可能会发生冲突,因此最好的做法是解决此问题。我知道SQL Server datetime 格式取决于服务器语言& VB.Net将使用本地计算机的日期时间格式。这意味着如果用户更改日期时间格式,将在插入时引起问题。

I am developing a VB.Net application that has operations which rely heavily on dates & times. As there could be conflicts on date formats if the application date format doesn't match the server date format what is the best practice to resolve this issue. I know that SQL Server datetime format depend on the server language & VB.Net will use the local machine datetime format. Which means if a user changes the datetime format it will cause problems when inserting.

我的想法是使用 SELECT GETDATE()在应用程序启动时,然后使用VB.Net&每当我尝试插入datetime值时,我都会将其转换为应用程序启动时确定的datetime格式。

My idea is to use SELECT GETDATE() on application startup, then identify its datetime format using VB.Net & whenever I try to insert a datetime value I will convert it to the datetime format identified at the application startup.

是否有更好的方法来使日期之间保持一致的datetime格式应用与SQL Server避免了冲突。例如:将一天与一个月混合在一起。

Is there a better approach to having a consistent datetime formats between the application & SQL Server to avoid comflicts. eg: Mixing a day with a month.

推荐答案

SQL Server不存储 DateTime 以任何字符串格式-它存储为8个字节的数值。

SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.

各种设置(语言,日期格式)仅影响<$ c在SQL Server Management Studio中向您显示了$ c> DateTime -或当您尝试将字符串转换为 DateTime 时如何解析。

The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.

SQL Server支持多种格式-请参见有关CAST和CONVERT的MSDN联机丛书。其中大多数格式取决于您所拥有的设置-因此,这些设置有时可能会起作用-有时不起作用。

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT. Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

因此,可能的话,不要在 DateTime 之间转换日期,并且始终保持字符串不变!在.NET中将其保留为 DateTime ,使用 DATE DATETIME2(n)在SQL Server中,并将日期设置为其原始格式。使用支持本地 DateTime 数据类型的参数化查询,因此您不需要将日期转换为字符串并返回!

So if ever possible, don't convert dates between DateTime and string all the time! Leave it as DateTime in .NET, use DATE or DATETIME2(n) in SQL Server, and let the dates be in their native format. Use parametrized queries that support the native DateTime datatype so you don't need to convert dates to string and back!

如果由于某种原因必须使用字符串表示日期,解决此问题的唯一可靠方法是使用(略微修改) ISO-8601日期格式受SQL Server支持-不管您使用哪种SQL Server语言和日期格式设置,此格式始终有效

If you must use strings to represent dates for whatever reason, the only reliable way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and dateformat settings.

SQL Server支持的href = http://msdn.microsoft.com/zh-cn/library/ms180878.aspx rel = noreferrer> ISO-8601格式有两种:

The ISO-8601 format is supported by SQL Server comes in two flavors:


  • YYYYMMDD 仅用于日期(无时间部分);请在此处注意:不要破折号!,这很重要! YYYY-MM-DD 独立于SQL Server中的日期格式设置,并且在所有情况下均有效!

  • YYYYMMDD for just dates (no time portion); note here: no dashes!, that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!

或:


  • YYYY-MM-DDTHH:MM:SS 用于日期和时间-请在此处注意:此格式具有破折号(但它们可以省略),并在 DATETIME 的日期和时间部分之间使用固定的 T 作为分隔符。

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME.

这对SQL Server 2000及更高版本有效。

This is valid for SQL Server 2000 and newer.

如果使用SQL Server 2008或更高版本以及 DATE 数据类型(仅 DATE -不是 DATETIME !),那么您实际上也可以使用 YYYY-MM-DD 格式,并且在SQL中的任何设置下也可以使用服务器。

If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME!), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.

不要问我,为什么整个主题如此棘手且有些令人困惑-就是这样。但是使用 YYYYMMDD 格式,您可以使用SQL Server的任何版本以及SQL Server中的任何语言和日期格式设置。

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.

对于SQL Server 2008及更高版本,建议仅使用日期部分使用 DATE ,并使用 DATETIME2(n) 当您同时需要日期和时间时。如果可能,您应该尝试逐步淘汰 DATETIME 数据类型

The recommendation for SQL Server 2008 and newer is to use DATE if you only need the date portion, and DATETIME2(n) when you need both date and time. You should try to start phasing out the DATETIME datatype if ever possible

这篇关于在SQL Server和Java之间具有一致的日期时间格式的最佳方法VB网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆