Retrieve serial ID with Npgsql when inserting with ExecuteScalar

后端 未结 4 1327
醉酒成梦
醉酒成梦 2020-12-16 19:08

I\'m trying to insert a row into a PostgreSQL table with a serial primary key and I need to retrieve this column after it was inserted. I got something like this:

Th

4条回答
  •  一生所求
    2020-12-16 19:56

    In order to select the last identity inserted you need to use: currval(sequencename)

    so your select statement should look like:

    NpgsqlCommand query = new NpgsqlCommand("insert into pais(nombre, capital) values(@nombre, @capital);select currval('table_sequence');", conn);
    

提交回复
热议问题