如何插入多个表 [英] how to insert into multiple tables

查看:62
本文介绍了如何插入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将名字",姓氏",电话"之类的值插入到客户表"中,并将地址,国家,城镇"的值插入到地址表"中,但是我想使用单个查询字符串,如何执行此操作,请帮助我提供代码. br/> 关于bishnu karki

hi frens, i want to insert the values like firstname, surname,phone into Customertable and address,country,town into Addresstable but i want to use the single query string , how to perform this please help me with the code.
with regards bishnu karki

推荐答案

您将在3条SQL语句中执行此操作.首先是将数据添加到客户表.接下来将获取刚刚添加的客户的ID.最后一个会将地址数据添加到其表中,并使用客户的ID来维护关系.

您可以使用单个SqlCommand来完成此操作,但是您将调用存储过程来处理将数据添加到表中. Google的" ]".
You''d do this in 3 SQL statements. The first is going to add the data to the customer table. The next is going to get the ID of the customer just added. The last one will add the address data to its table, using the ID of the customer to maintain the relation.

You can do it with a single SqlCommand, but you''re going to call a stored procedure to handle adding the data to the tables. Google for ""c# call stored procedure with parameters[^]" for examples.


插入是在单个表上完成的,您可以做的是启动Transaction并通过两个查询和完成后.

这将确保以原子方式完成这两个查询.
Inserts are done on single tables, what you can do is start a Transaction and insert into the two tables via two queries and Commit when done.

This will make sure the two queries are done in an atomic fashion.


我在

这篇关于如何插入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆