dotnet / dotnet/SqlClient

SqlCommand: EnableOptimizedParameterBinding throws error when used together with Prepare

Open
#3,835 5 comments 0 reactions 1 assignee Claimed by @mdaigle View on GitHub
Area\Documentation
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

When calling ``Prepare`` together with `` EnableOptimizedParameterBinding``, ``TdsExecuteRPC`` will throw an exception
``SQL.ParameterDirectionInvalidForOptimizedBinding`` because it seems that the Prepare-Method injects addional parameters into the command, that seem to not set the direction correctly.
When debugging the exception i can see it sets the ``sqlRPC`` to ``sp_prepexec`` and uses 3 additional parameters.

### To reproduce

Use the official Prepare-Example from here [Link](https://learn.microsoft.com/de-de/dotnet/api/system.data.sqlclient.sqlcommand.prepare?view=netframework-4.8.1&viewFallbackFrom=net-10.0)
and add ``EnableOptimizedParameterBinding = true;``

### Expected behavior
No exception or a more meaningful one.
I could not find any documentation regarding the issue.

### Further technical details
Microsoft.Data.SqlClient 6.1.3 on a net 10 Visual Studio 2026

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.