Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have two postgreSQL tables linked by a foreign key. I want to insert into the second table the data coming from the first table primary key. I wrote that in my c# application :

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();

For the second table:

command2.CommandText = "CREATE TABLE table2(table1id CHAR(256), champs1 CHAR(256), foreign key (table1id) references table1(ID))";
command2.ExecuteNonQuery();

The problem is that i dont know how to insert in table 2 the first column which is the primary key of the first table.

Any thoughts about that ? Thank you for your help :)

share|improve this question
2  
when you wanna insert the value in second table –  Rajeev Kumar May 15 '13 at 8:53

1 Answer 1

up vote 0 down vote accepted

Try to add following code.

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();

Hope its helpful.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.