public virtual bool Salvar() { bool confirmacao = false; try { using (SqlConnection connection = new SqlConnection( util.stringConexaoSql)) { List <string> campos = new List <string>(); List <string> valores = new List <string>(); foreach (PropertyInfo pi in this.GetType().GetProperties()) //GetProperties(/*BindingFlags.Public*/) não consegui fazer pegar apenas as publics { OpcoesBase pOpcoesBase = (OpcoesBase)pi.GetCustomAttribute(typeof(OpcoesBase)); if (pOpcoesBase != null && pOpcoesBase.UsarNoBanco && !pOpcoesBase.AutoIncremento) { campos.Add(pi.Name); if (!pOpcoesBase.Criptografado) { if (pi.PropertyType.Name.ToString().Equals("DateTime")) { valores.Add("'" + DateTime.Parse(pi.GetValue(this).ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"); } else { valores.Add("'" + pi.GetValue(this) + "'"); } } else { valores.Add("'" + util.criptografa(pi.GetValue(this).ToString(), pOpcoesBase.chaveCripto) + "'"); } } } string queryString = "insert into " + this.GetType().Name + "s (" + string.Join(", ", campos.ToArray()) + ")values(" + string.Join(", ", valores.ToArray()) + ");"; SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); confirmacao = true; } } catch (Exception error) { FrmAlerta alerta = new FrmAlerta("Ocorreu um erro ao salvar " + this.GetType().Name + ". Mensagem de erro:" + error.Message, null); alerta.ShowDialog(); confirmacao = false; } return(confirmacao); }
public virtual bool delete() { bool confirmacao = false; try { using (SqlConnection connection = new SqlConnection( util.stringConexaoSql)) { List <string> campos = new List <string>(); List <string> where = new List <string>(); foreach (PropertyInfo pi in this.GetType().GetProperties()) { OpcoesBase pOpcoesBase = (OpcoesBase)pi.GetCustomAttribute(typeof(OpcoesBase)); if (pOpcoesBase != null && pOpcoesBase.UsarNoBanco && !pOpcoesBase.UsarParaBuscar) { if (pOpcoesBase.UsarNoBanco && pOpcoesBase.UsarParaBuscar) { if (pi.GetValue(this) != null) { where.Add(pi.Name + " = '" + pi.GetValue(this) + "'"); } } string queryString = "DELETE FROM " + this.GetType().Name + "s"; if (where.Count > 0) { queryString += " where " + string.Join(" and ", where.ToArray()); } SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); confirmacao = true; } } } } catch (Exception error) { FrmAlerta alerta = new FrmAlerta("Ocorreu um erro ao salvar " + this.GetType().Name + ". Mensagem de erro:" + error.Message, null); alerta.ShowDialog(); confirmacao = false; } return(confirmacao); }
public virtual bool update() { bool confirmacao = false; try { using (SqlConnection connection = new SqlConnection( util.stringConexaoSql)) { List <string> campos = new List <string>(); List <string> where = new List <string>(); foreach (PropertyInfo pi in this.GetType().GetProperties()) { OpcoesBase pOpcoesBase = (OpcoesBase)pi.GetCustomAttribute(typeof(OpcoesBase)); if (pOpcoesBase != null && pOpcoesBase.UsarNoBanco && !pOpcoesBase.UsarParaBuscar) { if (!pOpcoesBase.Criptografado) { if (pi.PropertyType.Name.ToString().Equals("DateTime")) { campos.Add(pi.Name + " = '" + DateTime.Parse(pi.GetValue(this).ToString()).ToString("yyyy-MM-dd HH:mm:ss") + "'"); } else { campos.Add(pi.Name + " = '" + pi.GetValue(this) + "'"); } } else { campos.Add(pi.Name + " = '" + util.criptografa(pi.GetValue(this).ToString(), pOpcoesBase.chaveCripto) + "'"); } } if (pOpcoesBase.UsarNoBanco && pOpcoesBase.UsarParaBuscar) { var valor = pi.GetValue(this); if (valor != null) { where.Add(pi.Name + " = '" + valor + "'"); } } } string queryString = "update " + this.GetType().Name + "s set " + string.Join(", ", campos.ToArray()); if (where.Count > 0) { queryString += " where " + string.Join(" and ", where.ToArray()); } SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); confirmacao = true; } } catch (Exception error) { FrmAlerta alerta = new FrmAlerta("Ocorreu um erro ao salvar " + this.GetType().Name + ". Mensagem de erro:" + error.Message, null); alerta.ShowDialog(); confirmacao = false; } return(confirmacao); }