I am looking for a robust method for checking in C# if a given SQL connection string explicitly specifies a certain parameter, e.g. "Encrypt=...
". I tried parsing the string using SqlConnectionStringBuilder
, expecting that ContainsKey()
would tell me whether a key was specified but it's not working:
System.Data.SqlClient.SqlConnectionStringBuilder x =
new("Data Source=.;Initial Catalog=myDb;Integrated Security=True");
bool encryptSpecified = x.ContainsKey("Encrypt"); // returns true :(
Clarification
I should have clarified why I need to know whether the Encrypt
parameter was specified explicitly. In the current version of Microsoft.Data.SqlClient
, the default value of Encrypt
is true
, but before (in Sql.Data.SqlClient
) it was false
. Therefore, to ensure backwards compatibility in an application after upgrading to Microsoft.Data.SqlClient
, I want to set the Encrypt
parameter to false
unless the user explicitly specified a value for it.
Solution
[Based on discussion with @Charlieface]
// important: *not* Microsoft.Data.SqlClient.SqlConnectionStringBuilder!
System.Data.SqlClient.SqlConnectionStringBuilder scsb =
new(connectionString);
if (!scsb.Encrypted) scsb.Encrypted = false; // this will explicitly set Encrypt
connectionString = scsb.ConnectionString;