C
C#14mo ago
caique

❔ CRUD in Postgresql

I'm trying to select data to my database and put them in a combobox, but i can't get this information. I can only register informations on the database, but how could i get them?
7 Replies
caique
caiqueOP14mo ago
the registration class: using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace Orlagenda.Conexoes { public class CadastroCliente { Conexao conn = new Conexao(); NpgsqlCommand cmd = new NpgsqlCommand(); public String mensagem = ""; public CadastroCliente(string nome, string cpf, string datanasc, string endereco, string cidade, string genero) { cmd.CommandText = $"INSERT INTO tb_clientes(nome_cliente, cpf_cliente, datanasc_cliente, endereco_cliente, cidade_cliente, genero_cliente) values(@nome,@cpf,@datanasc,@endereco,@cidade,@genero )"; cmd.Parameters.AddWithValue("@nome",nome); cmd.Parameters.AddWithValue("@cpf", cpf); cmd.Parameters.AddWithValue("@datanasc", datanasc); cmd.Parameters.AddWithValue("@endereco", endereco); cmd.Parameters.AddWithValue("@cidade", cidade); cmd.Parameters.AddWithValue("@genero", genero); try { cmd.Connection = conn.estabelecerConexao(); cmd.ExecuteNonQuery(); this.mensagem = "Cadastrado com Sucesso"; } catch (NpgsqlException e ) { this.mensagem = "Erro na conexão do Banco" + " " + e; } } } } the selection class: using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace Orlagenda.Conexoes { public class Clientes { NpgsqlCommand cmd = new NpgsqlCommand(); public Clientes(string nome) { Conexao conn = new Conexao(); NpgsqlCommand cmd = new NpgsqlCommand(); cmd.CommandText = $"SELECT nome_cliente from tb_clientes;"; cmd.Parameters.AddWithValue("nome_cliente", nome); try { cmd.Connection = conn.estabelecerConexao(); cmd.ExecuteNonQuery(); } catch (NpgsqlException e ) {
} } } }
ekhidna
ekhidna14mo ago
Why don't you use EntityFramework. I think it's compatible with PostgreSql
caique
caiqueOP14mo ago
i found a video about it but i kind of dont know how to use it and the first part was already done, so i didn't knew if it was confuse my code
ekhidna
ekhidna14mo ago
Take a look here https://sqlines.com/postgresql/npgsql_cs_result_sets I don't know exactly but before I used Entity Framework for INSERT I'd use cmd.ExecuteNonQuery(); Like you have. But to SELECT it was another command cmd.CommandText = $"SELECT nome_cliente from tb_clientes;"; cmd.Parameters.AddWithValue("nome_cliente", nome); You have only this but I don't see where you are storing the values retreived from SELECT Maybe I am not reading well your code. Mas use a Entity Framework é melhor. E tem muito tutorial 🙂
caique
caiqueOP14mo ago
thx bro, im already seeing a Entity Framework video, but i'll check this site for sure
Pobiega
Pobiega14mo ago
ExecuteNonQuery doesnt return a response, it's not for queries Since you are only selecting a single column, you could use ExecuteScalar Otherwise you would use ExecuteDataReader
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server