Sqlserver
 sql >> Baza danych >  >> RDS >> Sqlserver

C#/SQL:tworzenie kopii zapasowych i przywracanie przez kopiowanie i zastępowanie plików bazy danych?

Rozwiązanie:

Przede wszystkim musisz wiedzieć, że przed kopiowaniem lub wymianą plików bazy danych musisz ustawić bazę danych w stan offline i przywrócić ją do trybu online po zakończeniu.

1) Używane metody

 // 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) Kopiuj pliki bazy danych / Kopia zapasowa

  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) Zastąp bieżące pliki bazy danych plikami, które kopiujesz/przywracasz

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



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. jak mogę zaktualizować 100 najlepszych rekordów na serwerze sql?

  2. Wstaw dane varbinary do bazy danych SQL Server

  3. Po zakleszczeniu jednej transakcji w różnych wersjach programu SQL Server

  4. Konwertuj „datetime2” na „date” w SQL Server (przykłady T-SQL)

  5. wyświetlić zmienione wartości po zaktualizowaniu instrukcji