c# - Insert data to multiple tables -


i need inserting data multiple tables. have 3 tables:

  • revizija (rev_id, broj_rev)
  • verzija (verz_id, broj_verz, rev_id)
  • program (prog_id, naziv_prog, verz_id)

connected fk - revizija verzija, verzija programski sustav.

my insert fails when try run program.

sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["database1"].connectionstring); sqlcommand xp = new sqlcommand("insert revizija values(@broj_rev)", con); xp.parameters.addwithvalue("@broj_rev", textbox5.text);  con.open(); xp.executenonquery(); con.close();  sqlcommand xp1 = new sqlcommand("insert verzija values(@broj)", con); xp1.parameters.addwithvalue("@broj", textbox4.text);  con.open(); xp1.executenonquery(); con.close();  sqlcommand xp4 = new sqlcommand("insert program values (@naziv_prog)", con); xp4.parameters.addwithvalue("@naziv_prog", textbox2.text);  con.open(); xp4.executenonquery(); con.close(); 

when try insert data in verzija table need know value assigned primary key of revizija table , same happens when try insert record in program table revizija.

in scenario use ability of sql server execute 2 commands , add each of first 2 commands sql select scope_identity() command text. scope_identity returns value of last identity value assigned through connection. result of command can read using executescalar , used input parameter following command.

using(sqlconnection con = new sqlconnection(....)) using(sqlcommand xp = new sqlcommand(@"insert revizija                                         (broj_rev) values(@broj_rev);                                        select scope_identity()", con); {     con.open();     xp.parameters.addwithvalue("@broj_rev", textbox5.text);     int rev_id convert.toint32(xp.executescalar());      using(sqlcommand xp1 = new sqlcommand(@"insert verzija                      (broj_verz, rev_id) values(@broj,@rev_id);                     select scope_identity()", con);     {         xp1.parameters.addwithvalue("@broj", textbox4.text);         xp1.parameters.addwithvalue("@rev_id", rev_id);         int verz_id = convert.toint32(xp1.executescalar());          using(sqlcommand xp4 = new sqlcommand(@"insert program                  (naziv_prog, verz_id) values (@naziv_prog, @verz_id)", con);         {             xp4.parameters.addwithvalue("@naziv_prog", textbox2.text);             xp4.parameters.addwithvalue("@verz_id", verz_id);             xp4.executenonquery();         }     } } 

notice have fixed main error in queries. in insert statement, if omit column names, required provide values fields present (otherwise engine not able correctly assign passed values matching columns) clarity , avoid future errors specify columns updated query


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -