2

I would like to use the Microsoft.Data.SqlClient namespace/ objects in a PowerShell script. Two (click1, click2) Github posts provide a way to load the correct dll's, but the solutions don't seem to work anymore.

E.g. result of the first solution while copying the next files:

(Packages copied from .nuget/packages folder)

  • Microsoft.Data.SqlClient.dll
  • Microsoft.Data.SqlClient.SNI.x64.dll
  • Microsoft.Identity.Client.dll

Result: Could not load file or assembly 'System.Runtime, Version=6.0.0.0'

In addition, I've tried to create a Dummy Console App -> Added the Microsoft.Data.SqlClient Nuget package -> built the project and copied all dll's to the same folder as the PS script. As soon as I start the script (using the 'Add-Type -Path' construction), it results in errors, such as 'couldn't load file or assembly - wrong version...' (this is strange, because the folder contains all dll's...)

Could you provide an alternative solution/ steps in order to use the described package in a PS script?

Odrai
  • 2,163
  • 2
  • 31
  • 62
  • "but the solutions don't seem to work anymore." - do you want to tell us what happens when you try to use the code, or would you rather wait for someone to guess it? :) – Mathias R. Jessen Jan 25 '23 at 14:01
  • @MathiasR.Jessen I've added the result of the first solution, 'Could not load file or assembly 'System.Runtime, Version=6.0.0.0' – Odrai Jan 25 '23 at 14:08
  • And which version of PowerShell are you using? – Mathias R. Jessen Jan 25 '23 at 14:10
  • PowerShell 5.1.19041.2364 (and the .NET 6 SDK has been installed) – Odrai Jan 25 '23 at 14:11
  • 2
    PowerShell 5.1 is based on .NET Framework; .NET 6 is .NET Core. So that's not going to work; you'd need PowerShell 6+ for that. You could conceivably install the assembly targeting .NET Framework from the NuGet package for PowerShell 5.1. All in all it seems far more trouble than it's worth, though (compared to building an app and running that from PowerShell -- you're going to be copying binaries anyway, after all). – Jeroen Mostert Jan 25 '23 at 14:20
  • Why use the dlls. You can connect using : $connection New-Object System.Data.SqlClient.SqlConnection $connString; $connection.Open() – jdweng Jan 25 '23 at 14:34
  • @jdweng Currently the System.Data.SqlClient package is used, but Microsoft.Data.SqlClient is the new (supported) package. This is the reason we would like to 'upgrade'. – Odrai Jan 25 '23 at 14:54
  • Using New-Object is using SQlClient and will take the version that is compatible with your PS. The code should be exactly the same. – jdweng Jan 25 '23 at 14:58

0 Answers0