我有一个 C# 项目,它是一个控制台应用程序,它使用 DbUp 将脚本运行到 sql server 数据库,如果 .sql 文件中存在任何语法问题,有没有办法停止构建过程
我尝试了几种方法,更改项目文件以通过 MS build 来完成,但没有成功
是的,您可以在构建项目之前检查脚本是否有效。
就像这样:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<!--This is the key part-->
<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="xxx" />
</Target>
<!--This is the key part-->
</Project>
将命令 xxx 替换为验证脚本。
例如:
sqlcmd -S xxx -d xxx -U your_username -P your_password -i "xxx" -b
如果在这种情况下出现错误,-b 将终止批处理作业。