In C# there's a SqlDateTime.MinValue
and SqlDateTime.MaxValue
, but I can't find one for the SmallDateTime
datatype from SQL Server.
在c#中有一个SqlDateTime。MinValue和SqlDateTime。MaxValue,但是我无法从SQL Server中找到SmallDateTime数据类型。
var smallDateTimeMin = DateTime(1900, 1, 1);
var smallDateTimeMax = DateTime(2079, 6, 6);
Is there one or do I need to implement this myself?
我需要自己实现这个吗?
6 个解决方案
#1
5
There is no smalldatetime
equivalent in System.Data.SqlTypes
. Nor is there an equivalent for the new datetime2
type. So I wouldn't expect min-max constants for those types in the .NET framework.
在system . data . sqltype中没有任何smalldatetime等效的。对于新的datetime2类型,也没有相应的方法。所以我不希望在。net框架中这些类型有最小的常量。
But the types are well documented on MSDN:
但是在MSDN上有详细的记录:
Date and time data from January 1, 1753 through December 31, 9999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 seconds, as shown in the table.
1753年1月1日至9999年12月31日的日期和时间数据,准确率为三百分之一秒(相当于3.33毫秒或0.00333秒)。值被四舍五入到增量为.000、.003或.007秒,如表中所示。
So you can easily define your own min-max constants.
所以你可以很容易地定义你自己的最小最大常数。
#2
16
Why not use an extension method?
为什么不使用扩展方法呢?
public static class DateTimeExtensions
{
public static DateTime SmallDateTimeMinValue(this DateTime sqlDateTime)
{
return new DateTime(1900, 01, 01, 00, 00, 00);
}
public static DateTime SmallDateTimeMaxValue(this DateTime sqlDateTime)
{
return new DateTime(2079, 06, 06, 23, 59, 00);
}
}
DateTime date = DateTime.Now;
Console.WriteLine("Minvalue is {0} ", date.SmallDateTimeMinValue().ToShortDateString());
Admittedly, it'd be nice for extension properties, but those don't exist yet.
诚然,这对于扩展属性是很好的,但是这些属性还不存在。
#3
3
For my purposes I used a simple static helper class:
出于我的目的,我使用了一个简单的静态助手类:
public static class SqlSmallDateTime
{
public static readonly SqlDateTime MinValue =
new SqlDateTime(new DateTime(1900, 01, 01, 00, 00, 00));
public static readonly SqlDateTime MaxValue =
new SqlDateTime(new DateTime(2079, 06, 06, 23, 59, 00));
}
#4
2
Since SQLDateTime
maps to both datetime
and smalldatetime
, I guess you'll have to do that manually.
由于SQLDateTime映射到datetime和smalldatetime,所以我认为您必须手动进行操作。
#5
2
Min value 1900-01-01 00:00:00
最小值1900-01-01就是
Max value 2079-06-06 23:59:00
马克思价值2079-06-06 23:59:00
#6
1
You will have to use your own constants. It would seem that the System.Data.SqlTypes namespace is incomplete without SqlSmallDateTime, but that's actually not the case. The purpose of that namespace is to provide classes that prevent type conversion errors.
你必须使用你自己的常数。系统。数据。没有SqlSmallDateTime, SqlTypes名称空间是不完整的,但事实并非如此。该名称空间的目的是提供防止类型转换错误的类。
The System.Data.SqlTypes namespace provides classes for native data types within SQL Server 2005. These classes provide a safer, faster alternative to the data types provided by the .NET Framework common language runtime (CLR). Using the classes in this namespace helps prevent type conversion errors caused by loss of precision. Because other data types are converted to and from SqlTypes behind the scenes, explicitly creating and using objects within this namespace also yields faster code.
System.Data。SqlTypes名称空间为SQL Server 2005中的原生数据类型提供了类。这些类为。net Framework公共语言运行时(CLR)提供的数据类型提供了更安全、更快的替代。使用这个名称空间中的类有助于防止由于精度损失而导致的类型转换错误。由于其他数据类型在后台转换为和转换为SqlTypes,在这个名称空间中显式地创建和使用对象也会产生更快的代码。
MSDN
Because SqlDateTime is sufficient for that purpose, no SqlSmallDateTime is provided.
因为SqlDateTime足以满足此目的,所以不提供SqlSmallDateTime。
#1
5
There is no smalldatetime
equivalent in System.Data.SqlTypes
. Nor is there an equivalent for the new datetime2
type. So I wouldn't expect min-max constants for those types in the .NET framework.
在system . data . sqltype中没有任何smalldatetime等效的。对于新的datetime2类型,也没有相应的方法。所以我不希望在。net框架中这些类型有最小的常量。
But the types are well documented on MSDN:
但是在MSDN上有详细的记录:
Date and time data from January 1, 1753 through December 31, 9999, to an accuracy of one three-hundredth of a second (equivalent to 3.33 milliseconds or 0.00333 seconds). Values are rounded to increments of .000, .003, or .007 seconds, as shown in the table.
1753年1月1日至9999年12月31日的日期和时间数据,准确率为三百分之一秒(相当于3.33毫秒或0.00333秒)。值被四舍五入到增量为.000、.003或.007秒,如表中所示。
So you can easily define your own min-max constants.
所以你可以很容易地定义你自己的最小最大常数。
#2
16
Why not use an extension method?
为什么不使用扩展方法呢?
public static class DateTimeExtensions
{
public static DateTime SmallDateTimeMinValue(this DateTime sqlDateTime)
{
return new DateTime(1900, 01, 01, 00, 00, 00);
}
public static DateTime SmallDateTimeMaxValue(this DateTime sqlDateTime)
{
return new DateTime(2079, 06, 06, 23, 59, 00);
}
}
DateTime date = DateTime.Now;
Console.WriteLine("Minvalue is {0} ", date.SmallDateTimeMinValue().ToShortDateString());
Admittedly, it'd be nice for extension properties, but those don't exist yet.
诚然,这对于扩展属性是很好的,但是这些属性还不存在。
#3
3
For my purposes I used a simple static helper class:
出于我的目的,我使用了一个简单的静态助手类:
public static class SqlSmallDateTime
{
public static readonly SqlDateTime MinValue =
new SqlDateTime(new DateTime(1900, 01, 01, 00, 00, 00));
public static readonly SqlDateTime MaxValue =
new SqlDateTime(new DateTime(2079, 06, 06, 23, 59, 00));
}
#4
2
Since SQLDateTime
maps to both datetime
and smalldatetime
, I guess you'll have to do that manually.
由于SQLDateTime映射到datetime和smalldatetime,所以我认为您必须手动进行操作。
#5
2
Min value 1900-01-01 00:00:00
最小值1900-01-01就是
Max value 2079-06-06 23:59:00
马克思价值2079-06-06 23:59:00
#6
1
You will have to use your own constants. It would seem that the System.Data.SqlTypes namespace is incomplete without SqlSmallDateTime, but that's actually not the case. The purpose of that namespace is to provide classes that prevent type conversion errors.
你必须使用你自己的常数。系统。数据。没有SqlSmallDateTime, SqlTypes名称空间是不完整的,但事实并非如此。该名称空间的目的是提供防止类型转换错误的类。
The System.Data.SqlTypes namespace provides classes for native data types within SQL Server 2005. These classes provide a safer, faster alternative to the data types provided by the .NET Framework common language runtime (CLR). Using the classes in this namespace helps prevent type conversion errors caused by loss of precision. Because other data types are converted to and from SqlTypes behind the scenes, explicitly creating and using objects within this namespace also yields faster code.
System.Data。SqlTypes名称空间为SQL Server 2005中的原生数据类型提供了类。这些类为。net Framework公共语言运行时(CLR)提供的数据类型提供了更安全、更快的替代。使用这个名称空间中的类有助于防止由于精度损失而导致的类型转换错误。由于其他数据类型在后台转换为和转换为SqlTypes,在这个名称空间中显式地创建和使用对象也会产生更快的代码。
MSDN
Because SqlDateTime is sufficient for that purpose, no SqlSmallDateTime is provided.
因为SqlDateTime足以满足此目的,所以不提供SqlSmallDateTime。