C# ServerConnection |使用Microsoft.SqlServer.SmoEnum进行备份还原错误-FileNotFoundException

C# ServerConnection |使用Microsoft.SqlServer.SmoEnum进行备份还原错误-FileNotFoundException,c#,sql-server,vb.net,smo,database-backups,C#,Sql Server,Vb.net,Smo,Database Backups,我在谷歌搜索了一段时间后使用引用编写了这段代码,但是在执行backupdatebase(myfilename.bak)时,它在行号11处给出了错误,并说: FileNotFoundException未处理 无法加载文件或程序集“Microsoft.SqlServer.smoneum,Version=9.0.242.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91”或其依赖项之一。系统找不到指定的文件。 我已经检查了SMO文件是否存在于我的驱动器和

我在谷歌搜索了一段时间后使用引用编写了这段代码,但是在执行
backupdatebase(myfilename.bak)
时,它在行号
11
处给出了错误,并说:

FileNotFoundException未处理

无法加载文件或程序集“Microsoft.SqlServer.smoneum,Version=9.0.242.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91”或其依赖项之一。系统找不到指定的文件。

我已经检查了
SMO
文件是否存在于我的驱动器和
bin
中。我无法理解这个问题。请帮忙

public readonly string ConnectionString = MyApp.Properties.Settings.Default.DbConnectionString;
public void BackupDatabase(string backUpFile)
    {
        ServerConnection con = new ServerConnection(ConnectionString);
        Server server = new Server(con);
        Backup source = new Backup();
        source.Action = BackupActionType.Database;
        source.Database = "TestDB";
        BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
        source.Devices.Add(destination);
        source.SqlBackup(server);
        con.Disconnect();
    }
此外,我还尝试:

if(con.Isopen)
{
    //Then all of my code goes in here
}
但是这个条件永远不会满足,内部代码也永远不会执行

我还注意到,就像我们使用
con.open()
进行
数据库连接
,我们在
服务器连接
中没有这样的选项

编辑-1:

在调试时,我用
con

编辑-2:

我对这个问题还有一个疑问:DB的connectionString与ServerConnection的connectionString是否相同

我实际上是在谈论这一行:-

public只读字符串ConnectionString=MyApp.Properties.Settings.Default.DbConnectionString


因为上面的
ConnectionString
包含
{server='Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory |\MyDb.mdf;Integrated Security=True;User Instance=True';Trusted_Connection=True;multipleactiveresultset=false}
要创建Smo连接,您只需要一个服务器连接,要创建服务器连接,只需输入SQL Server的名称。 范例

正如我所看到的,您正在使用SQLExpress。默认情况下,SQL Server实例将被称为“.\SQLEXPRESS”,因此您必须执行以下操作:

ServerConnection serverConnection = new ServerConnection(".\\SQLEXPRESS")
Smo.Server smoServer = new Smo.Server(serverConnection)
注意“\”以防止在字符串内转义

请看一下我的工作示例,以防它有所帮助:

您是否在项目中添加了对dll的引用?
ServerConnection serverConnection = new ServerConnection(".\\SQLEXPRESS")
Smo.Server smoServer = new Smo.Server(serverConnection)