2011-04-07 83 views
0

I',试图插入一行到串行主键PostgreSQL表,我需要插入后检索此列。我有这样的事情:ExecuteScalar问题与Npgsql插入

表“pais”有3列:id,pais,capital; id是一个串行列,是它的主键。

NpgsqlCommand query = new NpgsqlCommand("insert into pais(nombre, capital) values(@nombre, @capital)", conn); 
query.Parameters.Add(new NpgsqlParameter("nombre", NpgsqlDbType.Varchar)); 
query.Parameters.Add(new NpgsqlParameter("capital", NpgsqlDbType.Varchar)); 
query.Prepare(); 
query.Parameters[0].Value = this.textBox1.Text; 
query.Parameters[1].Value = this.textBox2.Text; 
Object res = query.ExecuteScalar(); 
Console.WriteLine(res); 

它在表格上插入行,但“res”值为空。如果我插入nexval('table_sequence')也返回null。

任何想法如何返回表的ID?我错过了什么?

在此先感谢

+0

应该在SO – TheLQ 2011-04-07 15:13:58

+0

你应该在StackOverflow上提出这个问题。 – 2011-04-07 15:14:07

+0

所以。程序员是关于什么的? – Cheluis 2011-04-07 15:21:07

回答

1

已回答here。方法是在insert语句后面使用“Select currval()”或“Select lastval()”。