【发布时间】:2020-03-30 06:38:47
【问题描述】:
我正在尝试使用 Npgsql 中的事务执行查询,因为它使代码明显更清晰,并且与其他纯 SQL 系统中的查询更加一致。但是我在以下代码中收到错误Npgsql.PostgresException: 42703: column "_hash" does not exist。
var cmd = new NpgsqlCommand(@"
do
$do$
begin
if ((select count(1) from components where hash = @_hash) = 0) then
insert into components (hash, name) values (@_hash, @_name);
end if;
end
$do$", db); // db is NpgsqlConnection connection
cmd.Parameters.AddWithValue("_hash", "00000000-0000-0000-0000-000000000000");
cmd.Parameters.AddWithValue("_name", "t_test");
cmd.ExecuteNonQuery(); // error on this line
由于某种原因,以下内容确实有效,这让我认为这是事务中 AddWithValue 的问题
硬编码值;
var cmd = new NpgsqlCommand(@"
do
$do$
begin
if ((select count(1) from components where hash = '00000000-0000-0000-0000-000000000000') = 0) then
insert into components (hash, name) values ('00000000-0000-0000-0000-000000000000', 't_test');
end if;
end
$do$", db);
cmd.ExecuteNonQuery();
摆脱交易
var cmd = new NpgsqlCommand("insert into components (hash, name) values (@_hash, @_name);", db)
cmd.Parameters.AddWithValue("_hash", "00000000-0000-0000-0000-000000000000");
cmd.Parameters.AddWithValue("_name", "t_test");
cmd.ExecuteNonQuery();
是什么导致了这个问题,如何解决?
注意:我可以运行在 JetBrains DataGrip 等数据库管理器中失败的查询,因此查询不会出现格式错误。
【问题讨论】:
-
你为什么不用
insert into ... where not exists ..?
标签: c# sql postgresql npgsql