我有3张桌子 -
1. Country (CountryName, CID (PK- AutoIncrement))
2. State (SID(PK- AutoIncrement), StateName, CID (FK to Country)
3. City (CityName, CID, SID (FK to State)
现在我需要只使用CountryName,StateName和CityName将名称插入到三个表中。需要更新ID。
我是怎么做到的?
谢谢,
答案 0 :(得分:0)
您可以将存储过程用于存储过程中的内容,您可以先将其插入到国家/地区表中:
Insert into Country ( CountryName) VALUES (@CountryName)
DECLARE @cid as INTEGER = @@IDENTITY
然后在secound插入中使用SELECT @@ IDENTITY,如下所示:
Insert into State( StateName, cid ) values (@StateName, @cid)
DECLARE @SID as INTEGER = @@IDENTITY
并在第三个插入语句中使用相同的内容:
Insert into City ( CityName, CID,SID ) values (@CityName,@CID,@SID )
这就是你需要的全部
答案 1 :(得分:0)
如果您想使用linq to SQL
var country = new Dbml.Country();
country.Name = "countryname";
var state = new Dbml.State();
state.Country = country;
state.Name = "stateName"
var city = new Dbml.City();
city.State= state;
city.cityName = "cityName";
context.SubmitChanges();