public static int GetUniqueID(object Instance) { int ID = ((ReflectEntity)Instance).ID; if (ID > 0) { return(ID); } string Query = "SELECT [ID] FROM [dbo].[" + EncryptTableName(Instance.GetType().Name) + "]"; string Filter = "[IsDeleted] = 0"; foreach (System.Reflection.PropertyInfo SystemAttribute in Instance.GetType().GetProperties().Where(x => Attribute.IsDefined(x, typeof(UniqueAtrribute)))) { if (Attribute.IsDefined(SystemAttribute, typeof(RelationAttribute))) { foreach (System.Reflection.PropertyInfo RelatedAttribute in SystemAttribute.PropertyType.GetProperties()) { if (Attribute.IsDefined(RelatedAttribute, typeof(NoDataAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(NoRelationAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(RelationAttribute))) { continue; } else if (RelatedAttribute.PropertyType.Namespace.StartsWith("System") || RelatedAttribute.PropertyType.IsEnum) { if (RelatedAttribute.PropertyType.IsEnum || RelatedAttribute.GetValue(Instance, null) != null) { Filter += " AND [" + SystemAttribute.Name + "." + RelatedAttribute + "] = '" + (IsCryptable(RelatedAttribute) ? EncryptColumnValue(RelatedAttribute.GetValue(Instance, null).ToString()) : RelatedAttribute.GetValue(Instance, null)) + "'"; } } } } else if (SystemAttribute.PropertyType.Namespace.StartsWith("System") || SystemAttribute.PropertyType.IsEnum) { if (SystemAttribute.PropertyType.IsEnum || SystemAttribute.GetValue(Instance, null) != null) { Filter += " AND [" + SystemAttribute.Name + "] = '" + (IsCryptable(SystemAttribute) ? EncryptColumnValue(SystemAttribute.GetValue(Instance, null).ToString()) : SystemAttribute.GetValue(Instance, null)) + "'"; } } } if (Filter != "") { Query += " WHERE " + Filter; } try { ID = Database.Executor.PrintQuery(Query); } catch (Exception) { } return(ID); }
public static int Save(object Instance) { int ID = ((ReflectEntity)Instance).ID; ((ReflectEntity)Instance).UpdatedOn = DateTime.Now; if (Instance.GetType().IsSubclassOf(typeof(ReflectEntity))) { if (((ReflectEntity)Instance).ID == 0) { ((ReflectEntity)Instance).CreatedOn = DateTime.Now; string Columns = ""; string Values = ""; foreach (System.Reflection.PropertyInfo SystemAttribute in Instance.GetType().GetProperties()) { if (Attribute.IsDefined(SystemAttribute, typeof(NoDataAttribute))) { continue; } if (SystemAttribute.Name != "ID") { if (Attribute.IsDefined(SystemAttribute, typeof(RelationAttribute))) { foreach (System.Reflection.PropertyInfo RelatedAttribute in SystemAttribute.PropertyType.GetProperties()) { if (Attribute.IsDefined(RelatedAttribute, typeof(NoDataAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(NoRelationAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(RelationAttribute))) { continue; } if (RelatedAttribute.PropertyType.Namespace.StartsWith("System") || RelatedAttribute.PropertyType.IsEnum) { if (Columns != "") { Columns += ", "; } Columns += "[" + EncryptColumnName(SystemAttribute.Name + "." + RelatedAttribute.Name) + "]"; } } } else if (SystemAttribute.PropertyType.Namespace.StartsWith("System") || SystemAttribute.PropertyType.IsEnum) { if (Columns != "") { Columns += ", "; } Columns += "[" + EncryptColumnName(SystemAttribute.Name) + "]"; } if (!SystemAttribute.PropertyType.IsEnum && SystemAttribute.GetValue(Instance, null) == null) { if (Values != "") { Values += ", "; } Values += "NULL"; } else { if (SystemAttribute.PropertyType == typeof(DateTime)) { if (Values != "") { Values += ", "; } if (Convert.ToDateTime(SystemAttribute.GetValue(Instance, null)) == DateTime.MinValue) { Values += "NULL"; } else { Values += "'" + (IsCryptable(SystemAttribute) ? EncryptColumnValue(SystemAttribute.GetValue(Instance, null).ToString()) : new System.Data.SqlClient.SqlParameter("@var", SystemAttribute.GetValue(Instance, null)) { SqlDbType = SqlDbType.SmallDateTime }.SqlValue.ToString()) + "'"; } } else if (Attribute.IsDefined(SystemAttribute, typeof(RelationAttribute))) { foreach (System.Reflection.PropertyInfo RelatedAttribute in SystemAttribute.PropertyType.GetProperties()) { if (Attribute.IsDefined(RelatedAttribute, typeof(NoDataAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(NoRelationAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(RelationAttribute))) { continue; } else if (RelatedAttribute.PropertyType.Namespace.StartsWith("System") || RelatedAttribute.PropertyType.IsEnum) { if (Values != "") { Values += ", "; } if (RelatedAttribute.GetValue(Instance, null) == null) { Values += "[" + EncryptColumnName(SystemAttribute.Name + "." + RelatedAttribute.Name) + "] = NULL"; } else { Values += "[" + EncryptColumnName(SystemAttribute.Name + "." + RelatedAttribute.Name) + "] = '" + (IsCryptable(RelatedAttribute) ? EncryptColumnValue(RelatedAttribute.GetValue(Instance, null).ToString()) : RelatedAttribute.GetValue(Instance, null)) + "'"; } } } } else if (SystemAttribute.PropertyType.Namespace.StartsWith("System") || SystemAttribute.PropertyType.IsEnum) { if (Values != "") { Values += ", "; } Values += "'" + (IsCryptable(SystemAttribute) ? EncryptColumnValue(SystemAttribute.GetValue(Instance, null).ToString()) : SystemAttribute.GetValue(Instance, null)) + "'"; } } } } string Query = "INSERT INTO [dbo].[" + EncryptTableName(Instance.GetType().Name) + "] (" + Columns + ") VALUES (" + Values + "); SELECT SCOPE_IDENTITY();"; ID = Database.Executor.PrintQuery(Query); } else { string Values = ""; foreach (System.Reflection.PropertyInfo SystemAttribute in Instance.GetType().GetProperties()) { if (Attribute.IsDefined(SystemAttribute, typeof(NoDataAttribute))) { continue; } if (SystemAttribute.Name != "ID") { if (Values != "") { Values += ", "; } if (SystemAttribute.PropertyType == typeof(DateTime)) { if (Convert.ToDateTime(SystemAttribute.GetValue(Instance, null)) == DateTime.MinValue) { Values += "[" + SystemAttribute.Name + "] = NULL"; } else { Values += "[" + SystemAttribute.Name + "] = '" + (IsCryptable(SystemAttribute) ? EncryptColumnValue(SystemAttribute.GetValue(Instance, null).ToString()) : new System.Data.SqlClient.SqlParameter("@var", SystemAttribute.GetValue(Instance, null)) { SqlDbType = SqlDbType.SmallDateTime }.SqlValue.ToString()) + "'"; } } else if (Attribute.IsDefined(SystemAttribute, typeof(RelationAttribute))) { foreach (System.Reflection.PropertyInfo RelatedAttribute in SystemAttribute.PropertyType.GetProperties()) { if (Attribute.IsDefined(RelatedAttribute, typeof(NoDataAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(NoRelationAttribute)) || Attribute.IsDefined(RelatedAttribute, typeof(RelationAttribute))) { continue; } if (RelatedAttribute.PropertyType.Namespace.StartsWith("System") || RelatedAttribute.PropertyType.IsEnum) { if (!RelatedAttribute.PropertyType.IsEnum && RelatedAttribute.GetValue(Instance, null) == null) { Values += "[" + EncryptColumnName(SystemAttribute.Name + "." + RelatedAttribute.Name) + "]= NULL"; } else { Values += "[" + EncryptColumnName(SystemAttribute.Name + "." + RelatedAttribute.Name) + "]= '" + (IsCryptable(RelatedAttribute) ? EncryptColumnValue(RelatedAttribute.GetValue(Instance, null).ToString()) : RelatedAttribute.GetValue(Instance, null)) + "'"; } } } } else if (SystemAttribute.PropertyType.Namespace.StartsWith("System") || SystemAttribute.PropertyType.IsEnum) { if (!SystemAttribute.PropertyType.IsEnum && SystemAttribute.GetValue(Instance, null) == null) { Values += "[" + SystemAttribute.Name + "] = NULL"; } else { Values += "[" + SystemAttribute.Name + "] = '" + (IsCryptable(SystemAttribute) ? EncryptColumnValue(SystemAttribute.GetValue(Instance, null).ToString()) : SystemAttribute.GetValue(Instance, null)) + "'"; } } } } string Query = "UPDATE [dbo].[" + EncryptTableName(Instance.GetType().Name) + "] SET " + Values + " WHERE [ID] = '" + ((ReflectEntity)Instance).ID + "';"; Database.Executor.ExecuteQuery(Query); } } return(ID); }