不同框架语言之间的 SQL 日期转换
我有这个查询:
SqlCommand cmdImpRN = new SqlCommand("SELECT COUNT(*) FROM copies WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, DATE_TIME_COLUMN)) = @Date;", conn);
cmdImpRN.Parameters.AddWithValue("@Date", DateTime.Now.ToString("MM-dd-yyyy"));
n_i_rn = (int)cmdImpRN.ExecuteScalar();
此查询旨在返回 DATE_TIME_COLUMN 日期(不是时间)等于今天日期的副本元组的数量。我目前正在部署到不同的 .NET 框架本地环境,因此我确保 @Date 添加为“MM-dd-yyyy”,并且它工作得很好,直到一些客户购买并安装了西班牙语的 SQL Server。现在该应用程序无法正常工作,会引发如下异常:
nvarchar 数据类型到 datetime 数据类型的转换 导致值超出范围
似乎我可以让它工作这样做:
cmdImpRN.Parameters.Add(new SqlParameter("@Date", SqlDbType.Date){Value = DateTime.Now })
但我不确定这是否是这样。为什么?因为我不知道 DateTime.Now 对于安装的不同 .NET Framework 语言是否返回不同的值,并且因为我不知道该构造函数是否会创建有效的日期对象,无论 DateTime.Now 的返回是否在 MM-dd 中yyyy 格式或 dd-MM-yyyy 格式。
有什么建议吗?提前致谢。
I have this query:
SqlCommand cmdImpRN = new SqlCommand("SELECT COUNT(*) FROM copies WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, DATE_TIME_COLUMN)) = @Date;", conn);
cmdImpRN.Parameters.AddWithValue("@Date", DateTime.Now.ToString("MM-dd-yyyy"));
n_i_rn = (int)cmdImpRN.ExecuteScalar();
This query is intented to return the number of tuples of copies whose DATE_TIME_COLUMN date (not time) is equal to today date. I'm currently deploying to diverse .NET framework local environments so I'm ensuring that the @Date is added as "MM-dd-yyyy" and it was working just fine until some client buy and install SQL Server on spanish. Now the app doesn't work on that throwing an exception like:
the conversion of a nvarchar data type to a datetime data type
resulted in an out-of-range value
It seems I can make it work doing this:
cmdImpRN.Parameters.Add(new SqlParameter("@Date", SqlDbType.Date){Value = DateTime.Now })
But I'm not sure if this is the way. Why? Because I don't know if DateTime.Now returns different for different .NET Framework languages installed and because I don't know if that constructor will create a valid Date Object no matter if the return of DateTime.Now is in MM-dd-yyyy format or dd-MM-yyyy format.
Any advice? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅使用 DateTime.Now 是正确的。 DateTime.Now 始终返回最终用户配置的时区的正确日期。所发生的情况是,日期以二进制格式(不依赖于区域设置)而不是作为字符串发送到服务器,并且服务器直接与该二进制格式进行比较,从而避免任何特定于区域设置的日期格式问题。
Your use of just DateTime.Now is correct. DateTime.Now always returns the correct date for the end-user's configured timezone. What happens is that the date is sent to the server in binary format (which is not locale-dependent), rather than as a string, and the server does the comparison against that binary format directly, avoiding any locale-specific date formatting issues.
使用日期时间.现在.日期。它已经是一个日期时间,格式 mm/dd/yyyy 并不重要。
更好的是,习惯 UTC 日期和时间,这样您就无需担心用户本地时间、服务器本地时间以及您拥有的其他时间。
Date 属性本身没有时间部分(它设置为午夜)。
http://msdn.microsoft.com/en-us/library /system.datetime.date.aspx
Use DateTime.Now.Date. It's already a DateTime and format mm/dd/yyyy won't matter.
Better yet, get used to UTC dates and times so you don't need to worry about user local time, server local time, and what have you.
The Date property has no time portion per se (it's set to midnight).
http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx