コード例 #1
0
        public override bool Execute()
        {
            using (var connection = GetSqlConnection(ServerName, DatabaseName))
            {
                connection.Open();

                int             rowsAffected;
                RunScriptResult result = RunScript(new BuildLogTextWriter(Log, false),
                                                   new BuildLogTextWriter(Log, true), connection, ScriptFile, RunScriptOptions.None,
                                                   out rowsAffected);

                if (result == RunScriptResult.Succeeded)
                {
                    return(true);
                }

                Log.LogError("Database script '" + ScriptFile + "' failed.");
                return(false);
            }
        }
コード例 #2
0
ファイル: ScriptHelper.cs プロジェクト: formist/LinkMe
        /// <summary>
        /// Runs the scripts listed in install.db.txt in the specified directory. If successfull returns null, otherwise
        /// returns the path of the script file that failed.
        /// </summary>
        public static string RunScriptList(TextWriter output, TextWriter error, string listFilePath,
                                           string serverName, string databaseName, string login, string password)
        {
            if (string.IsNullOrEmpty(listFilePath))
            {
                throw new ArgumentException("The script list file path must be specified.", "listFilePath");
            }
            if (string.IsNullOrEmpty(login) ^ string.IsNullOrEmpty(password))
            {
                throw new ArgumentException("Either both the login and the password must be specified or"
                                            + " neither must be specified to use integrated security.");
            }

            if (!File.Exists(listFilePath))
            {
                throw new FileNotFoundException("The script list file, '" + listFilePath + "', does not exist.",
                                                listFilePath);
            }

            Stopwatch stopwatch = Stopwatch.StartNew();

            output.WriteLine("Processing script list file '" + listFilePath + "'...");

            if (string.IsNullOrEmpty(serverName))
            {
                serverName = DefaultScriptListServer;
            }
            if (string.IsNullOrEmpty(databaseName))
            {
                databaseName = DefaultScriptListDatabase;
            }

            string connString = GetSqlConnectionString(serverName, databaseName, login, password);

            IList <string> scriptList = FileUtils.ReadFileList(listFilePath);

            output.WriteLine("Connecting to database '{0}' on server '{1}'...", databaseName, serverName);

            var listener = new SqlMessageListener(output);
            int totalRowsAffected = 0, succeeded = 0, skipped = 0;

            if (scriptList.Count != 0)
            {
                using (var connection = new SqlConnection(connString))
                {
                    connection.InfoMessage += listener.ConnectionInfoMessage;
                    connection.Open();

                    foreach (string filePath in scriptList)
                    {
                        int             scriptFileRowsAffected;
                        RunScriptResult scriptFileResult = RunScript(output, error, connection, filePath,
                                                                     RunScriptOptions.UseDatabaseScriptRecords, out scriptFileRowsAffected);
                        totalRowsAffected += scriptFileRowsAffected;

                        switch (scriptFileResult)
                        {
                        case RunScriptResult.Failed:
                            return(filePath);

                        case RunScriptResult.Succeeded:
                            succeeded++;
                            break;

                        case RunScriptResult.Skipped:
                            skipped++;
                            break;

                        default:
                            throw new ApplicationException("Unexpected value of RunScriptResult: "
                                                           + scriptFileResult);
                        }
                    }
                }
            }

            stopwatch.Stop();
            output.WriteLine("Finished processing script list file '{0}' in {1}.{2}{3} scripts succeeded,"
                             + " {4} skipped. {5} rows affected.",
                             listFilePath, stopwatch.Elapsed, System.Environment.NewLine, succeeded, skipped, totalRowsAffected);

            if (listener.Messages.Count > 0)
            {
                output.WriteLine();
                output.WriteLine("{0} info/warning message{1} generated:", listener.Messages.Count,
                                 (listener.Messages.Count == 1 ? " was" : "s were"));
                output.WriteLine();

                foreach (string message in listener.Messages)
                {
                    output.WriteLine(message);
                }
            }

            return(null);
        }