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.