Microsoft.Data.SQLClient 5.2.0 fails to work with .Net 8

Moshe Yalovsky 5 Reputation points
2024-05-04T22:39:27.66+00:00

I am having issues creating a Class library with C# and .net 8 that uses Microsoft.Data.SQLClient. version 5.2.0 which states that it will run on .net 8 platform.

Here is the code for the class library

using System.Data;
using System.Reflection.Metadata.Ecma335;
using Microsoft.Data.SqlClient;

namespace SQLClientDemo
{
   public class ConnectionString
   {
      
      public static string OUConnectionString(int OUID)
      {
         string returnValue = "";
         try
         {

            string connectionString = "Server = ****;Database=AchEnterprise;User ID = sa; Password =****;";
            using SqlConnection conn = new SqlConnection(connectionString);

            conn.Open();
            SqlCommand cmdGetOUConStrings = new SqlCommand("SPN_OU_GetAll", conn);
            cmdGetOUConStrings.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr = cmdGetOUConStrings.ExecuteReader();

            while (rdr.Read())
            {
               if (rdr.GetInt32(0)==OUID) {
                  if (!rdr.IsDBNull(4))
                  {
                     returnValue = rdr.GetString(4);
                  }
               }
               break;
            }
         }
         catch (Exception Ex)
         {
            throw Ex;
         }
         return returnValue;
      }
     
    
   }
}


I created a simple Console application to test the class library

// See https://aka.ms/new-console-template for more information

Console.WriteLine("Hello, World!");

string OUConnectionString = "";
OUConnectionString = SQLClientDemo.ConnectionString.OUConnectionString(1004);
Console.WriteLine(OUConnectionString);
Console.ReadLine();

On this line

OUConnectionString = SQLClientDemo.ConnectionString.OUConnectionString(1004);

I got the following error

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
  Source=SQLClientDemo
  StackTrace:
   at SQLClientDemo.ConnectionString.OUConnectionString(Int32 OUID) in C:\Dev\Achieve\2022Projects\SQLClientDemo\ConnectionString.cs:line 40
   at Program.<Main>$(String[] args) in C:\Users\myalo\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 6

Here are the SQLClientDemo and Console app project files

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
  </ItemGroup>

</Project>


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Achieve.Common">
      <HintPath>bin\Debug\net8.0\Achieve.Common.dll</HintPath>
    </Reference>
    <Reference Include="SQLClientDemo">
      <HintPath>..\..\..\..\..\..\Dev\Achieve\2022Projects\SQLClientDemo\bin\Debug\net8.0\SQLClientDemo.dll</HintPath>
    </Reference>
  </ItemGroup>

</Project>


I tried copying the SQLClientDemo.dll to the bin directory of the console app and reference it there. Then I got a different error - Microsoft.Data.SqlClient is not supported on this platform.

System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Microsoft.Data.SqlClient is not supported on this platform.
  Source=SQLClientDemo
  StackTrace:
   at SQLClientDemo.ConnectionString.OUConnectionString(Int32 OUID) in C:\Dev\Achieve\2022Projects\SQLClientDemo\ConnectionString.cs:line 37
   at Program.<Main>$(String[] args) in C:\Users\myalo\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line 6

I spent a few days trying to resolve this issue and would appreciate any help, insight or suggestion on how to resolve it.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,366 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 35,556 Reputation points Microsoft Vendor
    2024-05-06T10:58:08.8633333+00:00

    Hi @Moshe Yalovsky , Welcome to Microsoft Q&A,

    Your problem may be caused by two different issues:

    1. File not found exception:
      
          System.IO.FileNotFoundException
      
          HResult=0x80070002
      
          Message=Could not load file or assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'. The system cannot find the file specified.
      
      
    2. The platform does not support exceptions:
      
          System.PlatformNotSupportedException
      
          HResult=0x80131539
      
          Message=Microsoft.Data.SqlClient is not supported on this platform.
      
      

    To resolve these two issues, you can follow these steps:

    Solve file not found exception

    1. In the Console application project, ensure that the Microsoft.Data.SqlClient package is installed correctly. You can check in your project files (*.csproj) for the following:
      
          <ItemGroup>
      
            <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.0" />
      
          </ItemGroup>
      
      
    2. Make sure that the Microsoft.Data.SqlClient.dll file is located in the bin directory.

    Solve the problem that the platform does not support the exception

    1. Make sure your application is running in 64-bit mode. You can check and change your project's target platform by following these steps:
      • In Visual Studio, right-click the project and select Properties.
      • Under the Build tab, make sure Target Platform is set to x64.
    2. Add the following properties to the project file (*.csproj) to specify the target platform:
      
          <PropertyGroup>
      
            <OutputType>Exe</OutputType>
      
            <TargetFramework>net8.0</TargetFramework>
      
            <ImplicitUsings>enable</ImplicitUsings>
      
            <Nullable>enable</Nullable>
      
            <UseWPF>true</UseWPF> <!-- If your project uses WPF -->
      
            <Platforms>x64</Platforms>
      
          </PropertyGroup>
      
      

    With the above steps, you should be able to resolve both exceptions. If the problem persists, please let me know and I will try my best to help you resolve it.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bruce (SqlWork.com) 57,731 Reputation points
    2024-05-06T16:46:55.05+00:00

    you should use "dotnet publish" to build the folder to run the console app from. Microsoft.Data.SqlClient has a lot of dependent dll's. I build a quick app on my Mac using Microsoft.Data.SqlClient 5.2.0. here are the dlls

    >ls *.dll
    Azure.Core.dll
    Azure.Identity.dll
    Microsoft.Bcl.AsyncInterfaces.dll
    Microsoft.Data.SqlClient.dll
    Microsoft.Extensions.Configuration.Abstractions.dll
    Microsoft.Extensions.Configuration.Binder.dll
    Microsoft.Extensions.Configuration.EnvironmentVariables.dll
    Microsoft.Extensions.Configuration.FileExtensions.dll
    Microsoft.Extensions.Configuration.Json.dll
    Microsoft.Extensions.Configuration.dll
    Microsoft.Extensions.FileProviders.Abstractions.dll
    Microsoft.Extensions.FileProviders.Physical.dll
    Microsoft.Extensions.FileSystemGlobbing.dll
    Microsoft.Extensions.Primitives.dll
    Microsoft.Identity.Client.Extensions.Msal.dll
    Microsoft.Identity.Client.dll
    Microsoft.IdentityModel.Abstractions.dll
    Microsoft.IdentityModel.JsonWebTokens.dll
    Microsoft.IdentityModel.Logging.dll
    Microsoft.IdentityModel.Protocols.OpenIdConnect.dll
    Microsoft.IdentityModel.Protocols.dll
    Microsoft.IdentityModel.Tokens.dll
    Microsoft.SqlServer.Server.dll
    System.Configuration.ConfigurationManager.dll
    System.Diagnostics.EventLog.dll
    System.IdentityModel.Tokens.Jwt.dll
    System.Memory.Data.dll
    System.Runtime.Caching.dll
    System.Security.Cryptography.ProtectedData.dll
    sqltest.dll
    

  3. Karen Payne MVP 35,196 Reputation points
    2024-05-07T10:19:38.99+00:00

    The following is on Windows not Mac but should be the same.

    The following is incorrect.

    <ItemGroup>
    <Reference Include="Achieve.Common">
      <HintPath>bin\Debug\net8.0\Achieve.Common.dll</HintPath>
    </Reference>
    <Reference Include="SQLClientDemo">
      <HintPath>..\..\..\..\..\..\Dev\Achieve\2022Projects\SQLClientDemo\bin\Debug\net8.0\SQLClientDemo.dll</HintPath>
    </Reference>
    </ItemGroup>
    

    If SQLClientDemo is in a different VS solution, consider making a copy and add it to the current solution for test purposes or create a local NuGet package and add it that way.

    After adding project references to the main assembly you should get something like this

    <ItemGroup>
      <ProjectReference Include="..\CommonLibrary\CommonLibrary.csproj" />
      <ProjectReference Include="..\SqlServerLibrary\SqlServerLibrary.csproj" />
    </ItemGroup>
    

    Which copied the two (in this example) compiled DLL to the main assemblies debug or release folder.

    If you want to try the following which is a NET8 app using Microsoft.Data.SqlClient 5.2.0, see the following which is a stored procedure viewer. Clone the repository, in the main project, adjust the server name and run it. If it works than we know the problem is with your project. Lastly if it does not work, tried downgrading to one version down and adjust your project references as indicated above.

    0 comments No comments

  4. AlvaroGT 0 Reputation points
    2024-05-16T03:13:14.2866667+00:00

    Have you find any solution for these?. Same error. and the bug its the same as https://github.com/dotnet/SqlClient/issues/1945 they solved for wpf ,winforms but not for classlibrary