Esempio n. 1
0
        /// <summary>
        /// Deserialize the object.
        /// </summary>
        /// <param name="type">The type being read.</param>
        /// <param name="reader">The reader to use.</param>
        /// <returns>Returns the deserialized object.</returns>
        internal static object Deserialize(Type type, OleDbDataReader reader)
        {
            object data = Activator.CreateInstance(type);

            foreach (PropertyInfo prop in type.GetProperties())
            {
                AccessColumnAttribute attr = prop.GetCustomAttribute <AccessColumnAttribute>();
                if (attr != null)
                {
                    int ordinal = reader.GetOrdinal(attr.Name);

                    object value = null;
                    if (!reader.IsDBNull(ordinal))
                    {
                        Type fieldType = reader.GetFieldType(ordinal);
                        if (fieldType == typeof(string))
                        {
                            value = reader.GetString(ordinal);
                        }
                        else if (fieldType == typeof(short))
                        {
                            value = reader.GetInt16(ordinal);
                        }
                        else if (fieldType == typeof(int))
                        {
                            value = reader.GetInt32(ordinal);
                        }
                        else if (fieldType == typeof(byte))
                        {
                            value = reader.GetByte(ordinal);
                        }
                        else if (fieldType == typeof(DateTime))
                        {
                            value = reader.GetDateTime(ordinal);
                        }
                        else if (fieldType == typeof(double))
                        {
                            value = reader.GetDouble(ordinal);
                        }
                        else if (fieldType == typeof(decimal))
                        {
                            value = reader.GetDecimal(ordinal);
                        }
                        else if (fieldType == typeof(bool))
                        {
                            value = reader.GetBoolean(ordinal);
                        }
                        else
                        {
                            Debugger.Break();
                        }
                    }

                    prop.SetValue(data, value);
                }
            }

            return(data);
        }
Esempio n. 2
0
        /// <summary>
        /// Add a value to the db.
        /// </summary>
        /// <param name="settings">The access settings.</param>
        /// <param name="type">The type to add.</param>
        /// <param name="item">The item to add.</param>
        /// <returns>Returns the item id to add, or null if it wasn't.</returns>
        internal static int?Add(AccessSettings settings, Type type, object item)
        {
            AccessTableAttribute typeAttr = type.GetCustomAttribute <AccessTableAttribute>();
            int?id = null;

            if (typeAttr != null)
            {
                string columns = string.Empty;
                string values  = string.Empty;
                Dictionary <string, object> data = new Dictionary <string, object>();

                foreach (PropertyInfo prop in type.GetProperties())
                {
                    AccessColumnAttribute dataAttr = prop.GetCustomAttribute <AccessColumnAttribute>();
                    if (dataAttr != null)
                    {
                        if (!dataAttr.IsReadonly)
                        {
                            string cleanName = prop.Name;

                            Object value = prop.GetValue(item);
                            if (value != null)
                            {
                                columns += (string.IsNullOrWhiteSpace(columns) ? "" : ",") + $"[{dataAttr.Name}]";
                                values  += (string.IsNullOrWhiteSpace(values)? "" : ",") + $"@{prop.Name}";
                                data.Add(prop.Name, value);
                            }
                        }
                    }
                }

                AccessWrapper.ExecuteDatabase(settings, $"INSERT INTO [{typeAttr.Table}] ({columns}) VALUES ({values})", data, (command) =>
                {
                    command.ExecuteNonQuery();
                    id = AccessWrapper.QueryDatabase <int?>(settings, $"SELECT TOP 1 * FROM [{typeAttr.Table}] ORDER BY [{typeAttr.Key}] DESC", (ids) =>
                    {
                        ids.Read();
                        return(ids.GetInt32(0));
                    });
                });
            }

            return(id);
        }
Esempio n. 3
0
        /// <summary>
        /// Modify a value.
        /// </summary>
        /// <param name="settings">The settings to the access database.</param>
        /// <param name="type">The values type.</param>
        /// <param name="id">The item id to modify.</param>
        /// <param name="item">This values to modify</param>
        internal static void Modify(AccessSettings settings, Type type, int id, object item)
        {
            AccessTableAttribute typeAttr = type.GetCustomAttribute <AccessTableAttribute>();

            if (typeAttr != null)
            {
                string setValues = string.Empty;

                Dictionary <string, object> data = new Dictionary <string, object>();
                if (item != null)
                {
                    foreach (PropertyInfo prop in type.GetProperties())
                    {
                        AccessColumnAttribute dataAttr = prop.GetCustomAttribute <AccessColumnAttribute>();
                        if (dataAttr != null)
                        {
                            if (!dataAttr.IsReadonly)
                            {
                                string cleanName = prop.Name;

                                Object value = prop.GetValue(item);
                                if (value != null)
                                {
                                    setValues += (string.IsNullOrWhiteSpace(setValues) ? "" : ",") + $"[{dataAttr.Name}] = @{prop.Name}";
                                    data.Add(prop.Name, value);
                                }
                            }
                        }
                    }
                }

                if (!string.IsNullOrWhiteSpace(setValues))
                {
                    AccessWrapper.ExecuteDatabase(settings, $"UPDATE [{typeAttr.Table}] SET {setValues} WHERE [{typeAttr.Key}] = @1", data);
                }
                else
                {
                    throw new ArgumentException("No arguments.");
                }
            }
        }