Exemple #1
0
 /// <summary>
 /// Fill Stored Proc Parameters
 /// </summary>
 /// <param name="proc">Stored Procedure</param>
 /// <param name="values">Values</param>
 public static void Fill(this IStoredProc proc, IDictionary <string, object> values)
 {
     if (null != values)
     {
         var properties = proc.GetProperties();
         foreach (var property in properties)
         {
             if (null != property && property.CanWrite && values.ContainsKey(property.Name))
             {
                 var value = values[property.Name];
                 if (null == value)
                 {
                     property.SetValue(proc, null, null);
                 }
                 else if (value.GetType() == property.PropertyType)
                 {
                     property.SetValue(proc, value, null);
                 }
                 else
                 {
                     var t         = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                     var safeValue = Convert.ChangeType(value, t);
                     property.SetValue(proc, safeValue, null);
                 }
             }
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Build Command
        /// </summary>
        /// <param name="proc">Procedure</param>
        /// <param name="database">Database</param>
        /// <returns>Database Command</returns>
        public static DbCommand BuildCommand(this IStoredProc proc, Database database)
        {
            if (null == database)
            {
                throw new ArgumentNullException("database");
            }

            var command = database.GetStoredProcCommand(proc.StoredProc);

            foreach (var prop in proc.GetProperties())
            {
                var mapper = prop.GetAttribute <DataMapperAttribute>();

                if (mapper != null)
                {
                    var value = prop.GetValue(proc, null);
                    database.AddInParameter(command, mapper.ParameterName, mapper.DatabaseType, value);
                }
            }

            return(command);
        }