internal static void ExecuteDirect(EngineCommandContext ctx, string filePath, string dbName) { if (string.IsNullOrEmpty(filePath)) { throw new Exception("Attach requires the path of the data file to attach."); } try { filePath = Path.GetFullPath(filePath); } catch { throw new InvalidPathException(); } if (!File.Exists(filePath)) { MissingFileException.Throw(Path.GetFileName(filePath)); } if (!string.IsNullOrEmpty(dbName)) { DatabaseIdentifier identifier = DatabaseIdentifier.Parse(dbName); if (identifier.IsDatabaseName) { dbName = identifier.Value; } } IDbConnection connection = ctx.ConnectionManager.BuildAttachConnection(filePath, dbName); connection.Open(); connection.Close(); Console.WriteLine("Command completed successfully."); }
internal void ExecuteDirect(EngineCommandContext ctx) { IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command.CommandText = "SELECT @@VERSION"; Console.WriteLine(command.ExecuteScalar().ToString()); } finally { connection.Close(); } }
internal void ExecuteDirect(EngineCommandContext ctx) { IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(true); string str = "SELECT owning_principal_name AS [User], instance_pipe_name AS [Pipe], OS_process_id AS [ProcessId], heart_beat AS [Status] FROM sys.dm_os_child_instances"; IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command.CommandText = str; connection.Open(); try { SqlDataReader dataReader = (SqlDataReader)command.ExecuteReader(); if (dataReader != null) { try { if (dataReader.HasRows) { ITableFormatter formatter = TableFormatterBuilder.BuildTableFormatter(dataReader); Console.WriteLine(""); Console.WriteLine(formatter.ReadHeader()); Console.WriteLine(new string('-', formatter.TotalColumnWidth)); string str2 = null; while ((str2 = formatter.ReadNextRow()) != null) { Console.WriteLine(str2); } } else { Console.WriteLine("No child instance found."); } } finally { dataReader.Close(); } } } finally { connection.Close(); } Console.WriteLine(""); }
internal static void ExecuteDirect(EngineCommandContext ctx, bool listRemote) { string[] remoteInstances; if (listRemote) { Console.WriteLine("Gathering list of remote servers..."); try { remoteInstances = SqlServerEnumerator.GetRemoteInstances(); } catch { Console.WriteLine("Failed to retrieve the remote servers."); return; } } else { try { remoteInstances = SqlServerEnumerator.GetLocalInstances(); } catch { Console.WriteLine("Failed to retrieve local instances."); return; } } Console.WriteLine(""); if ((remoteInstances != null) && (remoteInstances.Length > 0)) { foreach (string str in remoteInstances) { Console.WriteLine(str); } } else { Console.WriteLine("No SQL Server instance found."); } }
internal void ExecuteDirect(EngineCommandContext ctx, bool enable, int traceToEnable) { string str; string str2; if (traceToEnable == -1) { str = "4054,-1"; } else { str = traceToEnable.ToString() + ",-1"; } if (enable) { str2 = "TRACEON"; } else { str2 = "TRACEOFF"; } IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(); IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command.CommandText = "DBCC " + str2 + " (" + str + ")"; connection.Open(); try { command.ExecuteNonQuery(); Console.Write("Trace flags {" + str + "} were set to " + enable.ToString()); } catch (Exception exception) { Console.WriteLine("Failed to enable trace flags -- " + exception.ToString()); } finally { connection.Close(); } Console.WriteLine(""); }
internal static void ExecuteDirect(EngineCommandContext ctx) { IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { List <DatabaseInfo> list = DataUtil.BuildDatabaseInfoList(ctx.ConnectionManager, connection); if (list != null) { int num = 1; foreach (DatabaseInfo info in list) { Console.WriteLine(num + ". " + info.Name); num++; } } } finally { connection.Close(); } }
internal void ExecuteDirect(EngineCommandContext ctx, string upgradePath) { if (string.IsNullOrEmpty(upgradePath)) { throw new Exception("Upgrade requires the path of the data file to upgrade or a folder."); } string[] files = null; if (Directory.Exists(upgradePath)) { files = Directory.GetFiles(upgradePath, "*.mdf"); } else if (File.Exists(upgradePath) && (string.Compare(Path.GetExtension(upgradePath), ".mdf", StringComparison.OrdinalIgnoreCase) == 0)) { files = new string[] { upgradePath }; } if ((files == null) || (files.Length == 0)) { throw new Exception("Could not data file(s) at the location specified."); } IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); int num = this.TryGetDatabaseVersion(connection, "master"); connection.Close(); foreach (string str in files) { connection.Open(); try { if (DataUtil.IsFileAttached(ctx.ConnectionManager, connection, str) != null) { Console.WriteLine("File '" + Path.GetFileName(str) + "' is already attached to the server. Skipping file."); goto Label_019C; } } finally { connection.Close(); } Console.Write("Processing '" + Path.GetFileName(str) + "'..."); IDbConnection connection2 = ctx.ConnectionManager.BuildAttachConnection(str, 0); string databaseName = null; connection2.Open(); try { int num2; databaseName = connection2.Database; DateTime now = DateTime.Now; if (num == -1) { goto Label_0170; } Label_0123: num2 = this.TryGetDatabaseVersion(connection2, databaseName); if ((num2 != -1) && (num2 != num)) { Thread.Sleep(500); if ((DateTime.Now - now) < TimeSpan.FromMinutes(2.0)) { goto Label_0123; } } } finally { connection2.Close(); } Label_0170: if (databaseName != null) { connection.Open(); try { DataUtil.DetachDatabase(ctx.ConnectionManager, connection, databaseName, true); } finally { connection.Close(); } } Console.WriteLine(" Done"); Label_019C :; } }
void IEngineCommand.Execute(EngineCommandContext ctx) { this.ExecuteDirect(ctx, this._upgradePath); }
internal static void ExecuteDirect(EngineCommandContext ctx, string database) { 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 = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { List <DatabaseInfo> list = DataUtil.BuildDatabaseInfoList(ctx.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(ctx.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(); } }
internal static void ExecuteDirect(EngineCommandContext ctx, string filePath) { ExecuteDirect(ctx, filePath, null); }
void IEngineCommand.Execute(EngineCommandContext ctx) { ExecuteDirect(ctx, this._filePath, this._dbName); }
internal void ExecuteDirect(EngineCommandContext ctx, string scriptPath, Dictionary <string, string> variables) { new SqlConsole(ctx.Engine).Run(scriptPath, variables); }
void IEngineCommand.Execute(EngineCommandContext ctx) { ExecuteDirect(ctx, this._database, this._publicKeyFile); }
internal static void ExecuteDirect(EngineCommandContext ctx, string filePath, string databaseName) { if (string.IsNullOrEmpty(filePath)) { throw new Exception("Create requires a file path or existing database name in brackets."); } bool flag = false; DatabaseIdentifier identifier = DatabaseIdentifier.Parse(filePath); if (identifier.IsDatabaseName) { if (!string.IsNullOrEmpty(databaseName)) { throw new Exception("Database name was already specified within brackets. Should not specify database name again."); } databaseName = identifier.Value; if (string.IsNullOrEmpty(databaseName)) { throw new Exception("Invalid empty database name specified."); } } else { filePath = identifier.Value; try { filePath = Path.GetFullPath(filePath); } catch { throw new InvalidPathException(); } if (string.IsNullOrEmpty(databaseName)) { flag = true; databaseName = Path.GetFileNameWithoutExtension(filePath); if (string.IsNullOrEmpty(databaseName)) { throw new Exception("Invalid database path specified."); } } } IDbConnection connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); string str = DataUtil.QuoteDbObjectName(databaseName); if (identifier.IsDatabaseName) { command.CommandText = "CREATE DATABASE " + str; } else { command.CommandText = "CREATE DATABASE " + str + " ON (NAME='" + str + "', FILENAME='" + filePath + "')"; } command.ExecuteNonQuery(); if (flag) { DataUtil.DetachDatabase(ctx.ConnectionManager, connection, databaseName, true); } } finally { connection.Close(); } Console.WriteLine("Command completed successfully."); }
internal static void ExecuteDirect(EngineCommandContext ctx, string database, string keyPath) { IDbConnection connection; if (string.IsNullOrEmpty(database)) { throw new Exception("Sign requires a file path or existing database name in brackets."); } if (string.IsNullOrEmpty(keyPath)) { throw new Exception("Sign requires the path to the key file (.snk) that contains the encryption key."); } keyPath = PathUtil.EnsureFullPath(keyPath); if (!File.Exists(keyPath)) { MissingFileException.Throw(Path.GetFileName(keyPath)); } string dbName = null; bool flag = false; DatabaseIdentifier identifier = DatabaseIdentifier.Parse(database); database = identifier.Value; if (identifier.IsDatabaseName) { if (string.IsNullOrEmpty(database)) { throw new Exception("Invalid empty database name specified."); } connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command.CommandText = "use [" + database + "]"; command.ExecuteNonQuery(); } catch { connection.Close(); throw; } dbName = database; } else { if (!File.Exists(database)) { MissingFileException.Throw(Path.GetFileName(database)); } connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { dbName = DataUtil.IsFileAttached(ctx.ConnectionManager, connection, database); } finally { connection.Close(); } if (dbName == null) { flag = true; } connection = ctx.ConnectionManager.BuildAttachConnection(database, dbName); connection.Open(); dbName = connection.Database; } try { IDbCommand command2 = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command2.CommandText = "sp_resign_database 'ASYMMETRIC KEY', '" + keyPath + "'"; command2.ExecuteNonQuery(); string str2 = null; try { byte[] publicKey = StrongName.ExtractPublicKeyFromKeyFile(keyPath); if ((publicKey == null) || (publicKey.Length == 0)) { throw new Exception("Invalid empty public key."); } byte[] bytes = StrongName.CreatePublicKeyToken(publicKey); if ((bytes == null) || (bytes.Length == 0)) { throw new Exception("Invalid empty public key token."); } str2 = Util.FormatBytesAsHex(bytes); } catch (Exception) { } if (flag) { command2.CommandText = "use master"; command2.ExecuteNonQuery(); DataUtil.DetachDatabase(ctx.ConnectionManager, connection, dbName, true); } if (str2 != null) { Console.WriteLine("Database signed successfully with key token '" + str2 + "'."); } else { Console.WriteLine("Database signed successfully."); } } finally { connection.Close(); } }
void IEngineCommand.Execute(EngineCommandContext ctx) { ExecuteDirect(ctx, this._database, this._keyPath); }
void IEngineCommand.Execute(EngineCommandContext ctx) { ExecuteDirect(ctx, this._listRemote); }
void IEngineCommand.Execute(EngineCommandContext ctx) { this.ExecuteDirect(ctx, this._enable, this._traceToEnable); }
internal static void ExecuteDirect(EngineCommandContext ctx, string database) { IDbConnection connection; if (string.IsNullOrEmpty(database)) { throw new Exception("Shrink requires a file path or existing database name in brackets."); } bool flag = false; string dbName = null; DatabaseIdentifier identifier = DatabaseIdentifier.Parse(database); database = identifier.Value; if (identifier.IsDatabaseName) { database = identifier.Value; if (string.IsNullOrEmpty(database)) { throw new Exception("Invalid empty database name specified."); } connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command.CommandText = "use [" + database + "]"; command.ExecuteNonQuery(); } catch { connection.Close(); throw; } dbName = database; } else { if (!File.Exists(database)) { MissingFileException.Throw(Path.GetFileName(database)); } database = PathUtil.EnsureFullPath(database); connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { dbName = DataUtil.IsFileAttached(ctx.ConnectionManager, connection, database); } finally { connection.Close(); } if (dbName == null) { flag = true; } connection = ctx.ConnectionManager.BuildAttachConnection(database, dbName); connection.Open(); dbName = connection.Database; } string[] initialSizes = null; List <DataUtil.DatabaseFileInfo> databaseFiles = null; try { databaseFiles = DataUtil.GetDatabaseFiles(ctx.ConnectionManager, connection); if ((databaseFiles == null) || (databaseFiles.Count == 0)) { throw new Exception("Could not get database file information from the server."); } initialSizes = new string[databaseFiles.Count]; for (int j = 0; j < databaseFiles.Count; j++) { initialSizes[j] = databaseFiles[j].Size; } IDbCommand command2 = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command2.CommandText = "CHECKPOINT"; command2.ExecuteNonQuery(); command2.CommandText = "CHECKPOINT"; command2.ExecuteNonQuery(); command2.CommandText = "DBCC SHRINKDATABASE ([" + dbName + "], 10)"; command2.ExecuteNonQuery(); } finally { connection.Close(); } connection = ctx.ConnectionManager.BuildMasterConnection(); connection.Open(); try { IDbCommand command3 = DataUtil.CreateCommand(ctx.ConnectionManager, connection); command3.CommandText = "use [" + dbName + "]"; command3.ExecuteNonQuery(); databaseFiles = DataUtil.GetDatabaseFiles(ctx.ConnectionManager, connection); if (flag) { command3.CommandText = "use master"; command3.ExecuteNonQuery(); DataUtil.DetachDatabase(ctx.ConnectionManager, connection, dbName, true); } } finally { connection.Close(); } if ((databaseFiles == null) || (databaseFiles.Count == 0)) { throw new Exception("Could not get database file information from the server."); } string[] finalSizes = new string[databaseFiles.Count]; for (int i = 0; i < databaseFiles.Count; i++) { finalSizes[i] = databaseFiles[i].Size; } PrintSizeReport(databaseFiles, initialSizes, finalSizes); Console.WriteLine("Command completed successfully."); }
void IEngineCommand.Execute(EngineCommandContext ctx) { this.ExecuteDirect(ctx); }
internal static void ExecuteDirect(EngineCommandContext ctx, string database, string publicKeyFile) { IDbConnection connection; if (string.IsNullOrEmpty(database)) { throw new Exception("IsSigned requires a file path or existing database name in brackets."); } if (string.IsNullOrEmpty(publicKeyFile)) { throw new Exception("IsSigned requires the path to the key file (.snk) that contains the public key."); } DatabaseIdentifier identifier = DatabaseIdentifier.Parse(database); database = identifier.Value; if (identifier.IsDatabaseName) { if (string.IsNullOrEmpty(database)) { throw new Exception("Invalid empty database name specified."); } } else if (!File.Exists(database)) { MissingFileException.Throw(Path.GetFileName(database)); } if (!Path.GetExtension(publicKeyFile).Equals(".snk", StringComparison.OrdinalIgnoreCase)) { throw new Exception("Key file should have the .snk extension. Only asymmetric key files are supported."); } if (!File.Exists(publicKeyFile)) { MissingFileException.Throw(Path.GetFileName(publicKeyFile)); } publicKeyFile = PathUtil.EnsureFullPath(publicKeyFile); byte[] fileBytes = GetFileBytes(publicKeyFile); string str = "0x" + Util.FormatBytesAsHex(fileBytes); string str2 = "0x" + Util.FormatBytesAsHex(StrongName.CreatePublicKeyToken(fileBytes)); Console.WriteLine("Verifying signature using the specified key file..."); bool flag = true; if (identifier.IsDatabaseName) { connection = ctx.ConnectionManager.BuildMasterConnection(); } else { IDbConnection connection2 = ctx.ConnectionManager.BuildMasterConnection(); connection2.Open(); string dbName = null; try { dbName = DataUtil.IsFileAttached(ctx.ConnectionManager, connection2, database); } finally { connection2.Close(); } if (dbName == null) { flag = true; } connection = ctx.ConnectionManager.BuildAttachConnection(database, dbName); } connection.Open(); try { string str4 = null; if (flag) { str4 = connection.Database; } IDbCommand command = DataUtil.CreateCommand(ctx.ConnectionManager, connection); if (identifier.IsDatabaseName) { command.CommandText = "use [" + database + "]"; command.ExecuteNonQuery(); } bool flag2 = false; command.CommandText = "SELECT count(*) FROM sys.asymmetric_keys where thumbprint=" + str2 + " AND public_key=" + str; object obj2 = command.ExecuteScalar(); if ((obj2 is int) && (((int)obj2) == 1)) { command.CommandText = "SELECT count (*) FROM sys.fn_check_object_signatures('ASYMMETRIC KEY', " + str2 + ") WHERE is_signed <> 1 OR is_signature_valid <> 1"; obj2 = command.ExecuteScalar(); if ((obj2 is int) && (((int)obj2) == 0)) { flag2 = true; } } if (flag2) { Console.WriteLine("Succeeded. The signature for all database objects matched."); } else { Console.WriteLine("Failed. One or more object(s) failed signature validation."); } if (flag) { try { DataUtil.DetachDatabase(ctx.ConnectionManager, connection, str4, true); } catch (Exception) { } } } finally { connection.Close(); } }
void IEngineCommand.Execute(EngineCommandContext ctx) { this.ExecuteDirect(ctx, this._scriptPath, this._variables); }