using (var con = new SqlConnection("Data Source=.; Integrated Security=True;"))
{
con.Open();
var databases = con.GetSchema("Databases");
foreach (DataRow row in databases.Rows)
{
var databaseName = (String)row["database_name"];
if (databaseName.Contains(MockPrefix))
{
var command = String.Format("ALTER DATABASE [{0}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{0}]", databaseName);
new SqlCommand(command, con).ExecuteNonQuery();
Debug.WriteLine(String.Format("Removed : {0}", databaseName));
}
}
}