Skip to content

Fix | Write DateOnly instances to sql_variant columns of table-valued parameters as date values#4439

Open
edwardneal wants to merge 1 commit into
dotnet:mainfrom
edwardneal:fix/dateonly-tvp-variant
Open

Fix | Write DateOnly instances to sql_variant columns of table-valued parameters as date values#4439
edwardneal wants to merge 1 commit into
dotnet:mainfrom
edwardneal:fix/dateonly-tvp-variant

Conversation

@edwardneal

Copy link
Copy Markdown
Contributor

Description

This builds on #4294, and handles the only other issue I saw while investigating #3934.

If a user-defined table type exists with a column of type sql_variant, client applications can insert a DateOnly instance into this. It's supposed to be sent with a type of date, but is actually sent as a datetime.

Besides a point around correctness, it presents an issue when sending DateOnly instances which are valid values for date but not for datetime: these values overflow.

This PR fixes the issue, transporting all DateOnly instances with the date type on modern .NET. On .NET Framework, there's no DateOnly type - and thus there's no way for the client to encapsulate a date value within a sql_variant column.

@ErikEJ, this covers the only other way to write DateOnly instances to SQL Server and should hopefully completely close #3934. SqlClient will continue to read DateTime instances by default, but this is needed for backwards compatibility purposes.

NB: similar issues also exist with TimeOnly instances. TdsParser assumes that only TimeSpan instances will be written as times, so there are failures to cast in a few different places.

Issues

Builds on #4294. Possible resolution to #3934.

Testing

#4294 added the relevant tests, with an exemption carved out for this use case. I've removed the exemption and verified that it covers the test case.

Instances of DateOnly stored within a SqlDataRecord field of type Variant will now be sent as dates, not datetimes.

By extension: sending DateOnly instances with values outside the acceptable range for a datetime will no longer throw overflow exceptions.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Support sql_variant containing DateOnly as native SQL date without client conversion

1 participant