简体   繁体   English

如何将数据插入主键和外键

[英]how to insert data into primary key and foreign key

I have three tables: 我有三个表:

PATIENT DETAILS 患者细节

ptID

firstName

lastName

address

IMPLANT DETAILS 植入细节

impID

ptID

productID

productSerialNo

PRODUCT 产品

productID

productName

I am inserting data in PATIENT DETAILS and IMPLANT DETAILS table on the same page from single button click . 我只需单击一次,即可在同一页面上的“ PATIENT DETAILS和“ IMPLANT DETAILS表中插入数据。

I have a foreign key on the table IMPLANT DETAILS (productID) which I populate through the value on the dropdown list. 我在表IMPLANT DETAILS (productID)上有一个外键,该键通过下拉列表中的值填充。

What my problem is how will I populate foreign key (ptID) in IMPLANT DETAILS table? 我的问题是如何在foreign key (ptID) IMPLANT DETAILS表中填充foreign key (ptID) When ptID (PATIENT DETAIL) will also be generated from the same button click at almost of the same time. 当几乎从同一时间单击同一按钮也将生成ptID (PATIENT DETAIL)时。 How would I do this in C#? 我将如何在C#中执行此操作?

I suggest you to use entity framework for this. 我建议您为此使用实体框架。 Please look this post Inserting Objects 请看这篇文章插入对象

Step By Step Example of Entity Framework 实体框架的分步示例

You need to fire two sql queries to do this. 您需要触发两个sql查询才能执行此操作。

INSERT INTO PATIENT_DETAILS (firstName,lastName,address) VALUES(NULL,'text');         
INSERT INTO IMPLANT_DETAILS(ptID,productID,productSerialNo) VALUES(LAST_INSERT_ID(),1,'text'); 
COMMIT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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