After inserting a line of information into a given table, I need to retrieve the ID value to fill my object, my code looks like this:
using (NpgsqlConnection pgsqlConnection = new NpgsqlConnection(conn)
{
pgsqlConnection.Open();
String sql = "INSERT INTO public.localpo(NOME) VALUES (:nome)";
using (NpgsqlCommand pgsqlcommand = new NpgsqlCommand(sql, pgsqlConnection))
{
pgsqlcommand.Parameters.Add(new NpgsqlParameter("nome", "Ricardo Soares"));
pgsqlcommand.ExecuteNonQuery();
}
}
With this code I can insert information into the database without problems, getting the doubt, and to retrieve the id that was generated?
below the sql code used to generate the table:
CREATE TABLE public.localpo (
id bigint NOT NULL DEFAULT nextval('localpo_id_seq'::regclass),
nome character varying(255) COLLATE pg_catalog."default",
CONSTRAINT pk_id_localpo PRIMARY KEY (id)
)