In the case of a SQL Server provider, and if you use a stored procedure, you can use the SqlCommandBuilder.DeriveParameters method.
In other cases, I have always thought that the best way, if your teams are at least persistent, is to use Designer support in Visual Studio. Either create a strongly typed DataSet and add a query, or do it the old-fashioned way and drag SqlConnection and SqlCommand onto the design surface. In any case, when you configure the CommandText as an operator with parameters, the Designer will ask you if you want the Parameters collection to fill up. As a result, code is created to create SqlCommand with a set of parameters already created. All you have to do is set the parameter values ββand everything will be set.
source share