public static void DropDatabase(string connectionName) { using ( var sqlConnection = new SqlConnection( ConfigurationManager.ConnectionStrings[connectionName] .ConnectionString)) { var serverConnection = new ServerConnection(sqlConnection); var server = new Microsoft.SqlServer.Management.Smo.Server( serverConnection); if (server.Databases[sqlConnection.Database] != null) { server.KillDatabase(sqlConnection.Database); } } }
public bool DropDatabase(string connectionName) { try { using (var sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[connectionName].ConnectionString)) { var serverConnection = new ServerConnection(sqlConnection); var server = new Microsoft.SqlServer.Management.Smo.Server(serverConnection); server.KillDatabase(sqlConnection.Database); return(true); } } catch (Exception) { return(false); } }
private void BtRestauracao_Click(object sender, EventArgs e) { try { OpenFileDialog d = new OpenFileDialog(); d.Filter = "Backup Files|*.bak"; d.ShowDialog(); if (d.FileName != "") { String nomeBanco = DadosDaConexao.banco; String localBackup = d.FileName; String conexao = @"Data Source=" + DadosDaConexao.servidor + ";Initial Catalog=master;User="******";Password="******"Backup restaurado com sucesso!!!!!"); } } catch (Exception erro) { MessageBox.Show(erro.Message); } }
protected override void DoWork(object state) { var mainCancellationTokenSource = new CancellationToken(); try { var tasks = new List <Task>(); var deploymentJobs = _deploymentJobService.Get(p => p.Server.Code == ServerId && (p.MarkToRemove ?? false) && !(p.IsRemoveDone ?? false)).Take(3).ToList(); if (!deploymentJobs.Any()) { return; } var listJobUpdated = new List <DeploymentJob>(); foreach (var job in deploymentJobs) { var localJob = job; var t = Task.Factory.StartNew(() => { //path var serviceDest = localJob.Configuration.DestinationWindowServicePath; var webDest = localJob.Configuration.DestinationWebPath; var webDestApi = localJob.Configuration.DestinationWebApiPath; //uninstall servie if (localJob.JobType == (int)JobType.WindowService && Directory.Exists(serviceDest)) { try { StopService(localJob.Configuration.ServiceName, serviceDest + @"\" + localJob.Configuration.ServiceFileName); if (Directory.Exists(serviceDest)) { Directory.Delete(serviceDest, true); } } catch (Exception exception) { _diagnosticService.Error(exception); } } //unstall host if (localJob.JobType == (int)JobType.WebApp) { if (!localJob.Configuration.WebDomainName.Contains("http")) { localJob.Configuration.WebDomainName = "http://" + localJob.Configuration.WebDomainName + "/"; } var uri = new Uri(localJob.Configuration.WebDomainName); _iisHostingHelper.RemoveHost(uri.Host); if (Directory.Exists(webDest)) { Directory.Delete(webDest, true); } } if (localJob.JobType == (int)JobType.WebApi) { if (!localJob.Configuration.WebApiDomainName.Contains("http")) { localJob.Configuration.WebApiDomainName = "http://" + localJob.Configuration.WebApiDomainName + "/"; } var uri = new Uri(localJob.Configuration.WebApiDomainName); _iisHostingHelper.RemoveHost(uri.Host); if (Directory.Exists(webDestApi)) { Directory.Delete(webDestApi, true); } } //delete database if (localJob.JobType == (int)JobType.Database) { var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MainDb"].ConnectionString); var server = new Server(new ServerConnection(conn)); var db = server.Databases[localJob.Configuration.DatabaseName]; if (db != null) { server.KillAllProcesses(localJob.Configuration.DatabaseName); server.KillDatabase(localJob.Configuration.DatabaseName); db.Drop(); } var login = server.Logins[localJob.Configuration.DatabaseUsername]; if (login != null) { login.Drop(); } } localJob.IsCopySourceDone = false; localJob.IsRemoveDone = true; listJobUpdated.Add(localJob); }, mainCancellationTokenSource); tasks.Add(t); } Task.WaitAll(tasks.ToArray(), mainCancellationTokenSource); _deploymentJobService.UpdateListJobs(listJobUpdated); } catch (Exception ex) { _diagnosticService.Error(ex); } }