Copy parameters from DbCommand to another DbCommand
// Copy parameters from cmd1 to cmd2
// Creates an array with new parameters
var nsp = cmd1.Parameters.Cast<ICloneable>().Select(x => x.Clone() as SqlParameter).Where(x => x != null).ToArray();
// Copy parameters into another command
cmd2.Parameters.AddRange(nsp);
You could put the code you need to re-use in a separate method:
public DbCommand RecycledParameters(string sql, IList<DbParameter> parameters)
{
var result = db.GetSqlStringCommand(sql);
foreach(DbParameter p in parameters)
{
db.AddInParameter(result, p.ParameterName, p.DbType, p.Value);
}
return result;
}
could you do something like this?
System.Data.Common.DbCommand command = new System.Data.SqlClient.SqlCommand();
System.Data.Common.DbCommand command1 = new System.Data.SqlClient.SqlCommand();
command1.Parameters.AddRange(command.Parameters.Cast<System.Data.Common.DbParameter>().ToArray());