14
votes

I know this question was asked for many times and I found a lot of information on Google regarding this problem, but I still can't solve the problem that exists on my DEV PC :(
I have that ugly error message on every page

Could not load file or assembly Microsoft.SqlServer.BatchParser

I installed Microsoft SQL Server 2005 Management Objects Collection as advised in many internet resources, but it did not help.

Then I tried to install Microsoft SQL Server 2008 Management Objects, but it says it can not be installed since newer version exists already (I have SQL Server 2008 installed).

Then I went to GAC, and found following lines:

Microsoft.SqlServer.BatchParser, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=AMD64

Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=AMD64

Microsoft.SqlServer.BatchParser, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=x86

Microsoft.SqlServer.BatchParser, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=x86

Microsoft.SqlServer.BatchParserClient, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91, processorArchitecture=MSIL <-- Should version 9.0.242.0 exist?

I already spent about half a day to find a solution without success, and it's very crappy to work with VS when IntelliSense does not work because of that error.

I have VS 2010 SQL Express 2005 and SQL Server 2008 installed (see configuration screenshot) enter image description here

How can this dependency problem be solved?

9

9 Answers

7
votes

NOTE: This answer is for people who are using MS SQL SMO (Management Objects) from a C# program.

I was struggling with the error mentioned in the question. The exact error I got was this:

Could not load file or assembly 'Microsoft.SqlServer.BatchParser.dll' or one of its dependencies. A dynamic link library (DLL) initialization routine failed. (Exception from HRESULT: 0x8007045A)

It ended up to be a problem with running .NET 2.0 assemblies within .NET 4.0 (or higher) CLR. The SQL server SMO assemblies are built against v2.0.50727 of the runtime and hence the following section is required in the app.config (or web.config)

  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

The hint was in this discussion thread on MSDN forums.

5
votes

Do you see this error in Management studio or Visual Studio? When do you get this error message? The problem could be caused due to 32 bit vs 64 bit interaction as doc'ed in

956178 Error message when you run a 32-bit version of SQL Server 2008 on an Itanium-based version of Windows: "The system cannot find the file specified" http://support.microsoft.com/kb/956178/EN-US

If you are using a 64 bit system ensure you installed 64 bit version of the updated management objects.

4
votes

At this time 08 feb 2020 smo 16 solve the problem.

Install-Package Microsoft.SqlServer.SqlManagementObjects -Version 160.1911221.0-preview

Still in preview but it works pretty well

3
votes

you must be getting this error while executing the application via visual studio. I have got this error in the past but it was resolved after some workaround like

  1. remove unwanted reference of BatchParser (if it exists in web.config).
  2. some 3rd party dll used which might be causing the application to through this error.
  3. You might have copied your aspx page from some other page and have forgotten to change the "inherits" section.
2
votes

I had to go into the advanced settings of my application pool and set "enable 32-bit applications" to TRUE ( I was running 64-bit SQL Server).

1
votes

If you are using the newest SMO retrieve from Nuget you may encounter similar issue either

Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.

or message without telling you the version number

Could not load file or assembly 'Microsoft.SqlServer.BatchParser.dll' or one of its dependencies. The specified module could not be found.

For guys whose error is the one not mentioned the version number, it's since we need vc2013 runtime to conduct SMO. See Microsoft's explanation and download link here.

For whom has error with the version number above, I found jendev provided a repo in this discussion which you can clone and get the dll after compile. If you don't have Visual Studio then take my fork which has included the exact dll already.

1
votes

I know this is a pretty old question but just in case this helps anyone else, I am using Microsoft.SqlServer.SqlManagementObjects in my C# application (v150.18208.0) from Nuget. When I got to my code that attempted to use ServerConnection.ConnectionContext.ExecuteNonQuery it blew up giving the error:

"Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.":"Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

This is a bug that hopefully gets fixed in the Nuget package.

I got around the error by manually adding v15.0.600.33 of Microsoft.SqlServer.BatchParser to the GAC. I found the dll at C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\SQLCommon\150. This path might change depending on your installation of Visual Studio.

If you are distributing and don't want your users to have to add this to the GAC then just include the dll as a reference in your C# project.

0
votes

I got this error after I upgraded my installshield project (2008 to 2017):

  • We upgraded from 2008 SMO being installed through the MSI
  • Removed the MSI from the project installing them
  • Upgraded to 2017 which are delivered through nuget packages
  • Installed it and it added all the dll's to the project
  • Added the dll's provided by the package to installshield

After installshield installed my program, it blew up right away complaining about the BatchPaser.dll:

"Could not load file or assembly 'Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.":"Microsoft.SqlServer.BatchParser, Version=15.100.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"

I downloaded the nuget package from the site after debugging for a while. With the new SMO's in packages, instead of msi's installing them, it's not recommended by Microsoft to add them to your GAC (In case your wondering):

As mentioned on the Files and Version Numbers page, you should not install the SMO assemblies into the GAC. Doing so could cause issues with other applications which also use those versions of SMO (such as SQL Server Management Studio).

https://docs.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/installing-smo?view=sql-server-ver15

https://docs.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/files-and-version-numbers?view=sql-server-ver15

What you need to do is download the nuget package from the site and goto runtimes/win-x86/native. You then see the BatchParser.dll. Add that to the project install/directory or wherever. Worked for me and was pretty much what the error message was telling me. Nuget just didn't install this file in the project.

https://www.nuget.org/packages/Microsoft.SqlServer.SqlManagementObjects/150.18178.0

0
votes

In my case, this issue could be solved by setting the target platform to x86 in the Build settings for a C# project in Visual Studio (2019), cheers