SqlClient shrink SqlParameter and fix 34414 by Wraith2 · Pull Request #35549 · dotnet/corefx (original) (raw)

fixes https://github.com/dotnet/corefx/issues/34414

When using SqlCommandBuilder.DeriveParameters to identify the parameters of a stored procedure any table valued parameters will have their type name incorrectly constructed including the database name part.

This bug has been outstanding in SqlClient for quite some time so the direct fix would break user code where they rely on the incorrect parameter format to identify and fix the problem themselves. Rather than produce a correct name the name is still incorrectly generated but parameter is flagged as having a known incorrect name. If the user assigns to the typename the flag is cleared and we assume they've fixed it themselves. Later in the execution process when the user can no longer alter the parameter before execution the flag is checked and if still set the type name is auto corrected. Discussion of this method can be found on the original PR by @ssa3512

I have added a new test UDTParams_DeriveParameters which verifies that the auto correction behaviour takes place. This test requires a new table values type and sproc in the UDT test database so the test database script has been updated. The functional and manual tests all pass.

While investigating and implementing these changes I have compressed the SqlParameter field definitions because it was being quite wasteful about storing multiple parameters. Multiple boolean fields have been combined into flags, the rarely used xml fields have been moved into the same lazy initialized object as used in SqlCommand. I realize that this adds noise to the fix diffs but it seemed wasteful not to take the opportunity while I was working on the type. The patterns used are already used in SqlCommand so they're low risk imo.

/cc all the usual people @afsanehr @tarikulsabbir, @David-Engel and @ssa3512 @saurabh500 from the original PR