SqlClient: Cannot use with F# scripts

Scripts are a common and everyday way to work F# and forms part of the standard development lifecycle. I can’t figure out how to use the SqlClient library from within an F# script:

  1. Download the nuget package.
  2. Try referencing the netstandard version of the library e.g. #r @"C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\1.0.19269.1\lib\netstandard2.0\Microsoft.Data.SqlClient.dll.
  3. Script will crash when you try to open a connection with a PlatformNotSupported exception.
  4. After looking around for a while, read this link.
  5. Try to reference the net46 version of the library: #r @"C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\1.0.19269.1\lib\net46\Microsoft.Data.SqlClient.dll"
  6. Get another exception:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\1.0.19269.1\lib\net46\x64\SNI.dll
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 66

I can’t find any SNI.dll within the nuget package. How can I get this to work?

Also - why include a netstandard2.0 TFM in the package, if netstandard2.0 is, effectively, not supported? This is misleading and very tightly couples the package to NuGet.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 37 (13 by maintainers)

Most upvoted comments

You’re going to have the same problems on netcore. The library is no longer in-box and has external dependencies which are resolved by nuget. You need either a nuget package restore or to manually walk the dependency tree and ensure all required dependencies are present.

Try the managed appcontext switch as @cheenamalhotra suggested, that removes the native dll requirement.

#r “C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\2.0.0-preview3.20122.2\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll”

Please ensure you pick up “runtime” libraries from path:

#r “C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\2.0.0-preview3.20122.2\runtimes\win\lib\netcoreapp3.1\Microsoft.Data.SqlClient.dll” OR #r “C:\Users\Isaac\.nuget\packages\microsoft.data.sqlclient\2.0.0-preview3.20122.2\runtimes\win\lib\netstandard2.0\Microsoft.Data.SqlClient.dll”

The DLLs in lib\netcoreapp2.1 and lib\netstandard2.0 are “Not Supported” DLLs.

Awesome, that does work. Is this going to be a default in .NET 5?

Separately, it seems like failing to find sni.dll is a bug in the dependency manager component used with FSI on .NET Core. We’ll need to investigate thoguh

I still get this same error with the #r "nuget:..." feature with F# scripts. This feature does load native .dlls (such as those in ML.NET), so there might be something else missing? @isaacabraham can you confirm this if you run the following?

#r "nuget: Microsoft.Data.SqlClient, Version=2.0.0-preview3"

open Microsoft.Data.SqlClient

let str = "Data Source=(local);Initial Catalog=NorthwindIntegrated Security=SSPI"
let qs = "SELECT OrderID, CustomerID FROM dbo.Orders;"

use conn = new SqlConnection(str)
let cmd = SqlCommand(qs, conn)
cmd.Connection.Open()
cmd.ExecuteNonQuery()

(replacing whatever query is there with your own)

dotnet fsi --langversion:preview foo.fsx

I get

System.DllNotFoundException: Unable to load DLL 'sni.dll' or one of its dependencies: The specified module could not be found. (0x8007007E)

@cheenamalhotra perhaps open this again?