Thursday, June 09, 2005

SqlCommandBuilder.DeriveParameters

Today, I ran into a problem with a difference between ADO and ADO.NET. In ADO, when you created a command object for executing a stored procedure, and added the parameters, the parameter names were ignored. Position is what mattered. With ADO.NET, parameter names when provided (and they have to be) matter. But thanks to Google and a little perseverance, I was able to find the answer I needed -- SqlCommandBuilder.

Using the following code, you can populate the Parameters SqlParameter collection of a command with all of its defined parameters.

SqlCommandBuilder.DeriveParameters(pCommand);

It's simply wonderful! Kudos to MS for providing this class, even though I still think I should be able to create a SqlCommand object and supply it's parameters based on position, without having to resort to an EXEC statement.

No comments: