예제 #1
0
        public static bool DeleteFromDb(this IDataModel obj)
        {
            try
            {
                using (AccessDbHelper helper = new AccessDbHelper())
                {
                    helper.ExecuteSQLNonQuery(string.Format("UPDATE [{0}] SET ISDELETED =1 WHERE ID={1}", obj.GetType().Name, ((IDataModel)obj).Id));
                }
                return(true);
            }
            catch (Exception ex)
            {
                ApplicationContext.Current.Logger.Error("DeleteFromDb failed.", ex);
            }

            return(false);
        }
예제 #2
0
        public static bool SaveToDb(this IDataModel obj)
        {
            try
            {
                using (var helper = new AccessDbHelper())
                {
                    string insertParams = GetInsertString(obj);
                    helper.ExecuteSQLNonQuery(string.Format("INSERT INTO [{0}] {1}", obj.GetType().Name, insertParams));
                }
                return(true);
            }
            catch (Exception ex)
            {
                ApplicationContext.Current.Logger.Error("DeleteFromDb failed.", ex);
            }

            return(false);
        }
예제 #3
0
        public static bool UpdateToDb(this IDataModel obj)
        {
            try
            {
                using (var helper = new AccessDbHelper())
                {
                    string updateParams = GetUpdateString(obj);
                    helper.ExecuteSQLNonQuery(string.Format("UPDATE [{0}] SET {1}", obj.GetType().Name, updateParams));
                }
                return(true);
            }
            catch (Exception ex)
            {
                ApplicationContext.Current.Logger.Error("DeleteFromDb failed.", ex);
            }

            return(false);
        }
예제 #4
0
        public static void Upgrade()
        {
            //IDataModel
            var assembly      = typeof(IDataModel).Assembly;
            var dataModelList = assembly.GetTypes().Where(x => x.GetInterfaces().Contains(typeof(IDataModel))).ToList();

            foreach (var item in dataModelList)
            {
                string tableName  = item.Name;
                var    properties = item.GetProperties();
                using (var helper = new AccessDbHelper())
                {
                    var table = helper.GetDataTable(string.Format("SELECT * FROM [{0}]", tableName));
                    foreach (var property in properties)
                    {
                        if (!table.Columns.Contains(property.Name))
                        {
                            helper.ExecuteSQLNonQuery(string.Format("ALTER TABLE [{0}] ADD COLUMN [{1}] {2}", tableName, property.Name, GetDBType(property)));
                        }
                    }
                }
            }
        }