Skip to content

Commit 15d2f84

Browse files
Implementation of DateTime2 + accompanying GetSqlDateTime2 method (dotnet#846)
1 parent 661b640 commit 15d2f84

File tree

16 files changed

+526
-7
lines changed

16 files changed

+526
-7
lines changed

doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml

+19
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,25 @@
634634
]]></format>
635635
</remarks>
636636
</GetSqlDateTime>
637+
<GetSqlDateTime2>
638+
<param name="i">The zero-based column ordinal.</param>
639+
<summary>
640+
Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
641+
</summary>
642+
<returns>
643+
The value of the column expressed as a <see cref="T:System.Data.SqlTypes.SqlDateTime2" />.
644+
</returns>
645+
<remarks>
646+
<format type="text/markdown">
647+
<![CDATA[
648+
649+
## Remarks
650+
No conversions are performed; therefore, the data retrieved must already be a date/time value or Null, otherwise an exception is thrown.
651+
652+
]]>
653+
</format>
654+
</remarks>
655+
</GetSqlDateTime2>
637656
<GetSqlDecimal>
638657
<param name="i">The zero-based column ordinal.</param>
639658
<summary>Gets the value of the specified column as a <see cref="T:System.Data.SqlTypes.SqlDecimal" />.</summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<docs>
2+
<members name="SqlDateTime2">
3+
<SqlFileStream>
4+
<summary>Exposes SQL Server data that is stored as a DATETIME2.</summary>
5+
<remarks>
6+
<format type="text/markdown">
7+
<![CDATA[
8+
9+
## Remarks
10+
The <xref:Microsoft.Data.SqlTypes.SqlDateTime2> class is used to work with `DATETIME2` data.
11+
`DATETIME2`has a wider range than DATETIME and thus needs its own type.
12+
13+
]]>
14+
</format>
15+
</remarks>
16+
</SqlFileStream>
17+
<ctor1>
18+
<param name="isNull">Whether or not this instance should be considered null - true means null, false not null</param>
19+
<summary>
20+
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> struct.
21+
Will initialize the datetime value to 0.
22+
</summary>
23+
</ctor1>
24+
<ctor2>
25+
<param name="ticks">Number of ticks to initialize this instance with - in the range of <see cref="T:Microsoft.DateTime.MinValue.Ticks"/> &amp; <see cref="T:Microsoft.DateTime.MaxValue.Ticks"/></param>
26+
27+
<summary>
28+
Initializes a new instance of the <see cref="T:Microsoft.Data.SqlTypes.SqlDateTime2" /> class.
29+
</summary>
30+
<exception cref="T:System.ArgumentOutOfRangeException">
31+
<paramref name="ticks" /> is not in the range of <see cref="T:Microsoft.DateTime.MinValue.Ticks"/> &amp; <see cref="T:Microsoft.DateTime.MaxValue.Ticks"/>
32+
</exception>
33+
</ctor2>
34+
<Value>
35+
<summary>
36+
Gets the <see cref="T:System.DateTime"/> representation of this instance.
37+
</summary>
38+
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
39+
If this instance is null
40+
</exception>
41+
</Value>
42+
<Null>
43+
<summary>
44+
Gets an instance representing the value NULL from the database.
45+
</summary>
46+
</Null>
47+
<OperatorDateTime>
48+
<summary>
49+
Converts a DateTime into a SqlDateTime2
50+
</summary>
51+
</OperatorDateTime>
52+
<OperatorDBNull>
53+
<summary>
54+
Converts a DBNull instance into a Null SqlDateTime2
55+
</summary>
56+
</OperatorDBNull>
57+
<OperatorSqlDateTime>
58+
<summary>
59+
Converts a SqlDateTime2 into DateTime
60+
</summary>
61+
<exception cref="T:Microsoft.Data.SqlTypes.SqlNullValueException">
62+
If the SqlDateTime2 instance has a null value
63+
</exception>
64+
</OperatorSqlDateTime>
65+
<GetXsdType>
66+
<summary>
67+
returns a <see cref="T:System.Xml.XmlQualifiedName"/> for serialization purposes
68+
</summary>
69+
<param name="schemaSet">unused parameter</param>
70+
</GetXsdType>
71+
</members>
72+
</docs>

src/Microsoft.Data.SqlClient.sln

+1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlClient.Se
159159
EndProject
160160
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Data.SqlTypes", "Microsoft.Data.SqlTypes", "{5A7600BD-AED8-44AB-8F2A-7CB33A8D9C02}"
161161
ProjectSection(SolutionItems) = preProject
162+
..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlDateTime2.xml
162163
..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml = ..\doc\snippets\Microsoft.Data.SqlTypes\SqlFileStream.xml
163164
EndProjectSection
164165
EndProject

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/Common/AdapterUtil.cs

+5
Original file line numberDiff line numberDiff line change
@@ -527,5 +527,10 @@ internal static void SetCurrentTransaction(Transaction transaction)
527527
{
528528
Transaction.Current = transaction;
529529
}
530+
531+
static internal Exception WrongType(Type got, Type expected)
532+
{
533+
return Argument(StringsHelper.GetString(Strings.SQL_WrongType, got.ToString(), expected.ToString()));
534+
}
530535
}
531536
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@
464464
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
465465
<Compile Include="Microsoft\Data\SqlClient\SqlEnclaveAttestationParameters.cs" />
466466
<Compile Include="Microsoft\Data\SqlClient\EnclaveDelegate.cs" />
467+
<Compile Include="..\..\src\Microsoft\Data\SqlTypes\SqlDateTime2.cs" Link="Microsoft\Data\SQLTypes\SqlDateTime2.cs" />
467468
</ItemGroup>
468469
<!-- Windows only -->
469470
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlBuffer.cs

+34
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,40 @@ internal SqlDateTime SqlDateTime
615615
}
616616
}
617617

618+
internal SqlDateTime2 SqlDateTime2
619+
{
620+
get
621+
{
622+
if (IsNull)
623+
{
624+
return SqlDateTime2.Null;
625+
}
626+
627+
if (StorageType.DateTime2 == _type)
628+
{
629+
return new SqlDateTime2(GetTicksFromDateTime2Info(_value._dateTime2Info));
630+
}
631+
632+
if (StorageType.DateTime == _type)
633+
{
634+
// also handle DATETIME without boxing to object first
635+
var dateTime = SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart);
636+
637+
return new SqlDateTime2(dateTime.Ticks);
638+
}
639+
640+
if (StorageType.Date == _type)
641+
{
642+
return (SqlDateTime2)DateTime.MinValue.AddDays(_value._int32);
643+
}
644+
645+
// cannot use SqlValue, since that causes invalid cast exception since object cannot be dynamic cast to SqlDateTime2 - only explicit cast
646+
// (SqlDateTime2)(object)dateTimeValue;
647+
// So assume its called on some kind of DATE type, so DateTime property can handle it
648+
return (SqlDateTime2)DateTime;
649+
}
650+
}
651+
618652
internal SqlDecimal SqlDecimal
619653
{
620654
get

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDataReader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -2397,6 +2397,13 @@ virtual public SqlDateTime GetSqlDateTime(int i)
23972397
return _data[i].SqlDateTime;
23982398
}
23992399

2400+
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDateTime2/*' />
2401+
virtual public SqlDateTime2 GetSqlDateTime2(int i)
2402+
{
2403+
ReadColumn(i);
2404+
return _data[i].SqlDateTime2;
2405+
}
2406+
24002407
/// <include file='../../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDecimal/*' />
24012408
virtual public SqlDecimal GetSqlDecimal(int i)
24022409
{

src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj

+19-6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
<Compile Include="..\..\src\Microsoft\Data\Common\ActivityCorrelator.cs">
104104
<Link>Microsoft\Data\Common\ActivityCorrelator.cs</Link>
105105
</Compile>
106+
<Compile Include="..\..\src\Microsoft\Data\SqlTypes\SqlDateTime2.cs">
107+
<Link>Microsoft\Data\SqlTypes\SqlDateTime2.cs</Link>
108+
</Compile>
106109
<Compile Include="..\..\src\Microsoft\Data\Common\DbConnectionPoolKey.cs">
107110
<Link>Microsoft\Data\Common\DbConnectionPoolKey.cs</Link>
108111
</Compile>
@@ -368,18 +371,26 @@
368371
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCngProvider.cs" />
369372
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionCspProvider.cs" />
370373
<Compile Include="Microsoft\Data\SqlClient\SqlColumnEncryptionEnclaveProvider.cs" />
371-
<Compile Include="Microsoft\Data\SqlClient\SqlCommand.cs" />
372-
<Compile Include="Microsoft\Data\SqlClient\SqlCommandBuilder.cs" />
374+
<Compile Include="Microsoft\Data\SqlClient\SqlCommand.cs">
375+
<SubType>Component</SubType>
376+
</Compile>
377+
<Compile Include="Microsoft\Data\SqlClient\SqlCommandBuilder.cs">
378+
<SubType>Component</SubType>
379+
</Compile>
373380
<Compile Include="Microsoft\Data\SqlClient\SqlCommandSet.cs" />
374-
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs" />
381+
<Compile Include="Microsoft\Data\SqlClient\SqlConnection.cs">
382+
<SubType>Component</SubType>
383+
</Compile>
375384
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionFactory.cs" />
376385
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolGroupProviderInfo.cs" />
377386
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionPoolKey.cs" />
378387
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionString.cs" />
379388
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionStringBuilder.cs" />
380389
<Compile Include="Microsoft\Data\SqlClient\SqlConnectionTimeoutErrorInternal.cs" />
381390
<Compile Include="Microsoft\Data\SqlClient\SqlCredential.cs" />
382-
<Compile Include="Microsoft\Data\SqlClient\SqlDataAdapter.cs" />
391+
<Compile Include="Microsoft\Data\SqlClient\SqlDataAdapter.cs">
392+
<SubType>Component</SubType>
393+
</Compile>
383394
<Compile Include="Microsoft\Data\SqlClient\SqlDataReader.cs" />
384395
<Compile Include="Microsoft\Data\SqlClient\SqlDataReaderSmi.cs" />
385396
<Compile Include="Microsoft\Data\SqlClient\SqlDelegatedTransaction.cs" />
@@ -443,7 +454,9 @@
443454
<Compile Include="Microsoft\Data\ProviderBase\DbBuffer.cs" />
444455
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionClosed.cs" />
445456
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionFactory.cs" />
446-
<Compile Include="Microsoft\Data\ProviderBase\SqlConnectionHelper.cs" />
457+
<Compile Include="Microsoft\Data\ProviderBase\SqlConnectionHelper.cs">
458+
<SubType>Component</SubType>
459+
</Compile>
447460
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionInternal.cs" />
448461
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPool.cs" />
449462
<Compile Include="Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs" />
@@ -545,4 +558,4 @@
545558
<Import Project="$(NetFxSource)tools\targets\GenerateThisAssemblyCs.targets" />
546559
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyRef.targets" />
547560
<Import Project="$(NetFxSource)tools\targets\GenerateAssemblyInfo.targets" />
548-
</Project>
561+
</Project>

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlBuffer.cs

+34
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,40 @@ internal SqlDateTime SqlDateTime
597597
}
598598
}
599599

600+
internal SqlDateTime2 SqlDateTime2
601+
{
602+
get
603+
{
604+
if (IsNull)
605+
{
606+
return SqlDateTime2.Null;
607+
}
608+
609+
if (StorageType.DateTime2 == _type)
610+
{
611+
return new SqlDateTime2(GetTicksFromDateTime2Info(_value._dateTime2Info));
612+
}
613+
614+
if (StorageType.DateTime == _type)
615+
{
616+
// also handle DATETIME without boxing to object first
617+
var dateTime = SqlTypeWorkarounds.SqlDateTimeToDateTime(_value._dateTimeInfo._daypart, _value._dateTimeInfo._timepart);
618+
619+
return new SqlDateTime2(dateTime.Ticks);
620+
}
621+
622+
if (StorageType.Date == _type)
623+
{
624+
return (SqlDateTime2)DateTime.MinValue.AddDays(_value._int32);
625+
}
626+
627+
// cannot use SqlValue, since that causes invalid cast exception since object cannot be dynamic cast to SqlDateTime2 - only explicit cast
628+
// (SqlDateTime2)(object)dateTimeValue;
629+
// So assume its called on some kind of DATE type, so DateTime property can handle it
630+
return (SqlDateTime2)DateTime;
631+
}
632+
}
633+
600634
internal SqlDecimal SqlDecimal
601635
{
602636
get

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs

+7
Original file line numberDiff line numberDiff line change
@@ -2777,6 +2777,13 @@ virtual public SqlDateTime GetSqlDateTime(int i)
27772777
return _data[i].SqlDateTime;
27782778
}
27792779

2780+
/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDateTime2/*' />
2781+
virtual public SqlDateTime2 GetSqlDateTime2(int i)
2782+
{
2783+
ReadColumn(i);
2784+
return _data[i].SqlDateTime2;
2785+
}
2786+
27802787
/// <include file='..\..\..\..\..\..\..\doc\snippets\Microsoft.Data.SqlClient\SqlDataReader.xml' path='docs/members[@name="SqlDataReader"]/GetSqlDecimal/*' />
27812788
virtual public SqlDecimal GetSqlDecimal(int i)
27822789
{

0 commit comments

Comments
 (0)