Repository navigation
Closed
Labels
External 🔗Issue is in an external component
Description
Is your feature request related to a problem? Please describe.
I am a PowerShell developer and do not know how to use this SQL Client. I cannot find any docs that help.
Describe the solution you'd like
- A code block in the official docs, starting with
Add-Type -Paththat at least includes a query to a remote server. - Let us know which of the DLLs we have to use - the one in lib or the one in runtime (a buddy just told me lib for our purposes).
- Highlight some differentiations or gotchas that we have to consider as a community that doesn't entirely understand DLLs.
- Can both System and Microsoft be used? If so, please add that to the code block with a comment like
# Pre-SQL Server 2008or# For Azure
I would like PowerShell to be given as much love as C# in the official docs. Our community uses the heck out of SqlClient.
Describe alternatives you've considered
I tried it myself to no avail.
Add-Type -Path C:\temp\sqlclient\lib\netstandard2.0\System.Configuration.ConfigurationManager.dll
Add-Type -Path C:\temp\sqlclient\lib\netstandard2.0\Microsoft.Data.SqlClient.dll
$azureconnstring = 'Data Source=TCP:psdbatools.database.windows.net,1433;Initial Catalog=dbatools;User [email protected];Password=XYZ;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;Application Name="dbatools PowerShell module - dbatools.io";Authentication="Active Directory Password"'
New-Object Microsoft.Data.SqlClient.SqlConnection $azureconnstring
System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "1" argument(s): "Microsoft.Data.SqlClient is not supported on this platform." --->
System.PlatformNotSupportedException: Microsoft.Data.SqlClient is not supported on this platform.
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
--- End of inner exception stack trace ---
at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArguments)
at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, Object[] arguments)
at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type, ConstructorInfo[] constructors, Object[] args)I also tried in Core with the same error
Add-Type -Path C:\temp\sqlclient\lib\netcoreapp2.1\Microsoft.Data.SqlClient.dll
$azureconnstring = 'Data Source=TCP:psdbatools.database.windows.net,1433;Initial Catalog=dbatools;User [email protected];Password=XYZ;MultipleActiveResultSets=False;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;Application Name="dbatools PowerShell module - dbatools.io";Authentication="Active Directory Password"'
New-Object Microsoft.Data.SqlClient.SqlConnection $azureconnstringAdditional context
Please also keep PowerShell documentation in mind for other new, cool SQL-related projects, not just cmdlets that are made (which are great for end-users), but the actual classes (which are appropriate for PS Devs).
Reacted by Friedrich Weinmann, Jeff Sharp, MartinHBA, Steffen Kampmann and Brett Gerhardi
Metadata
Metadata
Assignees
Labels
External 🔗Issue is in an external component
Activity
Wraith2 commentedon Aug 26, 2019
The api you use through powershell is just the standard api and the documentation for other .net langauges will apply equally. It may not have the specific powershell syntax but just like finding a vb.net sample when you're using c# you should be able to translate it.
The problem you're having is that you're trying to use the netstandard assembly and that won't work because netstandard isn't a platform. You need to use the correct binary for the runtime you're on, so netfx or netcore. I'm also not sure how you're going to deal with the dependency chain.
cheenamalhotra commentedon Sep 11, 2019
Hi @potatoqualitee
Although this would be good to have, I agree on that, but currently we are occupied with lot of high priority work including .NET API Browser Documentation onboarding for M.D.S. Once we get some bandwidth, we will consider adding this for Powershell users.
sharpjs commentedon Sep 15, 2019
I also was unable to use this package with PowerShell — specifically, in a PowerShell binary module. This package worked with PowerShell 5.1 (targeting
net461), but not PowerShell Core 6.2.3 (targeting eithernetstandard2.0ornetcoreapp2.1). In that environment, any use of types from this package throws aPlatformNotSupportedException.The
Microsoft.Data.SqlClient.dllfile in mybin\Debug\netcoreapp2.1folder is 323KB. ILSpy shows every method and property implemented with:The same DLL in my
bin\Debug\net461folder is 1835KB and is accompanied by a number of satellite assemblies.The relevant parts of my csproj:
For now I'm just going back to
System.Data.SqlClient.David-Engel commentedon Sep 19, 2019
When targeting netcore and picking your DLL manually, you need to pick the appropriate one from the runtime folder. The DLL in the lib folder just throws the PlatformNotSupported exception everywhere. The reason for this boils down to cross-platform support. SqlClient needs different DLLs depending on whether (at runtime) the environment is Windows versus Linux. But as Wraith2 mentioned, you are also going to have a problem with the dependency chain.
sharpjs commentedon Sep 28, 2019
I was able to get M.D.S working in my case via @David-Engel 's advice. It was quite painful, though. I had to traverse the M.D.S dependency graph, loading each DLL into ILSpy to see which references were unresolved, adding .csproj elements to resolve them, and recursing until all DLLs in the graph could resolve their dependencies. This was made more difficult by dependencies' assembly versions and nupkg versions often not matching, and by the dependency nupkgs rarely having release notes to explain what their versions mean. Then I needed a .ps1 script to load the appropriate M.D.S DLL constellation at module load time. I'm not confident that my solution is a correct one, and I'm not looking forward to doing this work again when M.D.S updates.
The desired developer experience here, IMO, is to have a simple set of instructions to follow (e.g. "go add this NuGet package"), resulting in a small .csproj that just works when you hit F5. I'm not an expert in the intricacies of packaging for various target frameworks/platforms/runtimes, nor should I need to be. That stuff should be handled automatically by the tooling.
MyProject.csproj
Resolve-SqlClient.ps1
MartinHBA commentedon Nov 5, 2019
could someone just give us PowerShell guys that are used to use just PowerShell Gallery modules clear step by step working connection example using Microsoft.Data.Sqlclient? I am having .NET 4.6 and I was able to add nuget as package provider to PowerShell, I was able to Install-Package Microsoft.Data.Sqlclient with PowerShell. But when I would like to create object in PowerShell code I need to load custom dll. When using Add-Type it is giving me errors with dependencies. However I can see all these additional required in same packages folder. When I was trying to load them one by one before calling Microsoft.Data.Sqlclient I was able to do it with Add-Type for Microsoft.Identity.Client and System.Data.Common, but no luck with Microsoft.Data.SqlClient.sni (missing manifest error). Can someone pls provide step by step PowerShell guide how can I use this new Microsoft.Data.Sqlclient namespace? I can do it pretty easily with System.Data.SqlClient but I'd really love to use new namespace now.
David-Engel commentedon Nov 7, 2019
Keep in mind that the developers of SqlClient are not PowerShell experts/developers.
After a bit of searching, it sounds like the PowerShell infrastructure might just not quite be up to easily handling NuGet packages yet. I found Loading Assemblies from NuGet Packages and Make using NuGet packages installed with Install-Package easier to use - make Add-Type support NuGet packages
It might be better to ask in the PowerShell repository how to correctly load and use a Nuget package.
It was much easier to use System.Data.SqlClient in .NET Framework because it and all dependencies were already installed for you and it only had to support one platform, Windows.
MartinHBA commentedon Nov 8, 2019
hi @David-Engel , I can load with Add-Type any nuget package without any problem if package has no dependency, Problem is not just loading nuget packages into PowerShell, it can be done, problem is loading nuget packages with all dependencies into PowerShell. And all dependencies should have manifests, etc. When I want to abstract this problem is:
There is not single "Hello World!" step by step example for PowerShell using Microsoft.Data.SqlClient. Not a single one as far as I know. I think this is far fatched said outrageous give that System.Data.Client won't get any more feature updates. @jpsnover promised us all with @theJasonHelmick that with PowerShell we can do all on Windows and here we are, having new .NET namespace that can't be easily loaded into PowerShell and used, yet System.Data.Client silently deprecated.
David-Engel commentedon Nov 8, 2019
You just described the problem with PowerShell. Dependencies are an integral part of NuGet packages. Once you leave .NET Framework, which was a very monolithic install of a lot of core dependencies, it's a rare package that has zero dependencies.
Until PowerShell fully supports NuGet packages, what new features are you blocked from using in Microsoft.Data.SqlClient that are not available in System.Data.SqlClient on .NET Framework?
MartinHBA commentedon Nov 8, 2019
@David-Engel thank you for your feedback, I am new to GitHub, can you tell me where we need to push so "Powershell fully supports NuGet packages" ?
when it comes to new features there are now just few, what is important is that they will no longer add more features to System.Data.SqlClient, meaning all new SQL 2019 features won't be available and so on. Which makes System.Data.SqlClient obsolete soon.
David-Engel commentedon Nov 8, 2019
@MartinHBA
I assume the PowerShell repository. Specifically, the issue I mentioned above looks relevant: Make using NuGet packages installed with Install-Package easier to use - make Add-Type support NuGet packages
MartinHBA commentedon Nov 8, 2019
Thank you all for your patience with me and guidance, I finally get it done. Key was to switch to PowerShell Core. I will test my scenario on fresh VM next week and let you know step by step if you are interested in. Have a great weekend all
MartinHBA commentedon Nov 9, 2019
I've documented it here
MartinHBA commentedon Nov 10, 2019
I also played around with package dependencies, looks brutal, I hope I have it wrong , check it here
MartinHBA commentedon Nov 13, 2019
Ok, I reworked dependency tree for package from post above. I was using Get-Package command which is not able to filter dependencies by target platform (I've opened issue for PowerShell here) . Alternatively I used Nuget API to determine full dependency tree for current major builds and .NET core 2.1
11 remaining items
MartinHBA commentedon Jun 15, 2020
@potatoqualitee easiest way forward is:
STEP 1: install Powershell 7
STEP 2: run Powershell 7 as administrator
STEP 3: Install-Package Microsoft.Data.SqlClient
DONE
edit: it worked before, now again showing dependency loop
sharpjs commentedon Jul 24, 2020
Does not work for me.
If this is the replacement for such a basic package as System.Data.SqlClient, why is it so difficult to use with PowerShell? So far, the only thing I've found to work is a hack at best.
MartinHBA commentedon Jul 24, 2020
@sharpjs , can you pls paste here your $psversiontable?
and btw for me my nuget location is https://www.nuget.org/api/v2/ when checked with Get-PackageSource
sharpjs commentedon Jul 25, 2020
I'll try changing my package source to the
v2URL. Edit: No luck.sharpjs commentedon Jul 25, 2020
FWIW, the hack I currently use for PowerShell binary modules is:
In project file:
In a
.ps1script executed at module load time:And
SniLoader.cs:...and make sure you execute
SniLoader.Load()somewhere before you use anySqlClientstuff.simonsabin commentedon Jul 29, 2020
Sounds like whats really needed is a better powershell module that wraps sqlclient. Invoke-sqlcmd isn't it.
sharpjs commentedon Jul 29, 2020
@simonsabin I'm working on it.
MartinHBA commentedon Jul 30, 2020
@sharpjs @cheenamalhotra @potatoqualitee @simonsabin
Based on previous discussion here I was looking into progress with PowerShellGet v3 which is supposed to be new go-to tool for installing packages, even from Nuget or other providers. It's preview version only atm.
I found nice article about it here
Having high hopes I started to follow article above:
Install-Module PowerShellGet -Force -AllowPrereleaseand now seeing PowerShellGet v3 commands suite:
Then I tried and I've got some result
Find-PSResource Microsoft.Data.SqlClient -Version *So I've tried final promising command:
Install-PSResource Microsoft.Data.SqlClient -Version 2.0.0 -VerboseHowever it's stuck for me as below:
If you would have more luck with this approach please let me know, I found similar issue opened for this.
cheenamalhotra commentedon Jul 31, 2020
@MartinHBA
Could you try making an entry for NuGet as trusted source? Wondering if this is related: https://github.com/PowerShell/PowerShellGet/issues/154#issuecomment-647027689 ?
MartinHBA commentedon Aug 1, 2020
@cheenamalhotra it worked fine to install Pester (without "force" parameter, that is unavailable), trying same for Microsoft.Data.SqlClient it didn't work and is stuck forever
Nuget is trusted for me
ErikEJ commentedon Aug 1, 2020
Should it not be: https://api.nuget.org/v3/index.json ?
MartinHBA commentedon Aug 1, 2020
@ErikEJ Pester installation from Nuget works, would you kindly test this from your side? I tested all http and https and v2 and v3, no luck for Microsoft.Data.SqlClient, works smoothly for Pester package.
cheenamalhotra commentedon Aug 5, 2020
@MartinHBA
Is there any other .NET NuGet package that worked for you?
Pester seems to be designed for Powershell, and also has no dependencies, it's not surprising it works.
MartinHBA commentedon Aug 10, 2020
well, is there? who knows...
MartinHBA commentedon Aug 15, 2020
I can't get it work at current, I just wait for next Powershell 7 preview version