Skip to content

Setting MySqlParameter.Value to a StringBuilder instance is not supported #975

Closed

Description

@appel1

Is this intentional? At least the Migrating from Connector/NET documentation should be updated to mention this, and other such breaking changes.

With Connector/NET this works.

StringBuilder sb = new StringBuilder();
sb.Append("foo");
command.Parameters.Add(new MySqlParameter { ParameterName = "ColumnName", Value = sb });

With MySqlConnector 1.3.3 it fails with this exception:

System.NotSupportedException: Parameter type StringBuilder is not supported; see https://fl.vu/mysql-param-type. Value: foo
   at MySqlConnector.MySqlParameter.AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions options) in /_/src/MySqlConnector/MySqlParameter.cs:line 435
   at MySqlConnector.Core.StatementPreparer.ParameterSqlParser.DoAppendParameter(Int32 parameterIndex, Int32 textIndex, Int32 textLength) in /_/src/MySqlConnector/Core/StatementPreparer.cs:line 87
   at MySqlConnector.Core.StatementPreparer.ParameterSqlParser.OnNamedParameter(Int32 index, Int32 length) in /_/src/MySqlConnector/Core/StatementPreparer.cs:line 75
   at MySqlConnector.Core.SqlParser.Parse(String sql) in /_/src/MySqlConnector/Core/SqlParser.cs:line 198
   at MySqlConnector.Core.StatementPreparer.ParseAndBindParameters(ByteBufferWriter writer) in /_/src/MySqlConnector/Core/StatementPreparer.cs:line 37
   at MySqlConnector.Core.SingleCommandPayloadCreator.WriteCommand(IMySqlCommand command, ByteBufferWriter writer) in /_/src/MySqlConnector/Core/SingleCommandPayloadCreator.cs:line 206
   at MySqlConnector.Core.SingleCommandPayloadCreator.WriteQueryPayload(IMySqlCommand command, IDictionary`2 cachedProcedures, ByteBufferWriter writer) in /_/src/MySqlConnector/Core/SingleCommandPayloadCreator.cs:line 59
   at MySqlConnector.Core.SingleCommandPayloadCreator.WriteQueryCommand(CommandListPosition& commandListPosition, IDictionary`2 cachedProcedures, ByteBufferWriter writer) in /_/src/MySqlConnector/Core/SingleCommandPayloadCreator.cs:line 32
   at MySqlConnector.Core.CommandExecutor.ExecuteReaderAsync(IReadOnlyList`1 commands, ICommandPayloadCreator payloadCreator, CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/Core/CommandExecutor.cs:line 49
   at MySqlConnector.MySqlCommand.ExecuteReaderAsync(CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken) in /_/src/MySqlConnector/MySqlCommand.cs:line 314
   at MySqlConnector.MySqlCommand.ExecuteReader() in /_/src/MySqlConnector/MySqlCommand.cs:line 106
   at ConsoleApp12.Program.Main(String[] args) in C:\Users\johan\source\repos\ConsoleApp12\ConsoleApp12\Program.cs:line 23

Activity

  1. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    At least the Migrating from Connector/NET documentation should be updated to mention this, and other such breaking changes.

    The documentation linked in the exception message does already cover this: https://fl.vu/mysql-param-type aka https://mysqlconnector.net/troubleshooting/parameter-types/. It's also linked here in the "Migrating" docs: https://mysqlconnector.net/tutorials/migrating-from-connector-net/#mysqlparameter

    Is this intentional?

    In the sense that everything not supported is prohibited, yes.

    Since StringBuilder has a well-known and safe ToString() method, this seems like a reasonable addition.

  2. appel1 commented on Apr 23, 2021

    @appel1
    Author

    I looked for any mention of types with support removed compared to Connector/NET. Should've read more carefully. Especially the link in the error message that made things clear. Sorry.

  3. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    I looked for any mention of types with support removed compared to Connector/NET.

    The list of types is basically "all of them" because Connector/NET will call .ToString() on anything (which is most often a bug, because ToString usually just returns the type name).

  4. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    In .NET 5 support for StringBuilder could be implemented using GetChunks, which should be much more efficient than ToString.

  5. added a commit that references this issue on Apr 23, 2021
    9f804ca
  6. bgrainger commented on Apr 23, 2021

    @bgrainger
    Member

    Fixed in 1.3.5.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions