Skip to content

Fix unbound parameter reporting for prefix-less and positional parameters - #39371

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
austinaviv:fix/sqlite-unbound-parameter-reporting
Oct 26, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
austinaviv:fix/sqlite-unbound-parameter-reporting

Conversation

@austinaviv

Copy link
Copy Markdown
Contributor

Fixes #39370

In SqliteCommand.GetStatements(), when expectedParams != boundParams, unbound parameters are gathered to populate the error message. Currently, it checks !_parameters.Cast<SqliteParameter>().Any(p => p.ParameterName == name).

This causes several issues:

  1. When parameters are added without prefix (e.g., cmd.Parameters.AddWithValue("Parameter", 1)), SQLite binds them successfully via FindPrefixedParameter, but the exact-equality check against @Parameter fails. As a result, already-bound parameters are reported as unbound in the exception message if any other parameter is missing.
  2. When _parameters was not initialized (_parameters == null), the block was skipped entirely, throwing an exception with an empty missing parameter list.
  3. Positional parameters (?) have a null name in sqlite3_bind_parameter_name, causing null entries in unboundParams and resulting in empty strings or dangling commas in the message.

This change:

  • Adds an internal Matches(string? name) method on SqliteParameter that matches both exact names and prefix-less names against @, $, and : prefixed statement parameters.
  • Uses Matches when collecting unbound parameters in SqliteCommand.
  • Populates unboundParams even when _parameters == null.
  • Falls back to "?" when sqlite3_bind_parameter_name returns null for positional parameters.
  • Adds unit tests covering prefix-less parameter error reporting, uninitialized parameters collections, and positional parameter error reporting.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The matching fallback can hide a genuinely unbound parameter when the supplied name already has a prefix.

1 open issue
What changed in this PR

This PR addresses #39370 by improving missing-parameter diagnostics in Microsoft.Data.Sqlite.

Changes:

  • Adds name matching for parameters supplied without a prefix.
  • Reports missing parameters when the collection is uninitialized and displays unnamed parameters as ?.
  • Adds regression tests for these diagnostic scenarios.
File Description
test/​Microsoft.Data.Sqlite.Tests/​SqliteCommandTest.cs Tests prefix-less, uninitialized-collection, and positional parameter diagnostics.
src/​Microsoft.Data.Sqlite.Core/​SqliteParameter.cs Adds parameter-name matching for missing-parameter detection.
src/​Microsoft.Data.Sqlite.Core/​SqliteCommand.cs Uses the matcher and populates previously empty diagnostic entries.

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The reviewed changes address the reported diagnostic issues and include regression coverage.

No open issues 🎉

1 resolved since last review

Review effort: Lite

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) October 26, 2026 23:23
@AndriySvyryd
AndriySvyryd merged commit bf50db4 into dotnet:main Oct 26, 2026
18 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqliteCommand falsely reports bound prefix-less parameters as unbound when another parameter is missing

3 participants