Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
shueybubbles opened this issue Apr 24, 2023 · 0 comments · Fixed by #2018
Closed

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

shueybubbles opened this issue Apr 24, 2023 · 0 comments · Fixed by #2018
Labels
🐛 Bug! Something isn't right !

Comments

@shueybubbles
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug! Something isn't right !
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants