NeedHack
NeedHack

Reputation: 3011

MSBuild return value from database

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)" />

Upvotes: 2

Views: 1746

Answers (4)

NeedHack
NeedHack

Reputation: 3011

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)" />

Upvotes: 1

Rob Rodi
Rob Rodi

Reputation: 3494

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.

Upvotes: 0

NeedHack
NeedHack

Reputation: 3011

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.

Upvotes: 0

flq
flq

Reputation: 22859

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.

Upvotes: 0

Related Questions