我试图用串行主键向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?我错过了什么吗?
在此先感谢
我不读你的查询为“选择”任何值作为标量返回? – 2011-04-07 15:31:16