PDA

View Full Version : IDbParametersBuilder Help



BryanB
08-30-2007, 06:51 PM
I have a DAO that extends Spring.Data.Core.AdoDaoSupport. I have a Save(object) method wherein I do a AdoTemplate.Execute(delegate(IDbCommand)).

Within my delegate method I am attempting to build parameters for a SQL stored procedure using the IDbParametersBuilder. This is all working up to the point where I would like to add my newly created parameters to my IDbCommand. I have attempted to do:


IDbParameters prms = builder.GetParameters();
foreach (IDbDataParameter prm in prms.DataParameterCollection)
command.Parameters.Add(prm);


I get the following exception:


System.ArgumentException : The SqlParameter is already contained by another SqlParameterCollection.


I have also explored using the the AdoTemplate.ExecuteNonQuery(...)but i need to return a business object after the stored proc has been called.

Any suggestions?

Thanks in advance!
Bryan

BryanB
08-30-2007, 08:04 PM
I wasn't able to find the solution readily in any documentation but I was able to dig through the Spring.Data source code and found references here and there to a Spring.Data.Support.ParameterUtils class with a couple-a very useful static methods. One was ParameterUtils.CopyParameters(IDbCommand, IDbParameters). And it worked beautifully. In case anyone was wondering.

Regards!

Mark Pollack
08-30-2007, 08:18 PM
Hi Bryan,

Glad you found what you needed. I was thinking though, can you use instead a RowMapper or ResultSetExtractor, the methods on AdoTemplate are



public IList QueryWithRowMapper(CommandType cmdType, string cmdText, IRowMapper rowMapper, IDbParameters parameters)


public object QueryWithResultSetExtractor(CommandType cmdType, string cmdText, IResultSetExtractor resultSetExtractor,
IDbParameters parameters)


or equivalent with delegate signatures for the RowMapper and ResultSetExtractor.

This will let you return a collection of business objects and you wouldn't have to copy the parameters created via the builder. It would also let you encapsulate the logic for converting from the DataReader to object(s).

Cheers,
Mark