简体   繁体   English

.NET DateTime 到 SqlDateTime 的转换

[英].NET DateTime to SqlDateTime Conversion

在将 .NET DateTime (when is default(DateTime)) 转换为 SqlDateTime 时,我应该总是检查 .NET 日期是否在 SqlDateTime.MinValue 和 SqlDateTime.MaxValue [或] 之间有没有好的方法可以做到这一点。

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");

If you are checking for DBNULL, converting a SQL Datetime to a .NET DateTime should not be a problem.如果您正在检查 DBNULL,将 SQL 日期时间转换为 .NET 日期时间应该不是问题。 However, you can run into problems converting a .NET DateTime to a valid SQL DateTime.但是,您可能会遇到将 .NET DateTime 转换为有效 SQL DateTime 的问题。

SQL Server does not recognize dates prior to 1/1/1753. SQL Server 无法识别 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.通常检查 DateTime.MinValue 就足够了,但如果您怀疑数据可能在 18 世纪之前有几年,则需要再次检查或使用不同的数据类型。 (I often wonder what Museums use in their databases) (我经常想知道博物馆在他们的数据库中使用了什么)

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 这可能是一个有效的业务规则,但不会引起问题。

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。

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 的链接也让我更进一步......谢谢。

- 仅比较日期部分,您可以执行以下操作:

var result = db.query($"SELECT * FROM table WHERE date >= '{fromDate.ToString("yyyy-MM-dd")}' and date <= '{toDate.ToString("yyyy-MM-dd"}'");
var sqlCommand = new SqlCommand("SELECT * FROM mytable WHERE start_time >= @StartTime");
sqlCommand.Parameters.Add("@StartTime", SqlDbType.DateTime);
sqlCommand.Parameters("@StartTime").Value = MyDateObj;

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

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