[英]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.