Skip to content

Fix | Send an empty IEnumerable<SqlDataRecord> TVP value as an empty table - #5192

Open
dirtyvanity wants to merge 2 commits into
dotnet:mainfrom
dirtyvanity:dev/automation/fix-2971-empty-tvp-enumerable
Open

dirtyvanity wants to merge 2 commits into
dotnet:mainfrom
dirtyvanity:dev/automation/fix-2971-empty-tvp-enumerable

Conversation

@dirtyvanity

Copy link
Copy Markdown

Requested by John · project thread

Description

Setting a SqlDbType.Structured parameter's value to an IEnumerable<SqlDataRecord> that yields no records
throws ArgumentException ("There are no records in the SqlDataRecord enumeration..."). Callers have to check for
emptiness 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 null value: as a
default parameter, which SQL Server treats as an empty table.

  • SqlParameter.GetActualFieldsAndProperties returns the shared ParameterPeekAheadValue.Empty instead of throwing.
  • TdsParser.WriteSmiParameter sends a parameter whose peek-ahead value is empty as a default. The decision has
    to 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 in
    Strings.resx, Strings.Designer.cs and the 13 localized .resx files, so the localized key sets stay equal to the
    English one (the localization validation reports obsolete keys).
  • There is one code path for this: the unified project compiles the same SqlParameter/TdsParser for net462 and
    net10.0, and sync and async execution both write RPC parameters through WriteSmiParameter.
  • Unchanged: a null value, DataTable and DbDataReader values, and a first record with no columns, which still
    throws ArgumentException.

This follows the approach edwardneal sketched in #3518 (an explicit "empty" flag on a shared peek-ahead instance, with
WriteSmiParameter forcing the default), with a null check so non-enumerable TVP values are unaffected.

Behaviour change: code that assigned an empty enumerable and caught the ArgumentException now gets an empty table
instead. No public API changes.

Suggested release note: Fixed: an empty IEnumerable<SqlDataRecord> table-valued parameter value is now sent as an
empty table instead of throwing ArgumentException.

Issues

Fixes #2971

Testing

Unit tests in tests/UnitTests/Microsoft/Data/SqlClient/SqlParameterTests.cs for SqlParameter.MetaDataForSmi: an
empty 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):

  • an empty iterator (yield break) passed to a stored procedure, sync and async;
  • an empty List<SqlDataRecord> and an empty array in a text (sp_executesql) command;
  • an empty TVP between two populated ones, asserting each table's row count.

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 failed
    with the ArgumentException above 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.

  • Tests added or updated
  • Public API changes documented (none)
  • Ensure no breaking changes introduced (only a case that used to throw now succeeds)

This change was prepared with AI assistance and reviewed and tested by me.

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.
@dirtyvanity
dirtyvanity requested a review from a team as a code owner December 6, 2026 01:03
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Dec 6, 2026
@github-actions github-actions Bot added the Localization Needed PRs that contain changes that would need localization label Dec 6, 2026
@azure-pipelines

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

@dirtyvanity

Copy link
Copy Markdown
Author

@dotnet-policy-service agree company="dirtyvanity PBC"

This branch has not been deployed

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

Labels

Localization Needed PRs that contain changes that would need localization

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

Change SqlParameter to allow empty IEnumerable<SqlDataRecord>?

1 participant