internal bool Initialize(ConnectionOptions connectionOptions) { this.connectionOptions = connectionOptions; if (connectionOptions.serverName == null) { if (false) //ProgramInfo.InSQLMode { string[] localInstances = null; try { localInstances = SqlServerEnumerator.GetLocalInstances(); } catch (Exception) { } if ((localInstances != null) && (localInstances.Length > 0)) { this.connectionOptions.serverName = localInstances[0]; } else { this.connectionOptions.serverName = "."; } } else { this.connectionOptions.serverName = @".\SQLEXPRESS"; } } if (connectionOptions.promptForPassword) { //try //{ // this.connectionOptions.password = ConsoleUtil.PromptForPassword("Enter password for user '" + connectionOptions.userName + "': "); // Console.WriteLine(""); //} //catch (UserCanceledException) //{ // return false; //} } if (!string.IsNullOrEmpty(connectionOptions.runningAs) && !connectionOptions.useMainInstance) { IDbConnection connection = this.BuildMasterConnection(true); string str = "SELECT owning_principal_name, instance_pipe_name FROM sys.dm_os_child_instances"; IDbCommand command = DataUtil.CreateCommand(this, connection); command.CommandText = str; connection.Open(); try { SqlDataReader reader = (SqlDataReader)command.ExecuteReader(); if (reader != null) { try { if (reader.HasRows) { int ordinal = reader.GetOrdinal("owning_principal_name"); int num2 = reader.GetOrdinal("instance_pipe_name"); while (reader.Read()) { if (string.Equals(reader.GetString(ordinal), connectionOptions.runningAs, StringComparison.OrdinalIgnoreCase)) { string str3 = reader.GetString(num2); if (this.originalServerName == null) { Console.WriteLine("Using instance '" + str3 + "'."); Console.WriteLine(""); this.originalServerName = connectionOptions.serverName; this.connectionOptions.serverName = str3; this.connectionOptions.useMainInstance = true; } else { Console.WriteLine("Multiple child instances were found to run under the principal name you specified. The first one will be used."); } } } } if (this.originalServerName == null) { throw new Exception("No child instance is running under the principal name you specified."); } } finally { reader.Close(); } } } finally { connection.Close(); } } return true; }
internal static void DetachDatabase(string database) { ConnectionOptions connectionOptions = new ConnectionOptions(); ConnectionManager connectionManager = new ConnectionManager(); connectionManager.Initialize(connectionOptions); if (string.IsNullOrEmpty(database)) { throw new Exception("Detach requires a file path or database name (within brackets)."); } bool flag = false; DatabaseIdentifier identifier = DatabaseIdentifier.Parse(database); database = identifier.Value; if (identifier.IsDatabaseName) { if (string.IsNullOrEmpty(database)) { throw new Exception("Invalid database name specified."); } } else { if (database.EndsWith("*")) { database = database.Substring(0, database.Length - 1); flag = true; } database = PathUtil.EnsureFullPath(database); } IDbConnection connection = connectionManager.BuildMasterConnection(); connection.Open(); try { List<DatabaseInfo> list = DataUtil.BuildDatabaseInfoList(connectionManager, connection); if (list != null) { bool flag2 = false; foreach (DatabaseInfo info in list) { bool flag3 = false; try { if (identifier.IsDatabaseName) { if (string.Equals(database, info.Name, StringComparison.OrdinalIgnoreCase)) { flag3 = true; } } else { string b = PathUtil.EnsureFullPath(info.Path); if (flag) { if (b.StartsWith(database, StringComparison.OrdinalIgnoreCase)) { flag3 = true; } } else if (string.Equals(database, b, StringComparison.OrdinalIgnoreCase)) { flag3 = true; } } if (flag3 && (((string.Compare(info.Name, "master", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(info.Name, "tempdb", StringComparison.OrdinalIgnoreCase) == 0)) || ((string.Compare(info.Name, "model", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(info.Name, "msdb", StringComparison.OrdinalIgnoreCase) == 0)))) { Console.WriteLine("Warning: Not detaching system database '{0}'. Use SQL commands in the console to do it.", info.Name); flag3 = false; } } catch { } if (flag3) { DataUtil.DetachDatabase(connectionManager, connection, info.Name, false); flag2 = true; } } if (!flag2) { if (identifier.IsDatabaseName) { Console.WriteLine("No valid database name matches the value specified."); } else { Console.WriteLine("No valid database path matches the value specified."); } } } } finally { connection.Close(); } }