Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用c插入到postgresql表中#_C#_Postgresql_Foreign Keys - Fatal编程技术网

C# 使用c插入到postgresql表中#

C# 使用c插入到postgresql表中#,c#,postgresql,foreign-keys,C#,Postgresql,Foreign Keys,我有两个用外键链接的postgreSQL表。 我想在第二个表中插入来自第一个表主键的数据。 我在我的c#申请中写道: command1.CommandText = "CREATE TABLE table1(ID CHAR(256) CONSTRAINT id PRIMARY KEY, Title CHAR)"; command1.ExecuteNonQuery(); command1.CommandText = "insert into projects (ID, Title)" + "valu

我有两个用外键链接的postgreSQL表。 我想在第二个表中插入来自第一个表主键的数据。 我在我的c#申请中写道:

command1.CommandText = "CREATE TABLE table1(ID CHAR(256) CONSTRAINT id PRIMARY KEY, Title CHAR)";
command1.ExecuteNonQuery();
command1.CommandText = "insert into projects (ID, Title)" + "values(@id, @title);";
command1.Parameters.AddWithValue("@id", ID);
command1.Parameters.AddWithValue("@title",Title);
command1.ExecuteNonQuery();
关于第二个表:

command2.CommandText = "CREATE TABLE table2(table1id CHAR(256), champs1 CHAR(256), foreign key (table1id) references table1(ID))";
command2.ExecuteNonQuery();
问题是我不知道如何在表2中插入第一列,它是第一个表的主键

有什么想法吗?
感谢您的帮助:)

尝试添加以下代码

cmd3=new SqlCommand("insert into table2 values(@table1id,@champs1,@table1id2)",conn);
cmd3.parameter.AddWithValue("@table1id",ID);//ID=ID of first table
cmd3.parameter.AddWithValue("@champs1",chaps);
cmd3.parameter.AddWithValue("@table1id2",ID);//ID=ID of first table
cmd3.ExecuteNonQuery();

希望对您有所帮助。

当您想在第二个表中插入值时