C# .NET DateTime 到 SqlDateTime 的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2191120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET DateTime to SqlDateTime Conversion
提问by Sreedhar
While converting .NET DateTime (when is default(DateTime)) to SqlDateTime should I always check if the .NET date is between SqlDateTime.MinValue and SqlDateTime.MaxValue [or] Is there a good way to do this.
在将 .NET DateTime (when is default(DateTime)) 转换为 SqlDateTime 时,我应该总是检查 .NET 日期是否在 SqlDateTime.MinValue 和 SqlDateTime.MaxValue [或] 之间有没有好的方法可以做到这一点。
采纳答案by Winston Smith
Is it possible that the date could actually be outside that range? Does it come from user input? If the answer to either of these questions is yes, then you should always check - otherwise you're leaving your application prone to error.
日期实际上可能在该范围之外吗?它来自用户输入吗?如果这些问题中的任何一个的答案是肯定的,那么您应该始终检查 - 否则您的应用程序容易出错。
You can format your date for inclusion in an SQL statement rather easily:
您可以很容易地格式化日期以包含在 SQL 语句中:
var sqlFormattedDate = myDateTime.Date.ToString("yyyy-MM-dd HH:mm:ss");
回答by fremis
If you are checking for DBNULL, converting a SQL Datetime to a .NET DateTime should not be a problem. However, you can run into problems converting a .NET DateTime to a valid SQL DateTime.
如果您正在检查 DBNULL,将 SQL 日期时间转换为 .NET 日期时间应该不是问题。但是,您可能会遇到将 .NET DateTime 转换为有效 SQL DateTime 的问题。
SQL Server does not recognize dates prior to 1/1/1753. Thats the year England adopted the Gregorian Calendar. Usually checking for DateTime.MinValue is sufficient, but if you suspect that the data could have years before the 18th century, you need to make another check or use a different data type. (I often wonder what Museums use in their databases)
SQL Server 无法识别 1/1/1753 之前的日期。那是英格兰采用公历的那一年。通常检查 DateTime.MinValue 就足够了,但如果您怀疑数据可能在 18 世纪之前有几年,则需要再次检查或使用不同的数据类型。(我经常想知道博物馆在他们的数据库中使用了什么)
Checking for max date is not really necessary, SQL Server and .NET DateTime both have a max date of 12/31/9999 It may be a valid business rule but it won't cause a problem.
检查最大日期并不是真正必要的,SQL Server 和 .NET DateTime 的最大日期都是 12/31/9999 这可能是一个有效的业务规则,但不会引起问题。
回答by Fakrudeen
Also please remember resolutions [quantum of time] are different.
另外请记住分辨率[时间量]是不同的。
http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime.aspx
http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqldatetime.aspx
SQL one is 3.33 ms and .net one is 100 ns.
SQL 一个是 3.33 ms,.net 一个是 100 ns。
回答by womd
on my quest to do this with entitie, i stumbled over here, just hitting back to post what i've found out...
在我与实体一起做这件事的过程中,我偶然发现了这里,只是回过头来发布我发现的东西......
when using EF4, "a sql's" datetime column can be filled from .NET's DateTime using BitConverter.
使用 EF4 时,可以使用 BitConverter 从 .NET 的 DateTime 填充“sql 的”日期时间列。
EntitieObj.thetime = BitConverter.GetBytes(DateTime.Now.ToBinary());
also Fakrudeen's link brought me further... thank you.
Fakrudeen 的链接也让我更进一步......谢谢。
回答by christianb35
-To compare only the date part, you can do:
- 仅比较日期部分,您可以执行以下操作:
var result = db.query($"SELECT * FROM table WHERE date >= '{fromDate.ToString("yyyy-MM-dd")}' and date <= '{toDate.ToString("yyyy-MM-dd"}'");
回答by rsc
var sqlCommand = new SqlCommand("SELECT * FROM mytable WHERE start_time >= @StartTime");
sqlCommand.Parameters.Add("@StartTime", SqlDbType.DateTime);
sqlCommand.Parameters("@StartTime").Value = MyDateObj;