简体   繁体   中英

Two Primary keys of different tables as foreign keys to another table

I have a winForms application with DataGridView, 3 tables and some queries.

Lets say I have Tables , A,B,C. In table AI have an auto increment primary Key and also in table B. Tables A and B are both strangers.

Table C is a Join Table that have 2 columns:

1) A primary Key

2) B primary Key

Now, in the form I have a button and it's just programmatically inserts values for table A and B. What I'm trying to do is to insert the primary keys of tables A and B to the right columns in table C. is it possible to do it programmatically?

if you using sql server you can run in your buttonclick code this:

declare @aid int, @bid int 
insert A(col1) values(val1) 
set @aid = scope_identity()

insert B(col1) values(val1) 
set @bid = scope_identity()

insert C(aid, bid) values(@aid, @bid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM