diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs index 660b5934e0..81bfc9084e 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs @@ -1812,7 +1812,35 @@ private bool TryOpen(TaskCompletionSource retry, SqlConnec // are not present. Throwing on open with a meaningful message helps identify the issue. if (_cultureCheckState == CultureCheckState.Unknown) { - _cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard; + // check if invariant state has been set by appcontext switch directly + if (AppContext.TryGetSwitch("System.Globalization.Invariant", out bool isEnabled) && isEnabled) + { + _cultureCheckState = CultureCheckState.Invariant; + } + else + { + // check if invariant state has been set through environment variables + string envValue = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT"); + if (string.Equals(envValue, bool.TrueString, StringComparison.OrdinalIgnoreCase) || string.Equals(envValue, "1", StringComparison.OrdinalIgnoreCase)) + { + _cultureCheckState = CultureCheckState.Invariant; + } + else + { + // if it hasn't been manually set it could still apply if the os doesn't have + // icu libs installed or is a native binary with icu support trimmed away + // netcore 3.1 to net5 do not throw in attempting to create en-us in inariant mode + // net6 and greater will throw so catch and infer invariant mode from the exception + try + { + _cultureCheckState = CultureInfo.GetCultureInfo("en-US").EnglishName.Contains("Invariant") ? CultureCheckState.Invariant : CultureCheckState.Standard; + } + catch (CultureNotFoundException) + { + _cultureCheckState = CultureCheckState.Invariant; + } + } + } } if (_cultureCheckState == CultureCheckState.Invariant) {