views:

503

answers:

4

Hi, does anyone know how to return a value from a (sql) database into an MSBuild script? I want to get the value into a property so I can pass it to subsequent build tasks.

I am fiddling round with something like the following at the moment, but this is returning -1 to indicate the query has executed successfully, whereas what I want is the actual result of the query. (SqlExecute from the community tasks).

 <SqlExecute Command="select count(*) from dbo.trade"
  ConnectionString="XXXX" >
  <Output PropertyName="TradeCount" TaskParameter="Result" />
 </SqlExecute>
 <Message Text="$(TradeCount)" />
A: 

Have you tried "select * from dbo.trade" ? According to the msbuild communitytasks docs the Result returns the number of rows for a given statement, while your statement probably returns a single row with your count contained.

flq
A: 

Tried "select * from dbo.trade", still gives the same -1.

The community task documentation says output "Output the return count/value", but it doens't go any further about how to use it.

Chris Needham
Yeah, examples of this one are really hard to find.
Michael Haren
A: 

It's often easiest to just open up the community tasks in Reflector or check the codebase directly to see how it's intended to be used.

Rob Rodi
Thank you Rob you have led me to my answer - extend the MSBuildCommunity tasks. I have extended SQLExecute task to return a scalar value, and do at some time intend update this into the community codebase. There is already some code in there to do this, but it is incomplete.
Chris Needham
+1  A: 

Rob,

Good thinking. Opening the community task in reflector reveals that the SqlExecute task is never going to return the value of a query.

this._result = command.ExecuteNonQuery();

This put me back to trying to get it to work with oSql and an Exec task...

 <Exec Command="osql -n -S $(DatabaseMachineName) -E -q &quot;select count(*) from trade&quot; -b -d $(DatabaseName)">
  <Output PropertyName="TradeCount" TaskParameter="Outputs"/>
 </Exec>

 <Message Text="Result:$(TradeCount)" />
Chris Needham