Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Data.SqlClient is not supported on this platform in .NET 6.0 C# library

When I run the C# .NET 6.0 library from my console program, I havn't problem with System.Data.SqlClient or Microsoft.Data.SqlClient i tested with two DLL , but if I call the dll with another .exe program in .NET 6.0, I get an exception for two dll System.Data.SqlClient` or Microsoft.Data.SqlClient and also oledb dll.

System.PlatformNotSupportedException: System.Data.SqlClient is not supported on this platform

My code:

using (var connection = new SqlConnection(connectionString))
{
    using (var cmd = new SqlCommand(queryString, connection))
    {
        cmd.Connection = connection;
        connection.Open();

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                return true;
            }                               
        }
    }
} 

My CSProj file

Exeption

like image 807
hbib Avatar asked Oct 25 '25 02:10

hbib


1 Answers

For me, adding the target OS platform in the Server.Proj.cs file fixed the issue.

<RuntimeIdentifier>ubuntu.22.04-x64</RuntimeIdentifier>

Use "dotnet --info" in terminal to find you target OS platform ("ubuntu.22.04-x64" for me), then add it to your proj.cs files.

Runtime Environment: OS Name: linuxmint OS Version: 21.1 OS Platform: Linux RID: ubuntu.22.04-x64 Base Path: /usr/lib/dotnet/sdk/7.0.109/

<PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <RuntimeIdentifier>ubuntu.22.04-x64</RuntimeIdentifier>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

Basically, from what I gathered after a few hours of research...

Microsoft.Data.SqlClient has multiple DLL's for different target platforms. For some reason, the incorrect one is being loaded. You can specify explicity which dll to load with the method I provided.

like image 154
Tree3708 Avatar answered Oct 27 '25 15:10

Tree3708



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!