public static Exception RestoreDatabase(Info_Server info_Server, string databaseName, string filePath) { conn = new ServerConnection(info_Server.Server_Name, info_Server.User, info_Server.Pass); //conn.ServerInstance = serverName; srv = new Server(conn); try { Restore res = new Restore(); res.Devices.AddDevice(filePath, DeviceType.File); RelocateFile DataFile = new RelocateFile(); string MDF = res.ReadFileList(srv).Rows[0][1].ToString(); DataFile.LogicalFileName = res.ReadFileList(srv).Rows[0][0].ToString(); DataFile.PhysicalFileName = srv.Databases[databaseName].FileGroups[0].Files[0].FileName; RelocateFile LogFile = new RelocateFile(); string LDF = res.ReadFileList(srv).Rows[1][1].ToString(); LogFile.LogicalFileName = res.ReadFileList(srv).Rows[1][0].ToString(); LogFile.PhysicalFileName = srv.Databases[databaseName].LogFiles[0].FileName; res.RelocateFiles.Add(DataFile); res.RelocateFiles.Add(LogFile); res.Database = databaseName; res.NoRecovery = false; res.ReplaceDatabase = true; res.SqlRestore(srv); conn.Disconnect(); } catch (SmoException ex) { return(new Exception(ex.Message, ex.InnerException)); } catch (IOException ex) { return(new Exception(ex.Message, ex.InnerException)); } return(null); }
bool SQLServer_Running(Info_DB DB, Info_Server instansce) { string Mess = ""; Exception er = null; //gán cờ er bằng null chỉ thị ko có lỗi Thread.CurrentThread.IsBackground = true; er = DB.Download_BackupFile(fTPServer); if (er != null) { Mess = er.Message + ": " + ((er.InnerException == null) ? "" : er.InnerException.Message); File_Read_Write.Write_File(log_path, DateTime.Now + ": Error - " + DB.DBTarget + ": " + Mess, true); SendEmail.Send_Email(DB.Email, null, "[AMS - TMS] Clone Database error!", "Server: " + instansce.Server_Name + @"\nDatabase name: " + DB.DBTarget + "\n" + Mess, false); return(false); } else { File_Read_Write.Write_File(log_path, DateTime.Now + ": Download Backup file " + fTPServer.URL + "BackupDB_zip/" + DB.ServerSource + "/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + DB.DBSource + ".zip....done!", true); } er = DB.Excute_Restore_DB(instansce); if (er != null) { Mess = er.Message + ": " + ((er.InnerException == null) ? "" : er.InnerException.Message); File_Read_Write.Write_File(log_path, DateTime.Now + ": Error - " + DB.DBTarget + ": " + Mess, true); SendEmail.Send_Email(DB.Email, null, "[AMS - TMS] Clone Database error!", "Server: " + instansce.Server_Name + @"\nDatabase name: " + DB.DBTarget + "\n" + Mess, false); return(false); } else { File_Read_Write.Write_File(log_path, DateTime.Now + ": Restore " + DB.ServerSource + @"\" + DB.DBSource + " to " + instansce.Server_Name + @"\" + DB.DBTarget + " done!", true); } SendEmail.Send_Email(DB.Email, null, "[AMS - TMS] Clone Database succufully!", "Clone Database " + DB.ServerSource + @"\" + DB.DBSource + " to " + instansce.Server_Name + @"\" + DB.DBTarget + " succufully!", false); return(true); }