首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文件中记录sqlpackage.exe错误

在文件中记录sqlpackage.exe错误
EN

Stack Overflow用户
提问于 2014-05-29 16:04:08
回答 1查看 2.7K关注 0票数 1

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

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

注意:我已经启用了包括跨国脚本选项。通过启用此选项,部署后脚本将不在事务块中。换句话说,如果在事务后脚本中有错误,但在模式中没有错误,那么模式部分将得到正确部署,这将在部署后脚本中抛出错误。因此,我的数据库处于不一致状态。我的观点是,如果有任何错误,它也应该回滚所有内容。

EN

回答 1

Stack Overflow用户

发布于 2016-03-25 15:36:43

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

代码语言:javascript
复制
    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(" ");
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23928594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档