Пример #1
0
        public List <SQLData> InsertFromClassPrep(object obj)
        {
            List <SQLData> Data = new List <SQLData>();

            FieldInfo[] fields = obj.GetType().GetFields();
            try
            {
                foreach (FieldInfo f in fields)
                {
                    string column = f.Name;
                    IEnumerable <Attribute> attrs = f.GetCustomAttributes();
                    foreach (Attribute a in attrs)
                    {
                        if (a is SQLLibColumn)
                        {
                            SQLLibColumn cl = (SQLLibColumn)a;
                            column = cl.col;
                            break;
                        }
                    }

                    if (column.Trim() == "")
                    {
                        continue;
                    }

                    SQLData sqldata = new SQLData(column, f.GetValue(obj));
                    sqldata.DataType = f.FieldType;
                    Data.Add(sqldata);
                }
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.ToString());
                if (SEHError == true)
                {
                    throw;
                }
                return(null);
            }

            return(Data);
        }
Пример #2
0
        public bool LoadIntoClass(SqlDataReader dr, object obj)
        {
            FieldInfo[] fields = obj.GetType().GetFields();
            try
            {
                foreach (FieldInfo f in fields)
                {
                    string column = f.Name;
                    IEnumerable <Attribute> attrs = f.GetCustomAttributes();
                    foreach (Attribute a in attrs)
                    {
                        if (a is SQLLibColumn)
                        {
                            SQLLibColumn cl = (SQLLibColumn)a;
                            column = cl.col;
                            break;
                        }
                    }

                    if (column.Trim() == "")
                    {
                        continue;
                    }

                    bool found = false;
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        if (column.ToLower() == dr.GetName(i).ToLower())
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found == false)
                    {
                        continue;
                    }

                    if (dr[column] is DBNull)
                    {
                        f.SetValue(obj, null);
                    }
                    else
                    {
                        if (f.FieldType.IsGenericType && f.FieldType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            f.SetValue(obj, Convert.ChangeType(dr[column], f.FieldType.GetGenericArguments()[0]));
                        }
                        else
                        {
                            if (f.FieldType.IsEnum == true)
                            {
                                f.SetValue(obj, Convert.ChangeType(dr[column], f.FieldType.GetEnumUnderlyingType()));
                            }
                            else
                            {
                                f.SetValue(obj, Convert.ChangeType(dr[column], f.FieldType));
                            }
                        }
                    }
                }
            }
            catch (Exception ee)
            {
                Debug.WriteLine(ee.ToString());
                if (SEHError == true)
                {
                    throw;
                }
                return(false);
            }
            return(true);
        }