Posted By

rengber on 05/09/07


command ADONet Reflection

Versions (?)

Who likes this?

2 people have marked this snippet as a favorite


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

 / Published in: C#

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.

  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  

You need to login to post a comment.