public static void DeleteEntityFromDB(string tableName, string IDproperty, object entity, string ConnectionName = "Default") { PropertyInfo pi = entity.GetType().GetProperty(IDproperty); if (pi != null) { string query = "DELETE FROM " + tableName + " WHERE " + IDproperty + "=" + pi.GetValue(entity, null).ToString(); DBFunction.ExecuteNonQuery(query, ConnectionName); SynapseForm.SynapseLogger.Debug(string.Format("Deleting from {0} with id {1}", tableName, pi.GetValue(entity, null).ToString())); } }
public static Int64 SaveEntityToDBOld(object entity, string tableName, string IDproperty, string exclusions) { IList <PropertyInfo> properties = (entity.GetType()).GetProperties(); string fields = string.Empty; string values = string.Empty; string updates = string.Empty; bool update = false; string query = string.Empty; Int64 id = 0; foreach (PropertyInfo p in properties) { if ((p.Name != IDproperty) && (!exclusions.Contains(p.Name))) { fields += p.Name + ","; switch (p.PropertyType.ToString()) { case "System.DateTime": DateTime date = (DateTime)p.GetValue(entity, null); if (date < DateTime.Parse("01/01/1753 00:00:00")) { values += "Null,"; updates += p.Name + "=Null,"; } else { values += "'" + date.ToString("MM/dd/yyyy HH:mm:ss") + "',"; updates += p.Name + "='" + date.ToString("MM/dd/yyyy HH:mm:ss") + "',"; } break; case "System.TimeSpan": TimeSpan time = (TimeSpan)p.GetValue(entity, null); values += "'" + time + "',"; updates += p.Name + "='" + time + "',"; break; case "System.Decimal": values += p.GetValue(entity, null).ToString().Replace(',', '.') + ","; updates += p.Name + "=" + p.GetValue(entity, null).ToString().Replace(',', '.') + ","; break; case "System.String": string value = string.Empty; if (p.GetValue(entity, null) != null) { value = doubleQuote(p.GetValue(entity, null).ToString()); } values += "'" + value + "',"; updates += p.Name + "='" + value + "',"; break; case "System.Boolean": values += (((bool)p.GetValue(entity, null)) ? 1 : 0).ToString() + ","; updates += p.Name + "=" + (((bool)p.GetValue(entity, null)) ? 1 : 0).ToString() + ","; break; case "SynapseCore.Database.LabelBag": break; default: values += p.GetValue(entity, null) + ","; updates += p.Name + "=" + p.GetValue(entity, null) + ","; break; } } else if (p.Name == IDproperty && (Int64)p.GetValue(entity, null) != 0) { id = (Int64)p.GetValue(entity, null); update = true; } } if (update) { query = "UPDATE " + tableName + " SET " + updates.TrimEnd(',') + " WHERE " + IDproperty + "=" + id.ToString(); DBFunction.ExecuteNonQuery(query); return(id); } else { query = "INSERT INTO " + tableName + "(" + fields.TrimEnd(',') + ") VALUES (" + values.TrimEnd(',') + ");SELECT cast(SCOPE_IDENTITY() as bigint);"; return((Int64)DBFunction.ExecuteScalarQuery(query)); } }