public static object SaveOrRemoveSimpleObject <T>(string Command, CommandType commandType, SqlOperation sqlOperation, object objectToInsert, SqlConnection sqlConnection, SqlTransaction sqlTransaction) where T : class, new()
        {
            object rowsAfectedOrIdentity = 0;
            Type   t = typeof(T);


            if (sqlConnection.State != ConnectionState.Open)
            {
                sqlConnection.Open();
            }
            using (SqlCommand sqlCommand = DataManager.CreateCommand(Command, commandType))
            {
                sqlCommand.Connection = sqlConnection;
                if (sqlTransaction != null)
                {
                    sqlCommand.Transaction = sqlTransaction;
                }
                if (sqlCommand.Parameters.Contains("@Action"))
                {
                    sqlCommand.Parameters["@Action"].Value = sqlOperation;
                }
                if (objectToInsert != null)
                {
                    foreach (FieldInfo fieldInfo in objectToInsert.GetType().GetFields())
                    {
                        if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute)))
                        {
                            FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute));
                            if (sqlCommand.Parameters.Contains(string.Format("@{0}", fieldMapAttribute.FieldName)))
                            {
                                if (fieldInfo.GetValue(objectToInsert) != null)
                                {
                                    sqlCommand.Parameters[string.Format("@{0}", fieldMapAttribute.FieldName)].Value = fieldInfo.GetValue(objectToInsert);
                                }
                                else
                                {
                                    sqlCommand.Parameters[string.Format("@{0}", fieldMapAttribute.FieldName)].Value = DBNull.Value;
                                }
                            }
                        }
                    }
                }
                rowsAfectedOrIdentity = sqlCommand.ExecuteScalar();
            }

            return(rowsAfectedOrIdentity);
        }
Example #2
0
        private static object CustomApply(FieldInfo info, IDataRecord reader)
        {
            SettingsCollection settings          = null;
            FieldMapAttribute  fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(info, typeof(FieldMapAttribute));

            try
            {
                if (reader != null)
                {
                    settings = new SettingsCollection(reader[fieldMapAttribute.FieldName].ToString());
                }
            }
            catch (Exception)
            {
                throw;
            }
            return(settings.ToDictionary());
        }
        /// <summary>
        /// Return SingleObject
        /// </summary>
        /// <typeparam name="ThingT"></typeparam>
        /// <param name="Key"></param>
        /// <param name="onApplyValue"></param>
        /// <returns></returns>
        public static ThingT CreateMainObject <ThingT>(IDataReader sqlDataReader, Func <FieldInfo, IDataRecord, object> onApplyValue) where ThingT : class, new()
        {
            Type   t            = typeof(ThingT);
            ThingT returnObject = (ThingT)Activator.CreateInstance(t);


            foreach (FieldInfo fieldInfo in t.GetFields())
            {
                object val;
                if (Attribute.IsDefined(fieldInfo, typeof(FieldMapAttribute)))
                {
                    FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(fieldInfo, typeof(FieldMapAttribute));

                    if (fieldMapAttribute.Show)
                    {
                        if (sqlDataReader.FieldExists(fieldMapAttribute.FieldName))
                        {
                            if (fieldMapAttribute.UseApplyFunction && onApplyValue != null)
                            {
                                val = onApplyValue(fieldInfo, sqlDataReader);
                            }
                            else
                            {
                                val = sqlDataReader[fieldMapAttribute.FieldName];
                            }
                            if (val is DBNull)
                            {
                                fieldInfo.SetValue(returnObject, null);
                            }
                            else
                            {
                                fieldInfo.SetValue(returnObject, val);
                            }
                        }
                    }
                }
            }
            return(returnObject);
        }
        public static IList GetListObject(FieldInfo fieldInfo, IDataReader sqlDataReader)
        {
            if (!fieldInfo.FieldType.IsGenericType || fieldInfo.FieldType.GetGenericTypeDefinition() == typeof(IList))
            {
                throw new Exception("This is not generic list");
            }
            Type  typeElement  = fieldInfo.FieldType.GetGenericArguments()[0];
            IList returnObject = (IList)Activator.CreateInstance(fieldInfo.FieldType);

            //Get the inner type

            while (sqlDataReader.Read())
            {
                object currentItem = Activator.CreateInstance(typeElement);
                foreach (FieldInfo f in typeElement.GetFields())
                {
                    if (Attribute.IsDefined(f, typeof(FieldMapAttribute)))
                    {
                        FieldMapAttribute fieldMapAttribute = (FieldMapAttribute)Attribute.GetCustomAttribute(f, typeof(FieldMapAttribute));
                        if (sqlDataReader.FieldExists(fieldMapAttribute.FieldName))
                        {
                            object val = sqlDataReader[fieldMapAttribute.FieldName];
                            if (val is DBNull)
                            {
                                f.SetValue(currentItem, null);
                            }
                            else
                            {
                                f.SetValue(currentItem, val);
                            }
                        }
                    }
                }
                returnObject.Add(currentItem);
            }
            sqlDataReader.Dispose();
            return(returnObject);
        }