使用C#代码备份和还原数据库 [英] BackUP And Restore Of Databse Using C# Code

查看:75
本文介绍了使用C#代码备份和还原数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人请帮助我
当我尝试还原数据库时,它给我错误-

Some One Please Help Me
When i Am trying to Restore a database it gives me error-

System.Data.SqlClient.SqlException: Logical file DB_Jaggry is not part of database DB_Jaggry_Restore&. Use RESTORE FILELISTONLY to list the logical file names.<br />
RESTORE DATABASE is terminating abnormally





private void button1_Click(object sender, EventArgs e)
   {
       BackupDB(@"C:\TempDB_Jaggry.bak");
       RestoreDB(@"C:\TempDB_Jaggry.bak", "DB_Jaggry");
   }



备份代码-



Code for Backup-

public static void BackupDB(string backupDestinationFilePath)
 {
    try
    {
       // Console.WriteLine("Backup operation started");
        Backup backup = new Backup();
        //Set type of backup to be performed to database
        backup.Action = BackupActionType.Database;
        backup.BackupSetDescription = "BackupDataBase description";
        //Set the name used to identify a particular backup set.
        backup.BackupSetName = "Backup";
        //specify the name of the database to back up
        backup.Database = "DB_Jaggry";
        //Set up the backup device to use filesystem.
        BackupDeviceItem deviceItem = new BackupDeviceItem(
                                        backupDestinationFilePath,
                                        DeviceType.File);
        backup.Devices.Add(deviceItem);

        // Setup a new connection to the data server
        ServerConnection connection = new ServerConnection();               
        Server sqlServer = new Server(@"SNEHA-PC\SQLEXPRESS");
        //Initialize devices associated with a backup operation.
        backup.Initialize = true;
        backup.Checksum = true;               
        backup.ContinueAfterError = true;               
        backup.LogTruncation = BackupTruncateLogType.Truncate;                
        backup.SqlBackup(sqlServer);
       MessageBox.Show("Backup operation succeeded");
    }
    catch (Exception ex)
    {
        //Console.WriteLine("Backup operation failed");
       // Console.WriteLine(ex.Message);
        MessageBox.Show(ex.ToString());
    }           
 }



还原代码-



Code For Restore-

public static void RestoreDB(string backUpFilePath, string databaseName)
    {
    try
    {            
        Restore restore = new Restore();            
        restore.Database = databaseName;
        
        restore.Action = RestoreActionType.Database;                 
        restore.Devices.AddDevice(backUpFilePath, 
DeviceType.File);           


        restore.ReplaceDatabase = true;            
        restore.NoRecovery = false;
        restore.RelocateFiles.Add(new RelocateFile("DB_Jaggry",@"C:\Temp\DB_jaggry.mdf"));
       restore.RelocateFiles.Add(new RelocateFile("DB_Jaggry_Log",@"C:\Temp\DB_Jaggry_Log.ldf"));
        ServerConnection connection = new ServerConnection(@"SNEHA-PC\SQLEXPRESS");
        Server sqlServer = new Server(connection);
        restore.SqlRestore(sqlServer);
        MessageBox.Show("Restore operation succeeded");
    }
    catch (Exception ex)
    {
        
        MessageBox.Show(ex.ToString());
    }        
}



提前感谢您...



Thnk you In Advance...

推荐答案

关注本文:
使用SMO进行备份,还原和安全目的 [ ^ ]
Follow the article :
Using SMO for Backup, Restore and Security Purposes[^]




您也可以尝试使用直接SQL查询来备份和还原数据库.您只需使用MASTER 数据库和SqlCommand Class方法的ExecuteNonQuery 方法执行该查询.

检查以下链接.
备份到磁盘设备 [ SQL SERVER –使用以下方法还原数据库备份SQL脚本(T-SQL) [
Hi,

you can also try with the direct SQL query for back up and restoring the databases. you simply execute that queries using MASTER database with ExecuteNonQuery Method of SqlCommand Class.

check these below links.
Backing up to a disk device[^]
SQL SERVER – Restore Database Backup using SQL Script (T-SQL)[^]

hope it helps.


这篇关于使用C#代码备份和还原数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆