使用 ExecuteScalar 插入时使用 Npgsql 检索序列 ID
我正在尝试使用串行主键将一行插入到 PostgreSQL 表中,并且我需要在插入后检索该列。我得到这样的信息:
表“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 吗?我错过了什么吗?
提前致谢
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:
The table "pais" has 3 columns: id, pais, capital; id is a serial column and is its primary key.
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);
It inserts the row on the table but "res" value is null. If I insert with the nexval('table_sequence') also returns null.
Any idea of how can I return the id of the table? Am I missing something?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该线程安全吗?
如果在插入和选择之间发生另一个插入怎么办?
为什么不使用:
INSERT INTO table (fieldnames) VALUES (values) RETURNING idcolumn
?Is that thread safe?
What if another insert happens between your insert and select?
Why not use:
INSERT INTO table (fieldnames) VALUES (values) RETURNING idcolumn
?将
id
替换为您的主键在此处输入代码
并使用Inside
res
您将获得 PK。replace
id
with your primary keyenter code here
and useInside
res
you'll have the PK.为了选择最后插入的身份,您需要使用: currval(sequencename)
因此您的 select 语句应如下所示:
In order to select the last identity inserted you need to use:
currval(sequencename)
so your select statement should look like:
插入本身不会导致返回值。当您执行 ExecuteScalar 时,它正在寻找要“选择”的单个值。
我相信您需要使用 select 语句来跟进您的插入来解决您的问题。
如果你使用 t-sql 你会这样做
string sql =
“插入[表](字段名称)值(@ParamName);”
+ "选择 CAST(scope_identity() AS int)";
然后 ExecuteScalar 将返回唯一的 id;
我不确定 postGresql 的确切语法,但希望这可以解决您的问题。
The insert itself does not cause a value to be returned. When you perform ExecuteScalar it is looking for a single value to be "Selected" so to speak.
I believe you need to follow up your insert with a select statement to solve your issue.
If you were using t-sql you would do this like so
string sql =
"INSERT INTO [Table] (FieldName) VALUES (@ParamName); "
+ "SELECT CAST(scope_identity() AS int)";
ExecuteScalar would then return the unique id;
I am not sure of the exact syntax for postGresql but hopefully this allows you to solve your issue.