Return to Snippet

Revision: 2926
at May 9, 2007 20:04 by rengber


Initial Code
private void AddParameters(Database db, DbCommand comm, PropertyInfo[] properties, ListDictionary parameters, object target)
        {
            foreach (PropertyInfo pi in properties)
            {
                string paramName = pi.Name;
                if (parameters.Contains(paramName))
                {
                    System.Data.DbType paramType = (System.Data.DbType)parameters[paramName];
                    object value = pi.GetValue(target, null);
                    db.AddInParameter(comm, paramName, paramType, value);
                }
            }

        }

Initial URL

                                

Initial Description
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.

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

Initial Tags
command

Initial Language
C#