在文件中记录sqlpackage.exe错误

时间:2014-05-29 08:04:08

标签: sql-server transactions database-project sql-server-data-tools sqlpackage

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

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

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

1 个答案:

答案 0 :(得分: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(" ");
    }