简体   繁体   English

Npgsql.PostgresException:'55000:在此会话中尚未定义序列“ student_folio_id_seq”的当前时间”

[英]Npgsql.PostgresException: '55000: currval of sequence “student_folio_id_seq” is not yet defined in this session'

I am new in C# syntax and trying out Insert data into two different table when user hit the Insert button. 我是C#语法的新手,当用户单击“插入”按钮时,尝试将数据插入到两个不同的表中。 At the same time, 2nd Insert statement need to grab the 1st Insert statement id as it's foreign key. 同时,第二个插入语句需要获取第一个插入语句ID作为外键。 Primary key id is auto increment. 主键ID是自动递增。

This sql works in PHP but seems not in C# and I get this throw error message Npgsql.PostgresException: '55000: currval of sequence "student_folio_id_seq" is not yet defined in this session'. 此sql在PHP中有效,但在C#中似乎不起作用,并且我收到此抛出错误消息Npgsql.PostgresException:'55000:在此会话中尚未定义序列“ student_folio_id_seq”的当前时间”。

Can any expert in C# Postgres db give me advice where goes wrong in syntax? C#Postgres db的任何专家都可以给我一些语法错误的建议吗? And good if can specify with examples. 而且如果可以举例说明就很好。

Here is my code: 这是我的代码:

 connection.Open();
 NpgsqlCommand cmd = new NpgsqlCommand();
 cmd.Connection = connection;

 cmd.CommandText = "INSERT INTO student_folio 
 (f_name,l_name,ic,dob,gender,remark)VALUES(@f_name, 
 @l_name,@ic,@dob,@gender,@remark) RETURNING id";

 cmd.CommandText = "INSERT INTO contact (s_id, address)VALUES((SELECT 
 currval(pg_get_serial_sequence('student_folio', 'id'))),@address) ";
 cmd.CommandType = CommandType.Text;

seems you are trying to re assign your cmd.CommandText with new dml value. 似乎您正在尝试重新分配具有新dml值的cmd.CommandText。 Try this. 尝试这个。

connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
cmd.Connection = connection;

cmd.CommandText = "INSERT INTO student_folio (f_name,l_name,ic,dob,gender,remark)VALUES(@f_name, @l_name,@ic,@dob,@gender,@remark) RETURNING id; " +
            "INSERT INTO contact (s_id, address)VALUES((SELECT currval(pg_get_serial_sequence('student_folio', 'id'))),@address);";     
cmd.CommandType = CommandType.Text;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#中的Postgresql Npgsql.PostgresException - Postgresql Npgsql.PostgresException in c# Npgsql.PostgresException 过程未找到异常 - Npgsql.PostgresException procedure not found exception Npgsql.PostgresException:关系“表名”不存在' - Npgsql.PostgresException: relation "tablename" does not exist' C#与SQLEditor — Npgsql.PostgresException 42703:“用户名”列不存在 - C# vs SQLEditor — Npgsql.PostgresException 42703: column “username” does not exist Npgsql.PostgresException (0x80004005): 42883: 运算符不存在: jsonb #> text - Npgsql.PostgresException (0x80004005): 42883: operator does not exist: jsonb #> text Npgsql.PostgresException:'42P01:关系“表”不存在” - Npgsql.PostgresException: '42P01: relation “table” does not exist' 尝试在 C# Winform 中查询 PostgreSQL 数据库时,ExecuteReader 出现 Npgsql.PostgresException 错误 - Npgsql.PostgresException error on ExecuteReader when attempting to query PostgreSQL database in C# Winform PostgreSQL:负载测试抛出错误 - Npgsql.PostgresException:“53300:抱歉,已经有太多客户端” - PostgreSQL: Load testing throws error - Npgsql.PostgresException: '53300: sorry, too many clients already' 连接到 Redshift 集群时出错 - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host - Error connecting to Redshift Cluster - Npgsql.PostgresException: 28000: no pg_hba.conf entry for host 使用 ExecuteScalar 插入时使用 Npgsql 检索序列号 - Retrieve serial ID with Npgsql when inserting with ExecuteScalar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM