C#/SQL : backup and restore by copy and replace database files? [closed]

£可爱£侵袭症+ 提交于 2019-12-17 14:57:15

问题


First of all, this is a some kind of share knowledge NOT a question.

I have faced some problems with creating backup and restore of database by the default method of using backup and restore commands so i have developed my own one by coping the database files and get them back when needed.

I will share it in answer to help others.


回答1:


Solution :

First of all you have to know that before any copy or replace database files, you have to set the database in offline state and get it back online after finish.

1) Used Methods

 // fullPath : the path for your database
 // executablePath : the path for your exe folder

    void setFullPath()
    {
        string executable = System.Reflection.Assembly.GetExecutingAssembly().Location;
        executablePath = (System.IO.Path.GetDirectoryName(executable));
        fullPath = executablePath + "\\Database.mdf";
    }

    void takeOffline(string fullPath)
    {
        homeObject.connection.Open();
        homeObject.cmd.CommandText = "ALTER DATABASE [" + fullPath + "] SET OFFLINE";
        homeObject.cmd.ExecuteNonQuery();
        homeObject.cmd.Clone();
    }

    void bringOnline(string fullPath)
    {
        homeObject.cmd.CommandText = "ALTER DATABASE [" + fullPath + "] SET ONLINE"; ;
        homeObject.cmd.ExecuteNonQuery();
        homeObject.cmd.Clone();
        homeObject.connection.Close();
    }

2) Copy database files / Backup

  bool getDatabaseCopy()
    {
        try
        {
            //
            takeOffline(fullPath);

            // copy database.mdf
            copyDBMDF();

            // copy database_log.ldf
            copyDB_logLDF();

            //
            bringOnline(fullPath);

            return true;
        }
        catch
        {
            //
        }

        return false;
    }

    // txtPath.txt :
    // folder location to save database files in

    void copyDBMDF()
    {
        string fileName = "Database.mdf";

        string sourceFile = fullPath;
        string targetPath = txtPath.Text;

        // Use Path class to manipulate file and directory paths.
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);
    }

    void copyDB_logLDF()
    {
        string fileName = "Database_log.ldf";

        string sourcePath = executablePath;
        string targetPath = txtPath.Text;

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);
    }

3) Replace current database files with files you copy / Restore

  bool restoreTheBackup()
    {
        try
        {
            //
            takeOffline(fullPath);

            // load .mdf
            loadMDFDatabaseFile();

            // load _log.ldf
            loadLDFDatabaseFile();

            //
            bringOnline(fullPath);

            return true;
        }
        catch
        {
            //
        }

        return false;
    }

// txtPath.txt :
// location to get database files from to replace with current files.

    void loadLDFDatabaseFile()
    {
        string fileName = "Database_log.ldf";

        string targetPath = executablePath;
        string sourcePath = txtPath.Text;

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);
    }

    void loadMDFDatabaseFile()
    {
        string fileName = "Database.mdf";

        string targetPath = executablePath;
        string sourcePath = txtPath.Text;

        // Use Path class to manipulate file and directory paths.
        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
        string destFile = System.IO.Path.Combine(targetPath, fileName);

        // To copy a folder's contents to a new location:
        // Create a new target folder, if necessary.
        if (!System.IO.Directory.Exists(targetPath))
        {
            System.IO.Directory.CreateDirectory(targetPath);
        }

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);
    }


来源:https://stackoverflow.com/questions/45646571/c-sql-backup-and-restore-by-copy-and-replace-database-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!