简体   繁体   English

命名空间“Microsoft.SqlServer”中不存在类型或命名空间名称“Smo”(您是否缺少程序集引用?)

[英]The type or namespace name 'Smo' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?)

I want to teke backup of database in windows forms application.我想在 Windows 窗体应用程序中备份数据库。 I have added References to all 4 ddls我已经添加了对所有 4 个 ddls 的引用

Microsoft.SqlServer.ConnectionInfo.dll
Microsoft.SqlServer.Management.Sdk.Sfc.dll
Microsoft.SqlServer.Smo.dll
Microsoft.SqlServer.SmoExtended.dll

My Code is我的代码是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Smo;
using Microsoft.SqlServer.Smo.SmoExtended;


namespace Lodging
{
    class BackupAndRestoreData
    {            
        public static void BackupDatabase(string backUpFile)
        {
            ServerConnection con = new ServerConnection(@"ANI-PC\SQLEXPRESS");
            Server server = new Server(con);
            Backup source = new Backup();
            source.Action = BackupActionType.Database;
            source.Database = "db_Lodge";
            BackupDeviceItem destination = new BackupDeviceItem(backUpFile,
                                                            DeviceType.File);
            source.Devices.Add(destination);
            source.SqlBackup(server);
            con.Disconnect();
        }
        public static void RestoreDatabase(string backUpFile)
        {
            ServerConnection con = new ServerConnection(@"ANI-PC\SQLEXPRESS");
            Server server = new Server(con);
            Restore destination = new Restore();
            destination.Action = RestoreActionType.Database;
            destination.Database = "MyDataBaseName";
            BackupDeviceItem source = new BackupDeviceItem(backUpFile,
                                                           DeviceType.File);
            destination.Devices.Add(source);
            destination.ReplaceDatabase = true;
            destination.SqlRestore(server);
        }
    }
}

This is not giving me error for这并没有给我错误

using Microsoft.SqlServer.Server;
using Microsoft.SqlServer.Management.Sdk.Sfc;

but gives error :但给出错误:

The type or namespace name 'Smo' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?)命名空间“Microsoft.SqlServer”中不存在类型或命名空间名称“Smo”(您是否缺少程序集引用?)

for为了

using Microsoft.SqlServer.Smo;
using Microsoft.SqlServer.Smo.SmoExtended;

What should I do to remove this error?我应该怎么做才能消除这个错误?

using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

Try this two namespaces.试试这两个命名空间。

The Microsoft.SqlServer.Management.Smo namespace contains the instance object classes that represent SQL Server Database Engine objects and some utility classes that represent specific tasks, such as scripting. Microsoft.SqlServer.Management.Smo 命名空间包含代表 SQL Server 数据库引擎对象的实例对象类和代表特定任务(如脚本)的一些实用程序类。 When a connection to the instance of the SQL Server Database Engine has been established by using a Server object variable, objects on the instance can be accessed by using the SMO instance objects.使用服务器对象变量建立到 SQL Server 数据库引擎实例的连接后,可以使用 SMO 实例对象访问该实例上的对象。 For example, you can use the Database object to access databases on the connected instance of SQL Server Database Engine.例如,您可以使用 Database 对象访问连接的 SQL Server 数据库引擎实例上的数据库。 All the instance classes are related to the Server class in the object hierarchy.所有实例类都与对象层次结构中的 Server 类相关。 Utility classes exist outside of the Server class object hierarchy and represent specific tasks, such as backup or scripting.实用程序类存在于服务器类对象层次结构之外,代表特定任务,例如备份或脚本。

Here is link to the article in MSDN这是MSDN 文章的链接

Common is also widely used I recommend you to read about it. Common 也被广泛使用,我建议你阅读它。

Not sure if this help you, but for the compile error:不确定这是否对您有帮助,但对于编译错误:

The type or namespace name 'XYZ' does not exist in the namespace 'ABC.DEF' (are you missing an assembly reference?)命名空间“ABC.DEF”中不存在类型或命名空间名称“XYZ”(您是否缺少程序集引用?)

I found following reasons causing that complation error:我发现以下原因导致该编译错误:

1) If the assembly is targeting later .NET framework(eg 4.5) and you refer the assembly from an application targeting early .NET framework (eg4.0) then you may face this problem. 1) 如果程序集面向较新的 .NET 框架(例如 4.5),并且您从针对早期 .NET 框架(例如 4.0)的应用程序中引用该程序集,那么您可能会遇到此问题。

2) Some times, the "Client Profile" .NET framework projects can't refer to full .NET version assemblies, because Client Profile .NET framework is a strip-down version of .NET framework. 2) 有时,“Client Profile”.NET framework 项目不能引用完整的 .NET 版本程序集,因为 Client Profile .NET framework 是 .NET framework 的精简版本。

Install SMO安装 SMO

The SQL Server Management Objects (SMO) Framework is a set of objects designed for programmatic management of Microsoft SQL Server and Microsoft Azure SQL Database. SQL Server 管理对象 (SMO) 框架是一组对象,旨在以编程方式管理 Microsoft SQL Server 和 Microsoft Azure SQL 数据库。

When explicitly targeting a pre-2017 SQL Server version, install the SQL Server Feature Pack (more info in this answer ).明确针对 2017 之前的 SQL Server 版本时,请安装 SQL Server 功能包(此答案中的更多信息)。

For SQL Server 2017 and above, install SMO as a NuGet package, as per this answer .对于 SQL Server 2017 及更高版本,请按照此答案将 SMO 作为 NuGet 包安装。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命名空间“Microsoft.SqlServer”中不存在类型或命名空间名称“Management” - SqlParser和VSTS自动构建 - The type or namespace name 'Management' does not exist in the namespace 'Microsoft.SqlServer' - SqlParser and VSTS automated builds 类型或名称空间名称在名称空间中不存在“是否缺少程序集引用?” - The type or namespace name does not exist in the namespace ''are you missing an assembly reference ?" 命名空间中不存在类型或命名空间名称(您是否缺少程序集引用?) - The type or namespace name does not exist in the namespace (are you missing an assembly reference?) 错误 CS0234:命名空间“Microsoft”中不存在类型或命名空间名称“Azure”(您是否缺少程序集引用?) - error CS0234: The type or namespace name 'Azure' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 命名空间“Microsoft.AspNetCore”中不存在类型或命名空间名称“Mvc”(您是否缺少程序集引用?) - The type or namespace name 'Mvc' does not exist in the namespace 'Microsoft.AspNetCore' (are you missing an assembly reference?) 类型或名称空间名称'PointOfService'在名称空间'Microsoft'中不存在(您是否缺少程序集引用?) - The type or namespace name 'PointOfService' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 错误 CS0234 命名空间“Microsoft”中不存在类型或命名空间名称“Reporting”(您是否缺少程序集引用?) - Error CS0234 The type or namespace name 'Reporting' does not exist in the namespace 'Microsoft' (Are you missing an assembly reference?) 类型或名称空间名称“ MediaServices”在名称空间“ Microsoft.WindowsAzure”中不存在(您是否缺少程序集引用?) - The type or namespace name 'MediaServices' does not exist in the namespace 'Microsoft.WindowsAzure' (are you missing an assembly reference?) 命名空间“Microsoft”中不存在类型或命名空间名称“VisualStudio”(您是否缺少程序集引用?) - The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) 命名空间“Microsoft”中不存在类型或命名空间名称“AspNetCore”(您是否缺少程序集引用?) - The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM