I created a register page for an mvc project. I want to add user information to a SQL database but, in the database I created two different but connected tables, person
and address
. addressId
is a foreign key to the person
table. I don't know how to add to person info to both the person
and address
table at the same time.
[image _person]
[image _address]
CodePudding user response:
You can create a stored procedure that will take all parameters that are needed for both records and perform the insert into
commands for both tables. You will also need to wrap a transaction around the insertion logic, so if one of the inserts succeeds but the other fails, then your db will safely roll back and notify you about the error. Otherwise your transaction can be committed.
CodePudding user response:
Best and secure way to insert by stored procedure with transaction.