Repository navigation
Fix | Send an empty IEnumerable<SqlDataRecord> TVP value as an empty table - #5192
Open
dirtyvanity wants to merge 2 commits into
Open
dirtyvanity wants to merge 2 commits into
dirtyvanity wants to merge 2 commits into
Conversation
Setting a table-valued parameter's value to an IEnumerable<SqlDataRecord> that yields no records threw ArgumentException, so callers had to check for emptiness first, which means materializing or enumerating the source. An empty enumerable carries no column metadata, so it is now sent the same way as a null value: as a default parameter, which SQL Server treats as an empty table. SqlParameter marks the peek-ahead value as empty instead of throwing, and TdsParser.WriteSmiParameter sends such a parameter as a default.
…ests The empty IEnumerable<SqlDataRecord> case now uses one shared ParameterPeekAheadValue.Empty instance instead of allocating a flagged value per parameter. The IEnumerableOfSqlDataRecordHasNoRows resource string had no caller left, so it is removed from Strings.resx, the designer file and the 13 localized files, keeping the localized key sets equal to the English one. Tests: unit tests for SqlParameter.MetaDataForSmi (empty enumerable, enumerable with a record, record without columns), and manual tests for an empty array and for an empty TVP between two populated ones. Test methods and helpers carry the XML summaries the testing guide asks for.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Author
|
@dotnet-policy-service agree company="dirtyvanity PBC" |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Requested by John · project thread
Description
Setting a
SqlDbType.Structuredparameter's value to anIEnumerable<SqlDataRecord>that yields no recordsthrows
ArgumentException("There are no records in the SqlDataRecord enumeration..."). Callers have to check foremptiness before assigning the value, which means materializing or enumerating the source first.
An empty enumerable carries no column metadata, so this change sends it the same way as a
nullvalue: as adefault parameter, which SQL Server treats as an empty table.
SqlParameter.GetActualFieldsAndPropertiesreturns the sharedParameterPeekAheadValue.Emptyinstead of throwing.TdsParser.WriteSmiParametersends a parameter whose peek-ahead value is empty as a default. The decision hasto be made there, after the enumerable has been read, because an iterator has no count to check up front.
SQL.IEnumerableOfSqlDataRecordHasNoRows()has no caller left and is removed, together with its resource string inStrings.resx,Strings.Designer.csand the 13 localized.resxfiles, so the localized key sets stay equal to theEnglish one (the localization validation reports obsolete keys).
SqlParameter/TdsParserfor net462 andnet10.0, and sync and async execution both write RPC parameters through
WriteSmiParameter.nullvalue,DataTableandDbDataReadervalues, and a first record with no columns, which stillthrows
ArgumentException.This follows the approach edwardneal sketched in #3518 (an explicit "empty" flag on a shared peek-ahead instance, with
WriteSmiParameterforcing the default), with a null check so non-enumerable TVP values are unaffected.Behaviour change: code that assigned an empty enumerable and caught the
ArgumentExceptionnow gets an empty tableinstead. No public API changes.
Suggested release note: Fixed: an empty
IEnumerable<SqlDataRecord>table-valued parameter value is now sent as anempty table instead of throwing
ArgumentException.Issues
Fixes #2971
Testing
Unit tests in
tests/UnitTests/Microsoft/Data/SqlClient/SqlParameterTests.csforSqlParameter.MetaDataForSmi: anempty enumerable returns the shared empty peek-ahead value and metadata without fields; an enumerable with a record
keeps that record; a record without columns still throws.
Manual tests in
tests/ManualTests/SQL/ParameterTest/TvpEmptyEnumerableTests.cs(Set 3), as asked for in #3518(iterators, empty lists, empty arrays, multiple TVPs):
yield break) passed to a stored procedure, sync and async;List<SqlDataRecord>and an empty array in a text (sp_executesql) command;The single-TVP tests also pass a parameter after the TVP, to check that later parameters still arrive.
Run against SQL Server 2022 (Linux container, Northwind created from
tools/testsql/createNorthwindDb.sql), net10.0,managed SNI, on the pushed commit:
TestSqlClientManual -p:TestSet=3: 489 passed, 0 failed, 125 skipped. The 5 new tests passed; the first 3 failedwith the
ArgumentExceptionabove before the fix.TestSqlClientUnit: 3358 passed, 0 failed, 20 skipped.TestSqlClientFunctional: 1312 passed, 0 failed, 15 skipped.BuildSqlClient(net462, net10.0, netstandard2.0 ref): 0 warnings, 0 errors.Not run: tests on net462 and native SNI (they need Windows), manual sets 1 and 2.
This change was prepared with AI assistance and reviewed and tested by me.