Csharp/C#教程:C#/ SQL:通过复制和替换数据库文件进行备份和还原?分享


C#/ SQL:通过复制和替换数据库文件进行备份和还原?

首先,这是一种分享知识而不是一个问题。

我通过使用备份和恢复命令的默认方法遇到了创建数据库备份和恢复的一些问题, 因此我通过处理数据库文件并在需要时将其恢复,开发了自己的数据库。

我将分享它以回答帮助他人。

方案:

首先,您必须知道在复制或替换数据库文件之前,您必须将数据库设置为脱机状态,并在完成后将其重新联机。

上述就是C#学习教程:C#/ SQL:通过复制和替换数据库文件进行备份和还原?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)

1)使用的方法

  // 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)复制数据库文件/备份

  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)用您复制/还原的文件替换当前数据库文件

  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://www.ctvol.com/cdevelopment/1025642.html

(0)
上一篇 2022年1月8日
下一篇 2022年1月8日

精彩推荐