DatabaseBackup.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace LYFZ.WanYuKeFuData.DAL
  6. {
  7. public class DatabaseBackup
  8. {
  9. public DatabaseBackup() {
  10. }
  11. /// <summary>
  12. /// 获取数据库备份文件名
  13. /// </summary>
  14. /// <param name="prefixName"></param>
  15. /// <returns></returns>
  16. public static string GetDatabaseBackupName(string prefixName)
  17. {
  18. return "(" + prefixName.Trim() + ")" + DateTime.Now.ToString("yyyy年MM月dd日HH时mm分ss秒.bak");
  19. }
  20. /// <summary>
  21. /// 执行数据库备份
  22. /// </summary>
  23. /// <param name="saveFileFullName">保存备份文件完全路径名</param>
  24. /// <returns></returns>
  25. public static string ExecutedDatabaseBackup(string saveFileFullName, string backDataBaseName,string connString)
  26. {
  27. string msg = "";
  28. StringBuilder BackupSql = new StringBuilder();
  29. BackupSql.Append("backup database @databaseName to disk=@diskPath");
  30. List<System.Data.SqlClient.SqlParameter> parameterlist = new List<System.Data.SqlClient.SqlParameter>();
  31. parameterlist.Add(new System.Data.SqlClient.SqlParameter("@diskPath", saveFileFullName));
  32. parameterlist.Add(new System.Data.SqlClient.SqlParameter("@databaseName", backDataBaseName));
  33. System.Data.SqlClient.SqlParameter[] parameters = parameterlist.ToArray();
  34. System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
  35. try
  36. {
  37. LYFZ.Helper.SQLHelper.ExecuteSql(BackupSql.ToString(), connString, parameters);
  38. msg = "数据库备份成功!";
  39. }
  40. catch (System.Data.SqlClient.SqlException ex)
  41. {
  42. msg = ex.Message;
  43. }
  44. return msg;
  45. }
  46. /// <summary>
  47. /// 执行数据库还原
  48. /// </summary>
  49. /// <param name="restoreDataBaseName">数据库备份文件所在完全路径名</param>
  50. /// <returns></returns>
  51. public static string ExecutedDatabaseRestore(string restoreDataFilePath, string restoreDataBaseName)
  52. {
  53. string msg = "";
  54. StringBuilder RestoreSql = new StringBuilder();
  55. RestoreSql.Append("/* 结束所有对当前数据库的连接 */\r\n");
  56. RestoreSql.Append("if exists(select 1 from sys.sysprocesses where dbid=db_id(@databaseName)) begin\r\n");
  57. RestoreSql.Append("declare #cs_spid cursor -- 声明游标\r\n");
  58. RestoreSql.Append("for\r\n");
  59. RestoreSql.Append("select #cs_spid=convert(varchar,spid) from sys.sysprocesses where dbid=db_id(@databaseName)\r\n");
  60. RestoreSql.Append("open #cs_spid\r\n");
  61. RestoreSql.Append("declare @spid varchar(20)\r\n");
  62. RestoreSql.Append("fetch next from #cs_spid into @spid -- 赋值并前进到下一条\r\n");
  63. RestoreSql.Append("while(@@fetch_status=0) begin -- 在fetch失败前执行\r\n");
  64. RestoreSql.Append("exec ('kill '+@spid) -- 结束对操作库的连接(exec执行SQL语句1)\r\n");
  65. RestoreSql.Append("fetch next from #cs_spid into @spid\r\n");
  66. RestoreSql.Append("end\r\n");
  67. RestoreSql.Append("close #cs_spid\r\n");
  68. RestoreSql.Append("deallocate #cs_spid -- 释放游标\r\n");
  69. RestoreSql.Append("end\r\n");
  70. RestoreSql.Append("/* 还原数据库 */\r\n");
  71. RestoreSql.Append("restore database @databaseName from disk=@diskPath");
  72. List<System.Data.SqlClient.SqlParameter> parameterlist = new List<System.Data.SqlClient.SqlParameter>();
  73. parameterlist.Add(new System.Data.SqlClient.SqlParameter("@databaseName", restoreDataBaseName));
  74. parameterlist.Add(new System.Data.SqlClient.SqlParameter("@diskPath", restoreDataFilePath));
  75. System.Data.SqlClient.SqlParameter[] parameters = parameterlist.ToArray();
  76. System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
  77. try
  78. {
  79. LYFZ.Helper.SQLHelper.ExecuteSqlToMaster(RestoreSql.ToString(), parameters);
  80. msg = "数据库还原成功!";
  81. }
  82. catch (System.Data.SqlClient.SqlException ex)
  83. {
  84. msg = (ex.Message);
  85. }
  86. return msg;
  87. }
  88. /// <summary>
  89. /// 获取待备份的数据库集合
  90. /// </summary>
  91. /// <returns></returns>
  92. public static List<string> GetWaitDatabaseBackupList()
  93. {
  94. List<string> waitBakDataBaseNameList = new List<string>();
  95. string sql = "select name From sysdatabases order by dbid asc ";
  96. string[] ignoreDbNames=new string[]{"master","tempdb","model","msdb","ReportServer","ReportServerTempDB"};
  97. System.Data.DataTable tb= LYFZ.Helper.SQLHelper.MasterQuery(sql,null).Tables[0];
  98. foreach (System.Data.DataRow row in tb.Rows)
  99. {
  100. string dbName=row["name"].ToString();
  101. if (!String.IsNullOrEmpty(dbName) && !ignoreDbNames.Any(n => n.ToLower() == dbName.ToLower()))
  102. {
  103. waitBakDataBaseNameList.Add(dbName);
  104. }
  105. }
  106. return waitBakDataBaseNameList;
  107. }
  108. }
  109. }