public static string GetBackupPathById(int id) { TopFashion.SQLDBHelper SqlHelper = new TopFashion.SQLDBHelper(); object obj = SqlHelper.GetSingle("select Path from Record where ID=" + id, true); if (obj != null && obj != DBNull.Value) { string filename = obj.ToString(); FileInfo fi = new FileInfo(filename); if (fi.Directory != null) { return(fi.Directory.Parent.FullName); } } return(""); }
/// < summary> /// SQL恢复数据库 /// < /summary> /// <param name="id">备份集ID</param> /// <param name="Restore_PercentComplete">进度</param> /// <param name="oRestore">数据库还原服务对象</param> public static bool SQLDbRestore(int id, SQLDMO.RestoreSink_PercentCompleteEventHandler Restore_PercentComplete, out SQLDMO.Restore oRestore) { oRestore = null; TopFashion.SQLDBHelper SqlHelper = new TopFashion.SQLDBHelper(); object obj = SqlHelper.GetSingle("select Path from Record where ID=" + id, true); if (obj != null && obj != DBNull.Value) { string filename = obj.ToString(); SqlConnectionStringBuilder connStrBuilder = new SqlConnectionStringBuilder(TopFashion.Configs.ConnString); string ServerIP = connStrBuilder.DataSource; if (ServerIP == "." || ServerIP == "(local)" || ServerIP == "127.0.0.1") { } else { StringBuilder TargetDir = new StringBuilder(); TargetDir.Append(@"\\"); TargetDir.Append(ServerIP); TargetDir.Append("\\"); TargetDir.Append("DatabaseBackup"); TargetDir.Append("\\"); filename = TargetDir + Path.GetFileName(filename); } if (File.Exists(filename)) { string LoginUserName = connStrBuilder.UserID; string LoginPass = connStrBuilder.Password; string DBName = connStrBuilder.InitialCatalog; oRestore = new SQLDMO.RestoreClass(); SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass(); try { oSQLServer.LoginSecure = false; oSQLServer.Connect(ServerIP, LoginUserName, LoginPass); //因为数据库正在使用,所以无法获得对数据库的独占访问权。不一定是由于其他进程的占用,还有其他的原因,所以要脱机再联机... //KillProcess(DBName); //KillSqlProcess(oSQLServer, DBName); //OffAndOnLine(DBName); OffLine(DBName); oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database; oRestore.PercentComplete += Restore_PercentComplete; oRestore.Database = DBName; oRestore.Files = @"" + string.Format("[{0}]", filename) + ""; oRestore.FileNumber = 1; oRestore.ReplaceDatabase = true; oRestore.SQLRestore(oSQLServer);//这里可能存在问题! OnLine(DBName); return(true); } catch (Exception e) { TopFashion.WriteLog.CreateLog("数据库操作", "SQLDbRestore", "error", "恢复时出错:" + e.Message); } finally { oSQLServer.DisConnect(); } } else { TopFashion.WriteLog.CreateLog("数据库操作", "SQLDbRestore", "error", "找不到要还原的备份数据库文件 [" + filename + "]"); } } return(false); }