2

I have a problem while using ExecuteNonQuery.

This is my code :

var connString = @"Data Source=serwer01;Initial Catalog=PolsatCyfrowy;Integrated Security=True";

FileInfo file = new FileInfo("C:\\Users\\azbudniewek\\source\\repos\\UM2 V2\\UM2 V2\\scripts.sql");
string script = file.OpenText().ReadToEnd();

SqlConnection conn = new SqlConnection(connString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);

And this is the error:

System.IO.FileNotFoundException: The file or set 'Microsoft.SqlServer.BatchParser, version = 14.100.0.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91' or one of its company can not be loaded. The file can not be downloaded.
File name: 'Microsoft.SqlServer.BatchParser, version = 14.100.0.0, Culture = neutral, PublicKeyToken = 89845dcd8080cc91'

in System.Reflection.RuntimeAssembly.GetType (RuntimeAssembly set, string name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack)
in System.Reflection.RuntimeAssembly.GetType (string name, Boolean throwOnError, Boolean ignoreCase)

I read a lot of solutions with problem but can't do anything .

Should I just install another SQL Server? I have installed SQL Server 2014 on my computer and folder with assembly looks like this:

Screenshot

What should I do to avoid this problem? I think I should have version 14.100.0.0 in my assembly folder but don't know how to update it. Maybe anyone know it?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Adam Zbudniewek
  • 107
  • 2
  • 12
  • 1
    Why are you not using standar SQL Server client related classes? Use `SQLCommand` intead `Microsoft.SqlServer.Management.Common.ServerConnection`. BTW, there is very good info on microsoft about connecting to SQL Server. – Cleptus Jun 26 '18 at 10:51
  • I think that dll should be present in folder C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\Extensions\Microsoft\SQLCommon\130 you will need to copy it from there to here. C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\VisualStudio\v15.0\Web – Prateek Deshmukh Jun 26 '18 at 10:57
  • What let you 'think' that assembly is in your folder? Please check it. Via context menu, settings you can check it's version. If the file is not there, check assembly settings in your editor. In VS context menu on assemlby/dll, preferences, set 'local copy' true. That way a copy is created to your build folders instead of using local copys, which might not exist on another machine. – nilsK Jun 26 '18 at 10:58
  • You are missing the 64 bit version of the assembly. Download the x64 version of the SMO – NoviceProgrammer Jun 26 '18 at 11:00
  • 2
    This is against "Keep it simple" principle. If you are not doing fancy things, you should consider removing those references and using regular `System.Data.SqlClient.SqlCommand`. – Cleptus Jun 26 '18 at 11:02
  • 1
    I just using SMO because ineed to load long scripts from file , can't do in in 'SQLCommand' – Adam Zbudniewek Jun 26 '18 at 11:57

4 Answers4

0

I had a similar issue. I have an SMO application which uses the Microsoft.SqlServer.SqlManagementObjects nuget package.

The app all runs perfectly on my p.c however when it came to deploying it and running it on our app server i was getting the error.

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

To fix the issue I needed to build the project again targeting x86 platform.

Ali Tooshmalani
  • 241
  • 2
  • 7
0

You should add the missing dll in your project and if you publish your project, you must put this dll in to the bin folder.

enter image description here

Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32
0

Install SMO using NuGet, then install VC++ 2013 Redistributables

SMO is now deployed via NuGet. The NuGet page is here.

Noted under "System Requirements" -- "Some native binaries installed with the NetFx SMO libraries also require the VC 2013 runtime to be installed; that runtime is not included in the package."

Thus, after installing SMO via NuGet, manually install the VC++ Redistributables for the architecture of your code (x86 / x64); this is the dependency that Microsoft.SqlServer.BatchParserClient.dll relies upon.

CJBS
  • 15,147
  • 6
  • 86
  • 135
-1

I had faced a similar issue. In my setup, I have a WPF application which uses a class library which in turn refers the Microsoft.SqlServer.SqlManagement Objects (SMO) (version 140.17283.0) via NuGet package.

After trying lot of things, finally figured out that in addition to the class library, the WPF application also needs to refer the SMO library via NuGet.

So I just added the SMO library via NuGet at the solution level and install it for class library as well as WPF application. Voila, it worked !!! (Looks like the Microsoft.SqlServer.BatchParserClient.dll is not properly referenced if it is referred only in the class library which is weird but it solved the issue)