简体   繁体   English

备份和还原SQL数据库

[英]Backup and Restore SQL Database

I need to backup (using C#) a SQL 2005 Database to a *.bak file. 我需要将SQL 2005数据库备份(使用C#)到* .bak文件。 From that file, I am trying to restore into a new Database on the same exact server. 从该文件,我试图在同一个服务器上恢复到一个新的数据库。

The backup works fine and then my C# code calls the restore method and it appears to be working until it finally "timesout". 备份工作正常,然后我的C#代码调用还原方法,它似乎工作,直到它最终“超时”。 I checked the "innerExceptions" and they reveal that the RESTORE made it to 90 percent and then returns the exception. 我检查了“innerExceptions”,他们发现RESTORE使其达到90%,然后返回异常。

Below is the code I am using to BACKUP AND RESTORE. 下面是我用于备份和恢复的代码。

Can someone please let me know where I am going wrong? 有人可以让我知道我哪里出错吗? It must be a timeout parameter somewhere that I can set but I do not know what it is or how to do it. 它必须是我可以设置的某个超时参数,但我不知道它是什么或如何做。

public class JRBackupRestoreDB
{
    public static void BackupDatabase(String databaseName, String userName, String password, String serverName, String destinationPath)
    {
        Backup sqlBackup = new Backup();

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

        sqlBackup.Database = databaseName;

        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
        ServerConnection connection = new ServerConnection(serverName, userName, password);
        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];

        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);
    }

    public static void RestoreDatabase(String databaseName, String filePath,
    String serverName, String userName, String password,
    String dataFilePath, String logFilePath)
    {
        Restore sqlRestore = new Restore();

        BackupDeviceItem deviceItem = new BackupDeviceItem(filePath, DeviceType.File);
        sqlRestore.Devices.Add(deviceItem);
        sqlRestore.Database = databaseName;

        ServerConnection connection = new ServerConnection(serverName, userName, password);
        Server sqlServer = new Server(connection);

        Database db = sqlServer.Databases[databaseName];
        sqlRestore.Action = RestoreActionType.Database;
        String dataFileLocation = dataFilePath + databaseName + ".mdf";
        String logFileLocation = logFilePath + databaseName + "_Log.ldf";
        db = sqlServer.Databases[databaseName];
        RelocateFile rf = new RelocateFile(databaseName, dataFileLocation);

        System.Data.DataTable logicalRestoreFiles = sqlRestore.ReadFileList(sqlServer);
        sqlRestore.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[0][0].ToString(), dataFileLocation));
        sqlRestore.RelocateFiles.Add(new RelocateFile(logicalRestoreFiles.Rows[1][0].ToString(), logFileLocation));

        sqlRestore.SqlRestore(sqlServer);
        db = sqlServer.Databases[databaseName];
        db.SetOnline();
        sqlServer.Refresh();
    }

}

Have you tried setting a value with ServerConnection.StatementTimeout to see what happens? 您是否尝试使用ServerConnection.StatementTimeout设置值以查看会发生什么?

MSDN reference here . MSDN 在这里参考。

This is something that'd be pretty darn easy to do in plain sql... 这在普通的sql中很容易做到...

Have you considered trying it that way? 你考虑过那样试试吗? You just have to write the sql script or procedure to do it and then run it. 您只需要编写sql脚本或过程来执行它然后运行它。 This is what we do (for a different situation, but similar): 这就是我们所做的(针对不同的情况,但类似):

backup database {{DATABASE NAME HERE}}
to disk = N'{{FILE_NAME_HERE}}'
    with
        name = N'{{BACKUP_DATABASE_NAME_HERE}}'
GO

restore database {{NEW_DATABASE_NAME}}
from disk = N'{{FILE_NAME_HERE}}'
    with
        file = 1
go

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM