コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }