insert into postgresql table using c# -
i have 2 postgresql tables linked foreign key. want insert second table data coming first table primary key. wrote in c# application :
command1.commandtext = "create table table1(id char(256) constraint id primary key, title char)"; command1.executenonquery(); command1.commandtext = "insert projects (id, title)" + "values(@id, @title);"; command1.parameters.addwithvalue("@id", id); command1.parameters.addwithvalue("@title",title); command1.executenonquery(); for second table:
command2.commandtext = "create table table2(table1id char(256), champs1 char(256), foreign key (table1id) references table1(id))"; command2.executenonquery(); the problem dont know how insert in table 2 first column primary key of first table.
any thoughts ? thank :)
try add following code.
cmd3=new sqlcommand("insert 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 helpful.
Comments
Post a Comment