Adding Parameters to a Command Object Using Relection Over Matching Class Properties

/ Published in: C#
Save to your folder(s)

You could eliminate the ListDictionary if you created a simple method to map C# types to DbTypes. Although this method also lets you skip over some object properties my using the ListDictionary to contain a subset of properties. It could also be used to map properties to param names if they needed to differ.

Copy this code and paste it in your HTML
  1. private void AddParameters(Database db, DbCommand comm, PropertyInfo[] properties, ListDictionary parameters, object target)
  2. {
  3. foreach (PropertyInfo pi in properties)
  4. {
  5. string paramName = pi.Name;
  6. if (parameters.Contains(paramName))
  7. {
  8. System.Data.DbType paramType = (System.Data.DbType)parameters[paramName];
  9. object value = pi.GetValue(target, null);
  10. db.AddInParameter(comm, paramName, paramType, value);
  11. }
  12. }
  14. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.