Not able to get Microsoft.Data.SqlClient working on Powershell under Linux
See original GitHub issueHI,
Right now we are using System.Data.SqlClient to validate sql connection strings via powershell but we realized this library doesn’t support this additional connection param i.e. Column Encryption Setting=enabled
with this we are always running into ,

We even tried “SqlServer” library, but the error message is still the same
On debugging why it’s the case, we found this article which state Microsoft.Data.SqlClient is the way to go, https://devblogs.microsoft.com/dotnet/introducing-the-new-microsoftdatasqlclient/
Now problem with Microsoft.Data.SqlClient is , from last 2 days two of us are trying to figure out how to even get this library working on powershell running inside a RHEL container
We installed library like this,
Register-PackageSource -provider NuGet -name nugetRepository -location https://www.nuget.org/api/v2
Install-Package Microsoft.Data.SqlClient -Provider nuget
Install-Package Microsoft.Data.SqlClient.SNI -Provider nuget
and then we are trying to validate connection string like this and irrespective of whatever DLL path we pass, we are always running into the same error i.e,
MethodInvocationException: Exception calling “Open” with “0” argument(s): “The type initializer for ‘Microsoft.Data.SqlClient.SqlAuthenticationProviderManager’ threw an exception.”
PS /> Add-Type -AssemblyName System.Data
PS /> Add-Type -Path ('./usr/local/share/PackageManagement/NuGet/Packages/Microsoft.Data.SqlClient.3.0.1/runtimes/win/lib/netcoreapp2.1/Microsoft.Data.SqlClient.dll') -ReferencedAssemblies Microsoft.Data.SqlClient.SNI
PS />
PS /> Add-Type -Path ('/usr/local/share/PackageManagement/NuGet/Packages/Microsoft.Data.SqlClient.3.0.1/runtimes/win/lib/netcoreapp2.1/Microsoft.Data.SqlClient.dll') -ReferencedAssemblies Microsoft.Data.SqlClient.SNI
PS />
PS /> $conn = new-object Microsoft.Data.SqlClient.SqlConnection
PS /> $conn.connectionstring = "Server=tcp:xxx-xxxx-sql.database.windows.net,1433;Initial Catalog=Automation_Platform;Persist Security Info=False;User Id=testadmin@xxxxxx-sql.database.windows.net;Password=xxx%3k@xx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Max Pool Size=100;Column Encryption Setting=enabled;"
PS />
PS /> Write-Verbose $conn.connectionstring
PS />
PS /> Add-Type -path "./usr/local/share/PackageManagement/NuGet/Packages/Microsoft.Data.SqlClient.3.0.1/runtimes/win/lib/netcoreapp2.1/Microsoft.Data.SqlClient.dll"
PS />
PS /> $con = New-Object Microsoft.Data.SqlClient.SqlConnection
PS /> $con.ConnectionString = "Server=xxxx;user=xxxxx;password=xxxxx"
PS />
PS /> $con.Open()
MethodInvocationException: Exception calling "Open" with "0" argument(s): "The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception."
Can you please help us understand how do we fix this or share us an working example which is powershell and linux based
we tried all this already and nothing works, https://github.com/dotnet/SqlClient/issues/623 https://gist.github.com/MartinHBA/86c6014175758a07b09fa7bb76ba8e27
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:12 (9 by maintainers)
Top Related StackOverflow Question
@rajivml I have spent sometime and I figured out the issue. Keep in mind that we, developers of SqlClient, are not PowerShell experts/developers.
The issue seems to come from the fact that PowerShell is not handling Nugets packages easily, look at https://github.com/PowerShell/PowerShell/issues/6724 Previous issues were addressed at issue #161, but non had the same issue as yours. Wrapping the command in
try/catchblock , as shown below, helped me to get to the source of the issue and was able to open a connection successfullyMicrosoft.Identity.Client Version 4.22.0 needs to be loaded.
Remember
System.Data.SqlClientdid not have these issues as it was shipped along with the framework and it had all needed libraries available to itself, whereasMicrosoft.Data.SqlClientdoes not have that privilege and some libraries need to be loaded before hand for PowerShell to work correctly. To get a better understanding run:and you will get a list of loaded libraries.
Thanks a lot guys @Wraith2 @JRahnama and @cheenamalhotra. This worked, you can resolve the ticket