为什么即使在添加程序集后 C# 也无法识别 Server() "Microsoft.SqlServer.ConnectionInfo"

Why C# does not recognize Server() even after adding assembly "Microsoft.SqlServer.ConnectionInfo"

找不到类型或命名空间名称 'Server'(是否缺少 using 指令或程序集引用?)

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

添加到顶部

using Microsoft.SqlServer.Management.Common;

仍然报错。
任何帮助适用。

已检查:

I can't add Microsoft.SqlServer.Management.Common to my ASP.NET MVC Application

按照 Han 的建议,请检查程序集引用路径并确保该文件位于引用位置。我遇到过类似的问题,这是在开发环境中工作,但在测试服务器上出错。原因是 SQL Server Management studio 没有安装在测试服务器上。因此,我将所需的程序集从开发机器复制到测试服务器上应用程序的 bin 文件夹,并且成功了。

安装这个 nuget 包:

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

然后添加using指令:

using Microsoft.SqlServer.Management.Smo;

现在您可以创建服务器对象了。

var server = new Server();

正在安装 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://docs.microsoft.com/en-us/sql/relational-databases/server-management-objects-smo/installing-smo?view=sql-server-2017

您可以尝试添加对以下内容的引用:

Microsoft.SqlServer.dac.dll

如果找不到dll,则需要安装SSDT (Sql Server Data Tools)。