Search code examples
c#.netenterprise-librarydbcommandinput-parameters

Copy parameters from DbCommand to another DbCommand


How do you copy DbCommand parameters to another DbCommand, I want a new DbCommand with the same parameters as my last DbCommand. But now with a different sql string.


Solution

  • 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;
    }