Unable to Add SqlServerSpatial140.dll RRS feed

  • Question

  • I m attempting to insert an SqlGeography instance into a SQL Server database with this code:

    Dim sql As SqlChars = New SqlChars(New SqlString(wkt.ToString))
    Dim GeoPoint As SqlGeography = SqlGeography.STPointFromText(sql, 4326)

    where wkt is the Well Known Text of a point.  I get this error message:

      Message=Unable to load DLL 'SqlServerSpatial140.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
      Source=<Cannot evaluate the exception source>
    <Cannot evaluate the exception stack trace>

    When I try to add it to my references I get this error message:

    A reference to SqlServerSpatial140.dll could not be addes.  Please make sure that the file is accessible, and that it is a valid assembly or COM component.

    I have used NuGet to add the file and still get this message.  I have downloaded the file from Microsoft and get the same message when I try to add it.  Now I have no idea what to try next.  Any ideas?

    Saturday, July 25, 2020 3:34 AM

All replies

  • When you install the Microsoft.SqlServer.Types nuget package, this should create a new folder in root:

    which should contain the appropriate dll's. It's also auto setup to copy if newer.

    Then, make sure your app loads the appropriate assembly:

    For ASP.NET Web Applications: SqlServerTypes.Utilities.LoadNativeAssemblies(Server.MapPath("~/bin"));
    For desktop applications / others: SqlServerTypes.Utilities.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
    Not sure if this works with .NET Core though.
    Saturday, July 25, 2020 11:35 AM
  • I've used NuGet and those directories you listed are, indeed, there.  But when I try to add SqlServerSpatial140.dll to my references, I get the second error message shown in my opening post.

    Also, thinking the problem lies somewhere in my Project, I created a dummy project that used SqlServerSpatial140.dll and nothing else and STILL got the same error.  I'm baffled, to put it mildly.

    Saturday, July 25, 2020 8:40 PM
  • A quick Bing Search finds an answer on Stack Overflow...https://stackoverflow.com/questions/42654893/unable-to-load-dll-sqlserverspatial140-dll-the-specified-module-could-not-be

    Essentially for the running code to find the Dll it must be in the output directory (Or the folder where the exe launches from or Windows system where other dlls live).

    Posted by: Kristoffer Jälén

    Copy the dll from C:\Users\<User>\.nuget\packages\Microsoft.SqlServer.Types\14.0.314.76\nativeBinaries\x86 to your project. Right-click the file and click Properties. Set "Copy To Output Directory" to "Copy Always".

    Monday, July 27, 2020 3:18 AM
  • I have read every single answer and comment in that SO thread.  Then I did it again.  Then once more.  I've got it memorized by now.

    Nothing there works .... for me anyway.

    I don't have a property called "Copy To Output Directory". I only get a property "Copy Local" and the only possible values are True/False.

    Monday, July 27, 2020 5:59 AM
  • Hi Roger,

    This is old school.  Use file explorer and put a copy of the DLL in the folder where the EXE lives. 



    Monday, July 27, 2020 5:28 PM
  • Old school indeed.  But it works so I'll go with it for now.

    Just to clear up this, can you be more specific about where I can find the option to "Copy To Output Directory"?

    In any case, thanks ... again.

    Monday, July 27, 2020 11:44 PM
  • I found some helpful stuff with a bing search...https://www.bing.com/search?q=nuget+copy+to+output+directory

    And there are some steps here but I am not a nuget expert by far... https://stackoverflow.com/questions/44747744/how-can-i-set-the-copy-to-output-directory-property-in-my-nuspec-file

    From Leo Liu-MSFT:

    To resolve this question, you can follow below steps:

    1. Add a xx.targets file in your project folder, make sure the name of the target file is the same name as the package id(TestDemo is my package ID, so the name of .targets is TestDemo.targets).

    2. Add below code in the targets file:

      <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <None Include="$(MSBuildThisFileDirectory)GRabc.txt">

    Note: The path of "$(MSBuildThisFileDirectory)" should be relative path, if you are not familiar with it, you can use the absolute path.

    1. In the nuspec file, add required file to the Build directory along with the targets file.

          <file src="bin\x64\Debug\GR*.txt" target="Build\" />
          <file src="TestDemo.targets" target="Build\" />
          <file src="bin\Debug\TestDemo.dll" target="lib\462" />
    2. Pack this package, then add it on other project to test, it work fine

    Tuesday, July 28, 2020 9:14 PM
  • This might not be a folder thing at all.  RogerSSB is using VB.Net and, to use SQL Types, you have to run an initialization routine to make it work right.  The routine is included along with the DLL but its in C#, so you can't call it from VB.

    Currently, I'm using the following code:

    Module Loader
        Private Declare Auto Function LoadLibrary Lib "kernel32.dll" (libname As String) As IntPtr
        Friend Sub LoadNativeAssemblies()
            Dim sBase = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SqlServerTypes\x86")  'server will not load the 32-bit versions!
            Dim Ptr = LoadLibrary(IO.Path.Combine(sBase, "msvcr120.dll"))
            If Ptr = IntPtr.Zero Then Throw New ApplicationException("Could not load msvcr120.dll from " + sBase)
            Ptr = LoadLibrary(IO.Path.Combine(sBase, "SqlServerSpatial140.dll"))
            If Ptr = IntPtr.Zero Then Throw New ApplicationException("Could not load SqlServerSpatial140.dll from " + sBase)
        End Sub
    End Module

    However, my application is set to be 32-bit only.  If you're app is 64-bit, you'll need to change it to use that.  If it's AnyCPU, you'll need to check what its running as then load accordingly.  (You can use the C# routine to guide you.

    Copy to Output Directory is a property on each of the two DLLs in the SqlServerTypes\x86 (or x64) folder.  Set it to Copy if Newer if it isn't already set to that.

    Tuesday, August 4, 2020 6:10 PM