public bool Salvar(Models.Produto produto) { bool ok = false; MySQLPersistencia bd = new MySQLPersistencia(false, true); try { string sql = "INSERT INTO produto(nome,preco) VALUES(@nome, @preco)"; Dictionary <string, object> ps = new Dictionary <string, object>(); ps.Add("@nome", produto.Nome); ps.Add("@preco", produto.Preco); bd.Executar(sql, ps); int id = (int)bd.UltimoId; foreach (var cat in produto.Categorias) { sql = "INSERT INTO produtocategoria(idProduto, idCategoria) VALUES(@idProduto, @idCategoria)"; Dictionary <string, object> ps2 = new Dictionary <string, object>(); ps2.Add("@idProduto", id); ps2.Add("@idCategoria", cat.Id); bd.Executar(sql, ps2); } //deu certo.... bd.Commit(); produto.Id = id; ok = true; } catch (Exception ex) { bd.Rollback(); } finally { bd.Fechar(); } return(ok); }
public bool SalvarImagem(int idProduto, byte[] imagem) { MySQLPersistencia bd = new MySQLPersistencia(); string sql = "update produto set imagem = @imagem where id = @id"; Dictionary <string, object> ps = new Dictionary <string, object>(); ps.Add("@imagem", imagem); ps.Add("@id", idProduto); int linhasAfetadas = bd.Executar(sql, ps); return(linhasAfetadas == 1); }
public bool Gravar(Models.Cliente cliente) { string sql = @"insert into cliente (nome, cpf, usuario, senha) values(@nome, @cpf, @usuario, @senha)"; Dictionary <string, object> ps = new Dictionary <string, object>(); ps.Add("@nome", cliente.Nome); ps.Add("@cpf", cliente.Cpf); ps.Add("@usuario", cliente.Usuario); ps.Add("@senha", cliente.Senha); if (_bd.Executar(sql, ps) > 0) { cliente.Id = (int)_bd.UltimoId; return(true); } return(false); }