As part of sql server 2005, there is the bulk copy command “bcp.exe” (http://msdn.microsoft.com/en-us/library/ms162802%28SQL.90%29.aspx)
We are running the bcp command from our msbuild script using the exec task. Unfortunately, when bcp fails to load a row of data, the build still succeeds.
I tried (per build script snippet below) specifying an error file, and checking for its existence, unfortunately that means the build always fails even if bcp completely succeeds, because it appears that bcp will always create the error file even if there are no errors.
<ItemGroup>
<bcpFiles Include="$(DataPath)\*.txt" />
</ItemGroup>
<Delete Files="BcpErrors.txt" />
<Message Text="bcp $(DatabaseName).dbo.%(bcpFiles.FileName) in %(bcpFiles.FullPath) -eBcpErrors.txt -c -E -q -t"`" -r"`\n" $(bcpConnectionString)" />
<Exec Command="bcp $(DatabaseName).dbo.%(bcpFiles.FileName) in %(bcpFiles.FullPath) -eBcpErrors.txt -c -E -q -t"`" -r"`\n" $(bcpConnectionString)" />
<Error Condition="Exists('BcpErrors.txt')" Text="One or more bcp commands contained errors." />
Is there any way I can get msbuild to fail the build if bcp fails to load any of the data?
Ok, so it’s not exactly using bcp.exe, but BULK INSERT is very similar to bcp.exe, can take the same format of files, and has most of the same options. The Sql.Execute task will also fail if the BulkInsert query fails. So I ended up with
(Sql.Execute task is defined in Microsoft.Sdc.Common.tasks)