Fix additional error conditions for issue #5628

This commit is contained in:
BlueSkeye_cp 2012-09-19 17:15:19 +00:00
parent 4acc61602c
commit 6a91eeb14f

View file

@ -9,6 +9,7 @@ using System.Data.Common;
using System.Data.Objects; using System.Data.Objects;
using System.Data.Objects.DataClasses; using System.Data.Objects.DataClasses;
using System.Reflection; using System.Reflection;
using System.Text;
using Microsoft.Win32; using Microsoft.Win32;
using Microsoft.Samples.Debugging.CorSymbolStore; using Microsoft.Samples.Debugging.CorSymbolStore;
using System.Diagnostics.SymbolStore; using System.Diagnostics.SymbolStore;
@ -40,24 +41,29 @@ namespace Cosmos.Debug.Common {
int databaseId = 0; int databaseId = 0;
bool xExists = false; bool xExists = false;
using (var xCmd = xConn.CreateCommand()) { using (var xCmd = xConn.CreateCommand())
{
xCmd.CommandText = "select database_id from sys.databases where name = '" + aDbName + "'"; xCmd.CommandText = "select database_id from sys.databases where name = '" + aDbName + "'";
object rawId = xCmd.ExecuteScalar(); object rawId = xCmd.ExecuteScalar();
if (null != rawId) { if (null != rawId)
{
databaseId = (int)rawId; databaseId = (int)rawId;
xExists = true; xExists = true;
} }
} }
if (xExists) { if (xExists)
{
List<string> databaseFiles = new List<string>(); List<string> databaseFiles = new List<string>();
bool damagedDatabase = false; bool damagedDatabase = false;
using (var xCmd = xConn.CreateCommand()) { using (var xCmd = xConn.CreateCommand())
xCmd.CommandText = "SELECT [physical_name] FROM [master].[sys].[master_files] WHERE [database_id] = " + databaseId; {
using (SqlDataReader reader = xCmd.ExecuteReader()) { xCmd.CommandText = "SELECT [physical_name] FROM [master].[sys].[master_files] WHERE [database_id] = " + databaseId.ToString();
if (reader.NextResult()) { using (SqlDataReader reader = xCmd.ExecuteReader())
while(reader.Read()) { {
while (reader.Read())
{
string filePath = reader.GetString(0); string filePath = reader.GetString(0);
if (!File.Exists(filePath)) { damagedDatabase = true; } if (!File.Exists(filePath)) { damagedDatabase = true; }
@ -65,29 +71,35 @@ namespace Cosmos.Debug.Common {
} }
} }
} }
} if (!damagedDatabase)
if (!damagedDatabase) { {
// Necessary to because of SQL pooled connections etc, even if all our connections are closed. // Necessary to because of SQL pooled connections etc, even if all our connections are closed.
using (var xCmd = xConn.CreateCommand()) { using (var xCmd = xConn.CreateCommand())
{
xCmd.CommandText = "ALTER DATABASE " + aDbName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE"; xCmd.CommandText = "ALTER DATABASE " + aDbName + " SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
xCmd.ExecuteNonQuery(); xCmd.ExecuteNonQuery();
} }
}
// Yes this throws an exception if the database doesnt exist, so we have to run it only if we // Yes this throws an exception if the database doesnt exist, so we have to run it only if we
// know it exists. This will detach and also delete the physical files. // know it exists. This will detach and also delete the physical files.
using (var xCmd = xConn.CreateCommand()) { using (var xCmd = xConn.CreateCommand())
{
xCmd.CommandText = "DROP DATABASE " + aDbName; xCmd.CommandText = "DROP DATABASE " + aDbName;
xCmd.ExecuteNonQuery(); xCmd.ExecuteNonQuery();
} }
} if (damagedDatabase)
else { {
// Just detach database. //// Just detach database.
using (var xCmd = xConn.CreateCommand()) { //using (var xCmd = xConn.CreateCommand())
xCmd.CommandText = string.Format("sp_detach_db '{0}', 'true'" + aDbName); //{
xCmd.ExecuteNonQuery(); // xCmd.CommandText = string.Format("sp_detach_db '{0}', 'true'", aDbName);
} // xCmd.ExecuteNonQuery();
//}
// And try to cleanup remaining files. // And try to cleanup remaining files.
foreach (string filePath in databaseFiles) { foreach (string filePath in databaseFiles)
try { File.Delete(filePath); } catch { } {
try { File.Delete(filePath); }
catch { }
} }
} }
} }