///<summary> /// Detaches the database. ///</summary> privatevoidDetachDatabase() { using (var connection = new SqlConnection(LocalDbMasterConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = string.Format("exec sp_detach_db '{0}'", this.DatabaseName); try { cmd.ExecuteNonQuery(); } catch { Console.WriteLine("Could not detach"); } } }
///<summary> /// Cleanups the database. ///</summary> ///<returns>System.String.</returns> privatestringCleanupDatabase() { var fileName = string.Concat(@"G:\",this.DatabaseName); try { var mdfPath = string.Concat(fileName, ".mdf"); var ldfPath = string.Concat(fileName, "_log.ldf");
var mdfExists = File.Exists(mdfPath); var ldfExists = File.Exists(ldfPath);
if (mdfExists) File.Delete(mdfPath); if (ldfExists) File.Delete(ldfPath); } catch { Console.WriteLine("Could not delete the files (open in Visual Studio?)"); } return fileName; }