0

我有 3 层应用程序。我正在尝试使用 C# 代码备份 SQL Server 数据库。我从 UI 传递了选定的文件夹路径并在数据层中执行代码。在选择数据库的地方,它给了我一个错误。

未能为此请求检索数据。连接类型无效。
在 Microsoft.SqlServer.Management.Smo.Enumerator.Process(Object connectionInfo, Request request)
在 Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorData(Request req)
在 Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbCollat​​ion(String dbname )
在 Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer() 的 Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean inServer
)

我创建了一个带有一层的示例项目并执行代码。它工作正常。我找不到问题

我使用的代码如下;

public void BackupData(string filePath)
    {
        Backup sqlBackup = new Backup();

        sqlBackup.Action = BackupActionType.Database;
        sqlBackup.BackupSetDescription = "ArchiveDataBase:" +
                                         DateTime.Now.ToShortDateString();
        string fileName = "\\ArchiveDataBase.bak";
        sqlBackup.BackupSetName = "Archive";

        sqlBackup.Database = "SanasaLibrarySystem";

        BackupDeviceItem deviceItem = new BackupDeviceItem(filePath + fileName, DeviceType.File);
        DataConnection dataConnection = new DataConnection();
        ServerConnection connection = new ServerConnection(dataConnection.DataBaseConnection);
        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases["SanasaLibrarySystem"];

        sqlBackup.Initialize = true;
        sqlBackup.Checksum = true;
        sqlBackup.ContinueAfterError = true;

        sqlBackup.Devices.Add(deviceItem);
        sqlBackup.Incremental = false;

        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

        sqlBackup.FormatMedia = false;

        sqlBackup.SqlBackup(sqlServer);

    }
4

0 回答 0