Пример #1
0
        public bool Cadastro(Cidades cidades)
        {
            bool resultado = false;

            try
            {
                con = new SqlConnection(conexao);
                con.Open();
                cmd             = new SqlCommand();
                cmd.Connection  = con;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = "Insert into Cidades(nome, estado, habitantes) values(@n, @e, @h)";
                cmd.Parameters.AddWithValue("@n", cidades.Nome);
                cmd.Parameters.AddWithValue("@e", cidades.Estado);
                cmd.Parameters.AddWithValue("@h", cidades.Habitantes);

                int r = cmd.ExecuteNonQuery();
                if (r > 0)
                {
                    resultado = true;
                }

                cmd.Parameters.Clear();
            } catch (SqlException se) {
                throw new Exception(se.Message);
            } catch (Exception e) {
                throw new Exception(e.Message);
            } finally{
                con.Close();
            }
            return(resultado);
        }
Пример #2
0
        public string Editar(Cidades cidade)
        {
            SqlConnection con = new SqlConnection(conexao);

            string msg;

            try{
                SqlCommand cmd = new SqlCommand();
                cmd.Connection  = con;
                cmd.CommandText = "Update Cidades set nome = @n, estado = @e, habitantes = @h where id = @id";
                cmd.Parameters.AddWithValue("@n", cidade.Nome);
                cmd.Parameters.AddWithValue("@e", cidade.Estado);
                cmd.Parameters.AddWithValue("@h", cidade.Habitantes);
                cmd.Parameters.AddWithValue("@id", cidade.Id);
                con.Open();
                int r = cmd.ExecuteNonQuery();
                if (r > 0)
                {
                    msg = "Atualização Efetuada";
                }
                else
                {
                    msg = "Não foi possível atualizar";
                }
                cmd.Parameters.Clear();
            } catch (SqlException se) {
                throw new Exception("Erro ao tentar atualizar dados" + se.Message);
            } catch (System.Exception e) {
                throw new Exception("Erro inesperado" + e.Message);
                throw;
            } finally {
                con.Close();
            }

            return(msg);
        }