When I try to use SqlClient when Globalization.Invariant is true., It fails to open a sql connection (tried with mssql localdb)
If you are seeing an exception, include the full exceptions details (message and stack trace).
Exception message: System.NotSupportedException: Globalization Invariant Mode is not supported.
Stack trace:
System.NotSupportedException
HResult=0x80131515
Message=Globalization Invariant Mode is not supported.
Source=Microsoft.Data.SqlClient
StackTrace:
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open(SqlConnectionOverrides overrides)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at ConsoleApp1.Program.Main(String[] args) in D:\Source Code\dotNet\PracticeProject01\ConsoleApp1\Program.cs:line 16
Here is the complete code, It uses a mssql local db as a database to connect.
// ConsoleApp1.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!--You need to rebuild the project after adding/removing the following line, for it to take effect-->
<RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.0.0" />
</ItemGroup>
</Project>
```c#
// Program.cs
using System;
using Microsoft.Data.SqlClient;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// MSSQLLocalDB is custom local db on my machine, it may not exist in your machine
var cStr = "Server=(localdb)\MSSQLLocalDB;Database=master;Integrated Security=true";
var sqlString = "Select 1";
using (var conn = new SqlConnection(cStr)) {
conn.Open();
var command = conn.CreateCommand();
command.CommandText = sqlString;
var res = command.ExecuteScalar().ToString();
Console.WriteLine("Response is: {0}", res);
}
}
}
}
```
I expect the connection to succed and respond with "1" even when "Globalization Invariant Mode" is on.
Microsoft.Data.SqlClient version: v2.0.0
.NET target: .NET Core v3.1
SQL Server version: . SQL Server 2017
Operating system: Windows 10
This is not a bug. It is by design. SqlClient is dependent on the Globalization libraries and can throw unexpected errors when it is not present. Thus the exception, "Globalization Invariant Mode is not supported."
@David-Engel thanks for quick response. is this documented somewhere?
It is documented in the error message.
@ErikEJ I mean, like acutal documentation. which I can link to as a resource to others, as of right now the best source for this info is this issue and #220 which is far from ideal.
Should it have been 07-23 ??
No, I wrote it two months ago to get my blog up and running and I haven't got around to putting anything else on there yet. It's not documentation per-se but it's better than trying to find the related information in various issues.
Great, shared on Twitter.
Closing this issue as it is a expected behavior. and will use @Wraith2 article as a reference
Most helpful comment
https://blog.wraithware.org/2020/05/23/SqlClient-and-Globalization-Invariant-mode.html