bool CreateReferenceDatabase() { if (ReferenceMDFFile.Exists() && ReferenceLDFFile.Exists()) { return(false); } var error = false; // create database + data try { CreateDatabaseFromScripts(); } catch { error = true; throw; } finally { // Detach it MasterDatabaseAgent.DetachDatabase(ReferenceDatabaseName); if (error) { ReferenceMDFFile.Delete(); ReferenceLDFFile.Delete(); } } return(true); }
bool CreateReferenceDatabase() { if (ReferenceMDFFile.Exists() && ReferenceLDFFile.Exists()) { Debug.WriteLine("Temp database. Aborted creating a new reference database for the current SQL Scripts. The DB file already exists: " + ReferenceMDFFile.FullName); return(false); } var error = false; // create database + data try { CreateDatabaseFromScripts(); } catch { error = true; throw; } finally { // Detach it MasterDatabaseAgent.DetachDatabase(ReferenceDatabaseName); if (error) { ReferenceMDFFile.Delete(harshly: true); ReferenceLDFFile.Delete(harshly: true); } } return(true); }
private bool CreateReferenceDatabase() { if (ReferenceMDFFile.Exists() && ReferenceLDFFile.Exists()) { return(false); } var error = false; // create database + data try { var start = LocalTime.Now; CreateDatabaseFromScripts(); } catch { error = true; throw; } finally { // Detach it DataAccessor.Current.DetachDatabase(ReferenceDatabaseName); if (error) { ReferenceMDFFile.Delete(harshly: true); ReferenceLDFFile.Delete(harshly: true); } } return(true); }
protected override void CloneReferenceDatabaseToTemp() { // Make sure if it exists in database already, it's deleted first. DataAccessor.Current.DeleteDatabase(TempDatabaseName); var directory = ProjectTempRoot.GetOrCreateSubDirectory("Current"); var newMDFPath = directory.GetFile(TempDatabaseName + ".mdf"); var newLDFPath = directory.GetFile(TempDatabaseName + "_log.ldf"); try { ReferenceMDFFile.CopyTo(newMDFPath); ReferenceLDFFile.CopyTo(newLDFPath); } catch (IOException ex) { if (ex.InnerException != null && ex.InnerException is UnauthorizedAccessException) { throw new Exception("Consider setting the IIS Application Pool identity to LocalSystem.", ex); } throw; } var script = "CREATE DATABASE [{0}] ON (FILENAME = '{1}'), (FILENAME = '{2}') FOR ATTACH" .FormatWith(TempDatabaseName, newMDFPath.FullName, newLDFPath.FullName); using (new DatabaseContext(DataAccessor.Current.GetMasterConnectionString())) { try { DataAccessor.Current.ExecuteNonQuery(script); } catch (SqlException ex) { throw new Exception("Could not attach the database from file " + newMDFPath.FullName + "." + Environment.NewLine + "Hint: Ensure SQL instance service has access to the folder. E.g. 'Local Service' may not have access to '{0}'" + newMDFPath.Directory.FullName, ex); } } }