c# - INSERT INTO table with foreign key contraints -
i have read couple things on , came following code. using access database , coding in c# through visual studio.
i getting syntax error in this. have tried create query in access test difficult create in that.
can me figure out why isnt working?
using(oledbconnection conn1 = new oledbconnection(global::insulationprojecttracker.properties.settings.default.insulationdb)) { using(oledbcommand command1 = conn1.createcommand()) { conn.open(); command.commandtext = "insert jobsites (customerid, jobsitename) values ((select customers.customerid customers customers.customername = @cname1 , customers.branchnumber = @bnumber1), @jname1)"; command.parameters.add(new oledbparameter("@cname1", cbocustomername.text)); command.parameters.add(new oledbparameter("@bnumber1", cbobranch.text)); command.parameters.add(new oledbparameter("@jname1", txtjobsitename.text)); command.executenonquery(); conn.close(); command.parameters.clear(); } }
tables setup
branches
pk branchnumber
branchname
customers
pk customerid
customername
fk branchnumber
jobsites
pk jobsiteid
jobsitename
fk customerid
edit***
the below commmand produces no error, doesnt insert data database
command.commandtext = "insert jobsites (customerid, jobsitename) select @jname1, c.customerid customers c c.customername = @cname1 , c.branchnumber = @bnumber1";
access sql not accept alias destination table.
for example, either of these statements trigger "syntax error in insert statement." ...
insert jobsites j (j.customerid, j.jobsitename) values (1, 'foo'); insert jobsites j (j.customerid, j.jobsitename) values (1, 'foo');
without alias, 1 executes without error ...
insert jobsites (customerid, jobsitename) values (1, 'foo');
in order query access accept, think may need switch insert ... select
instead of insert ... values
...
command.commandtext = "insert jobsites (customerid, jobsitename) select c.customerid, @jname1 customers c c.customername = @cname1 , c.branchnumber = @bnumber1";
so @conradfrix suggested earlier without alias jobsites table.
Comments
Post a Comment