Skip to content

SqlConnectionStringBuilder property indexer doesn't support non-string values #2015

Closed
@shueybubbles

Description

@shueybubbles

This code:

// See https://aka.ms/new-console-template for more information
using Microsoft.Data.SqlClient;

var conn = new SqlConnectionStringBuilder();
try
{
    Console.WriteLine("Set Encrypt to SqlConnectionEncryptionOption type");
    conn["Encrypt"] = SqlConnectionEncryptOption.Strict;
}
catch (Exception e)
{
    Console.WriteLine(e);
}
try
{
    Console.WriteLine("Set Encrypt to boolean false");
    conn["Encrypt"] = false;
}
catch (Exception e)
{
    Console.WriteLine(e);
}
Console.WriteLine(conn.ConnectionString);

Prints the following:

Set Encrypt to SqlConnectionEncryptionOption type
System.ArgumentException: Invalid value for key 'Encrypt'.
   at Microsoft.Data.Common.DbConnectionStringBuilderUtil.ConvertToSqlConnectionEncryptOption(String keyword, Object value)
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConvertToSqlConnectionEncryptOption(String keyword, Object value)
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
   at Program.<Main>$(String[] args) in E:\git\sqlenc\Program.cs:line 8
Set Encrypt to boolean false
System.ArgumentException: Invalid value for key 'Encrypt'.
   at Microsoft.Data.Common.DbConnectionStringBuilderUtil.ConvertToSqlConnectionEncryptOption(String keyword, Object value)
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.ConvertToSqlConnectionEncryptOption(String keyword, Object value)
   at Microsoft.Data.SqlClient.SqlConnectionStringBuilder.set_Item(String keyword, Object value)
   at Program.<Main>$(String[] args) in E:\git\sqlenc\Program.cs:line 17

Given that there's client code floating around that uses the indexer to save copies of the properties in a dictionary then writes them back using the indexer in order to capture parameters without knowing their names and types ahead of time, at minimum the indexer should allow one to set Encrypt to an instance of SqlConnectionEncryptionOption. It'd be nice if it supported a bool setter too.

Otherwise in SSMS 20 we'll have to modify the designer code to special case this option to convert the stored value to a string.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug! 🐛Issues that are bugs in the drivers we maintain.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions