Skip to content

SqlConnectionStringBuilder deviation in behavior for MultipleActiveResultSets #811

@aarondandy

Description

@aarondandy

Describe the bug

I am using SqlConnectionStringBuilder to generate my connection strings for different database connections at run-time. For some usages I have older System.Data code (DbUp in this case) that consumes these generated connection strings. When connection strings are generated by Microsoft.Data.SqlClient.SqlConnectionStringBuilder it adds extra spaces to MultipleActiveResultSets making it Multiple Active Result Sets which is not compatible with the older System.Data versions of the library.

Note: these stack traces are for System.Data.SqlClient.SqlConnectionStringBuilder and System.Data.SqlClient.SqlConnection to show the incompatibility

System.ArgumentException: Keyword not supported: 'multiple active result sets'.
   at System.Data.SqlClient.SqlConnectionStringBuilder.GetIndex(String keyword)
   at System.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
   at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnectionStringBuilder..ctor(String connectionString)
System.ArgumentException: Keyword not supported: 'multiple active result sets'.
   at System.Data.Common.DbConnectionOptions.ParseInternal(Dictionary`2 parsetable, String connectionString, Boolean buildChain, Dictionary`2 synonyms, Boolean firstKey)
   at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
   at System.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
   at System.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at System.Data.SqlClient.SqlConnection..ctor(String connectionString)

To reproduce

References:
System.Data.SqlClient 4.8.2
Microsoft.Data.SqlClient 2.1.0

using System;

namespace MarsConnectionString
{
    class Program
    {
        static void Main(string[] args)
        {
            const string connectionString = @"Data Source=localhost;PersistSecurityInfo=False;MultipleActiveResultSets=False;";

            var builderSystem = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
            Console.WriteLine(builderSystem.ConnectionString);
            // Expected: Data Source=localhost;Persist Security Info=False;MultipleActiveResultSets=False

            var builderMsft = new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
            Console.WriteLine(builderMsft.ConnectionString);
            // Actual:   Data Source=localhost;Persist Security Info=False;Multiple Active Result Sets=False

            // OK
            new System.Data.SqlClient.SqlConnectionStringBuilder(builderSystem.ConnectionString);

            // OK
            new Microsoft.Data.SqlClient.SqlConnectionStringBuilder(builderMsft.ConnectionString);


            // exception: System.ArgumentException: Keyword not supported: 'multiple active result sets'.
            try
            {
                // System.Data can't handle a connection string made by Microsoft.Data
                new System.Data.SqlClient.SqlConnectionStringBuilder(builderMsft.ConnectionString);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            // exception: System.ArgumentException: 'Keyword not supported: 'multiple active result sets'.'
            try
            {
                // System.Data can't handle a connection string made by Microsoft.Data
                using var _ = new System.Data.SqlClient.SqlConnection(builderMsft.ConnectionString);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

Expected behavior

I expect Microsoft.Data.SqlClient.SqlConnectionStringBuilder to produce a connection string where "MultipleActiveResultSets" does not have extra spaces added to it

Further technical details

Microsoft.Data.SqlClient version: 2.1.0
.NET target: netcoreapp3.1
SQL Server version: N/A
Operating system: Reproduced on Windows 19041.630 and Azure PaaS Linux Hosts

Additional context

Possibly related to #654

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions