简体   繁体   English

日期时间和文化

[英]DateTime and culture

SQL: SQL:

select * 
from tvideoconference 
where del = 'false' 
and iduserpatient = 0 and startdate >= N'18.02.2013 20:37:07'  
order by startdate 

Error: 错误:

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. 从nvarchar数据类型到datetime数据类型的转换导致值超出范围。

I got this error, when I tried to use method below in de-DE session culture. 当我尝试在de-DE会话文化中使用以下方法时,出现了此错误。 What is more, in pl-PL and en-US culture, this methods works perfect. 而且,在pl-PLen-US文化中,此方法非常有效。

public static DataSet getSpecialistConfs(int iduserspecialist)
{
    DateTime? today = DateTime.Now;
    today = today.Value.AddHours(-today.Value.Hour).AddMinutes(-(today.Value.Minute + 1));
    string sql = "select * from tvideoconference where del='false' and startdate >=N'" + today.Value + "' and iduserspecialist=" + iduserspecialist;
    sql += " order by startdate ";

    return Tools.SQLTools.getDataSet(sql);
}

How can I resolve it? 我该如何解决? I tried with many solutions (substrings, date format), with the same effect.. 我尝试了许多解决方案(子字符串,日期格式),但效果相同。

How can I resolve it? 我该如何解决?

Use parameterized SQL in the first place. 首先使用参数化的SQL。 Don't include the values in your query itself; 不要在查询本身中包含值; use parameters and set the values of those parameters. 使用参数并设置这些参数的值。

It's unclear what Tools.SqlTools is, but you really, really should use parameterized SQL. 目前尚不清楚Tools.SqlTools是什么,但是您确实应该使用参数化SQL。 That way you won't be converting the DateTime value into a string to start with, so cultures and formatting can't get in the way. 这样一来,您就不会将DateTime值转换为字符串开头,因此文化和格式不会受到影响。 Additionally, you won't be vulnerable to SQL injection attacks ... 此外,您将不会受到SQL注入攻击的攻击 ...

(Additionally, it's not at all clear why you're using DateTime? instead of DateTime - it's not like DateTime.Now can be null... and you should consider using DateTime.Today instead... or DateTime.UtcNow.Date .) (此外,还不清楚您为什么使用DateTime?而不是DateTime它与DateTime.Now可以不为null ...,而应考虑使用DateTime.Today ...或DateTime.UtcNow.Date 。 )

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 Management Studio中如何显示DateTime或尝试将字符串转换为DateTime时如何解析。

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT . SQL Server支持多种格式-请参阅CAST和CONVERT上MSDN联机丛书 Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not. 这些格式中的大多数取决于您拥有的设置-因此,这些设置可能有时会起作用-有时不起作用。

The 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 date format settings. 解决此问题的方法是使用SQL Server支持的(略微调整的) ISO-8601日期格式 -该格式始终有效 -不管您使用的SQL Server语言和日期格式设置如何。

The ISO-8601 format is supported by SQL Server comes in two flavors: SQL Server支持ISO-8601格式 ,有两种形式:

  • YYYYMMDD for just dates (no time portion); YYYYMMDD仅显示日期(无时间部分); 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-DD 不是独立的在SQL Server中的日期格式设置,并不会在所有情况下工作!

or: 要么:

  • 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 . YYYY-MM-DDTHH:MM:SS用于日期和时间-请注意:此格式带有破折号(但可以省略),并且在DATETIME的日期和时间部分之间使用固定的T作为分隔符。

This is valid for SQL Server 2000 and newer. 这对SQL Server 2000及更高版本有效。

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. 如果您使用SQL Server 2008或更高版本以及DATE数据类型(仅DATE 不使用 DATETIME !),则实际上也可以使用YYYY-MM-DD格式,并且在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. 但是使用YYYYMMDD格式,则可以使用任何版本的SQL Server以及SQL Server中的任何语言和日期格式设置。

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

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