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.
11,305 questions
{count} votes

8 answers

Sort by: Most helpful
  1. Tacito Fieker 20 Reputation points
    2024-09-27T20:11:31.6566667+00:00

    Using:

    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

    all assemblies will be copied to the BIN folder.

    0 comments No comments

  2. Tacito Fieker 20 Reputation points
    2024-09-27T20:11:59.0366667+00:00

    Using:

    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

    all assemblies will be copied to the BIN folder.


  3. AlvaroGT 0 Reputation points
    2024-12-17T03:07:01.06+00:00

    the only solution is use since net 8
    dotnet publish -c Debug -r win-x64 --self-contained
    because just dotnet build it fails.
    Es increible que microsoft no pudo colocar esta solucion, hace perder el tiempo

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.