2

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

hbib
  • 21
  • 1
  • 1
  • 5
  • 2
    And what's the platform? You probably need to use Microsoft.Data.SqlClient instead. It's cross-platform where the other is, I think, Windows-specific. – John Jun 09 '22 at 12:06
  • 1
    We won't know how to make the changes to your existing code base without seeing your original code. Please post a [mre], and fully explain what needs to be modified. – gunr2171 Jun 09 '22 at 12:08
  • @John i use framework .net 6.0 to build Library for Windows x64 – hbib Jun 09 '22 at 12:08
  • I have installed Microsoft.Data.SqlClient 4.1.0 the last version in nuget – hbib Jun 09 '22 at 12:09
  • 2
    No one should have to read the comments to understand the question. Don't add information in comments that should have been in the question from the outset. Edit the question and provide ALL the relevant information. You should spend some time in the Help Center to learn how to write a good question. – John Jun 09 '22 at 12:12
  • 1
    [This](https://stackoverflow.com/questions/47718924/fixing-platformnotsupportedexception-when-referencing-system-data-sqlclient-from) seems relevant and suggests that using the other package will work. You should have found that for yourself before posting a question at all, given that it was the first result when I searched the web for "sqlclient PlatformNotSupportedException". You should never post a question here without having searched thoroughly first. – John Jun 09 '22 at 12:15
  • @John I did a search and I tried several proposed solutions but I still have the same problem. – hbib Jun 09 '22 at 12:17
  • Are you saying, without actually saying, that the same issue exists when using Microsoft.Data.SqlClient? – John Jun 09 '22 at 12:22
  • 3
    I guess that even though you have installed the Microsoft.Data.SqlClient package, you are still using the System.Data.SqlClient namespace. – Alexander Petrov Jun 09 '22 at 12:24
  • I 'am using Microsoft.Data.SqlClient 4.1.0 and ihave the same exeption and i havn' t System.data.sSqlClent System.PlatformNotSupportedException : 'Microsoft.Data.SqlClient is not supported on this platform.' – hbib Jun 09 '22 at 12:35
  • [earlier dup](https://stackoverflow.com/questions/72556321/exception-when-using-microsoft-data-sqlclient-ado-net-with-project-net-core-6-0) – SMor Jun 09 '22 at 12:37
  • So have you searched all source files in your library and your .NET 6.0 console program (both of them) for any references to `System.Data.SqlClient`? – AlwaysLearning Jun 09 '22 at 13:22
  • I checked and I don't have System.Data.SqlClient neither has my console nor my library project – hbib Jun 09 '22 at 14:07
  • I tired to use System.Data.OleDb but i have same problem – hbib Jun 09 '22 at 15:41
  • Can you post the .csproj file so we can see what packages are being referenced? – brent.reynolds Jun 09 '22 at 18:27
  • @brent.reynolds I added my csproj . – hbib Jun 09 '22 at 22:28
  • Nothing in there is dependent on `System.Data.SqlClient`, unless it's the redacted package pulling it in. – brent.reynolds Jun 09 '22 at 22:47
  • @brent.reynolds first time i used System.Data.SqlClient and i change to Microsoft.Data.SqlClient 4.1.0 – hbib Jun 10 '22 at 00:31
  • @brent.reynolds I added my exception Screenshot – hbib Jun 10 '22 at 00:33
  • I change from System.Data.SqlClient to Microsoft.Data.SqlClient and to System.Data.OleDb and i have same exeption PlatformNotSupportedException is not supported on this platform – hbib Jun 10 '22 at 00:34
  • You can see from your screenshot that the exception is being thrown from `iYDA.Agent.Validation(Cassiopae).dll` Whatever that is, it is probably where your dependency on `System.Data.SqlClient` is coming from. – brent.reynolds Jun 10 '22 at 15:16

1 Answers1

0

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.

Tree3708
  • 33
  • 4