1

我正在使用 SQlPackage.exe 来部署/发布数据库项目。我想在单独的日志文件中记录发布活动,例如创建数据库/表或任何修改。但似乎 sqlpackage.exe 中没有选项来记录此信息

其次,如果我以某种方式停止之间的 sqlpackage 部署(因为我使用的是 bat 文件并且从那里我调用 sqlpackage.exe 命令),那么它不会回滚所有更改。

请注意,我已经启用了包含跨国脚本的选项。通过启用此功能,部署后脚本不在事务块中。换句话说,如果事务后脚本中有错误但架构中没有错误,则架构部分将被正确部署,这将在部署后脚本中引发错误。因此我的数据库处于不一致状态。我的观点是,如果有任何错误,它应该回滚所有内容。

4

1 回答 1

0

您还可以从 C# 调用 SqlPackage.exe 并将其包装在 ProcessStartInfo 中(即从 C# 中执行 shell 命令。我从堆栈溢出的其他地方获取此代码并修改它以在发生错误时执行 Console.ReadLine()我们处于调试模式;想象您传入的命令是 SqlPackage.exe;然后您可以将错误消息更改为红色并暂停控制台窗口:

    public void ExecuteCommandSync(object command, string message)
    {
        Console.WriteLine(message);
        Console.WriteLine("------------------------------------------------------------------- ");
        Console.WriteLine(" ");

        Console.WriteLine("Executing command: " + command);

        Console.WriteLine(" ");
        Console.WriteLine("------------------------------------------------------------------- ");
        Console.WriteLine(" ");

        // create the ProcessStartInfo using "cmd" as the program to be run,
        // and "/c " as the parameters.
        // Incidentally, /c tells cmd that we want it to execute the command that follows,
        // and then exit.
        var procStartInfo = new ProcessStartInfo("cmd", "/c " + command)
        {
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        // The following commands are needed to redirect the standard output.
        // This means that it will be redirected to the Process.StandardOutput StreamReader.
        // Do not create the black window.
        // Now we create a process, assign its ProcessStartInfo and start it
        var proc = new Process { StartInfo = procStartInfo };
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.Start();

        // Get the output into a string
        var result = proc.StandardOutput.ReadToEnd();

        string err = proc.StandardError.ReadToEnd();

        // write the error and pause (if DEBUG)

        if (err != string.Empty)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine(err);
            Console.ResetColor();

#if DEBUG                
            Console.WriteLine("Press enter to continue...");
            Console.ReadLine();
#endif
        }


        proc.WaitForExit();

        // Display the command output.
        Console.WriteLine(result);

        Console.WriteLine(" ");
        Console.WriteLine("------------------------------------------------------------------- ");
        Console.WriteLine(" ");

        Console.WriteLine("Finished executing command: " + command);

        Console.WriteLine(" ");
        Console.WriteLine("------------------------------------------------------------------- ");
        Console.WriteLine(" ");
    }
于 2016-03-25T07:36:43.377 回答