1

The type or namespace name 'Server' could not be found (are you missing a using directive or an assembly reference?)

string connetionString = @"connectionStirng"; 
SqlConnection conn = new SqlConnection(connetionString); 
var server = new Server(new ServerConnection(conn));

added at the top

using Microsoft.SqlServer.Management.Common;

Still giving error.
Any help appricated.

Already checked:
Why i'm getting error for the Microsoft.SqlServer.Server namespace?
I can't add Microsoft.SqlServer.Management.Common to my ASP.NET MVC Application

enter image description here

Han
  • 3,052
  • 2
  • 22
  • 31
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90

3 Answers3

1

Install this nuget package:

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

Then add using directive:

using Microsoft.SqlServer.Management.Smo;

Now you can create Server object.

var server = new Server();

Installing SMO

Beginning with SQL Server 2017 SMO is distributed as the Microsoft.SqlServer.SqlManagementObjects NuGet package to allow users to develop applications with SMO.

This is a replacement for SharedManagementObjects.msi, which was previously released as part of the SQL Feature Pack for each release of SQL Server. Applications that use SMO should be updated to use the NuGet package instead and will be responsible for ensuring the binaries are installed with the application being developed.

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

Han
  • 3,052
  • 2
  • 22
  • 31
  • It is already added to references and exists in folder you specified. still getting error – Suresh Kamrushi Oct 11 '18 at 10:02
  • cant install that because of NuGet version required 2.12 but i can update NuGet because of VS 2012. see https://stackoverflow.com/questions/27384885/how-to-upgrade-nuget-with-visual-studio-express-2012 – Suresh Kamrushi Oct 11 '18 at 10:27
  • @SureshKamrushi I saw your screenshot. You wrote `new server(...)`. It should be `new Server(...)`. C# is case sensitive. – Han Oct 11 '18 at 11:29
  • @SureshKamrushi It would've been solved quicker if you had posted the screenshot earlier. – Han Oct 11 '18 at 11:35
  • 1
    `using` *directive*, not `using` *statement*. C# has two features that both use the `using` keyword. Using the name of one to refer to the other doesn't aid communication. – Damien_The_Unbeliever Oct 11 '18 at 11:35
  • @Damien_The_Unbeliever Yes, my mistake. I was working with VB.Net earlier. I posted Import statement in my earlier edit. Then changed Import into using, I forgot to change the statement into directive. – Han Oct 11 '18 at 11:37
  • 1
    It solved because of installation of "Microsoft.SqlServer.SqlManagementObjects". typo is because i trying it with different cases. – Suresh Kamrushi Oct 11 '18 at 11:47
0

As suggested by Han, Please check the assembly reference path and make sure that the file is there at the referenced location. I had faced similar problem, this was working on the Development environment but error on test server. The reason was the SQL Server Management studio was not installed on the Test server. So i copied the required Assemblies from dev machine to bin folder of the application on test server and that worked.

0

Can you try adding a reference to:

Microsoft.SqlServer.dac.dll

If cant find the dll, then need to install SSDT (Sql Server Data Tools).

Gauravsa
  • 6,330
  • 2
  • 21
  • 30