We Recommend

Accelerated C# 2008 Accelerated C# 2008
This book is both a rapid tutorial and a permanent reference. You’ll quickly master C# syntax while learning how the CLR simplifies many programming tasks. You’ll also learn best practices that ensure your code will be efficient, reusable, and robust. Why spend months or years discovering the best ways to design and code C# when this book will show you how to do things the right way, right from the start?


Posted By

rengber on 05/09/07


Tagged

command ADONet Reflection


Versions (?)


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. }
  13.  
  14. }

Report this snippet 

You need to login to post a comment.