我有两个由外键链接的表,我需要在VB.net SQL Server中同时向两个表插入数据

我有两个由外键链接的表,我需要在VB.net SQL Server中同时向两个表插入数据,sql,asp.net,vb.net,Sql,Asp.net,Vb.net,以下是两个表格 供应商 登录详细信息 试着这样做: BEGIN TRY BEGIN TRAN insert into Supplier([Supplier Name]) values ('abce') if @@IDENTITY is not NULL BEGIN insert into LoginDetails(Username,Password) values ('User1','dfd845jdfgdf',@@IDENTITY) END COMMIT TRAN END TRY B

以下是两个表格

供应商

登录详细信息

试着这样做:

BEGIN TRY
BEGIN TRAN 
insert into Supplier([Supplier Name]) values
('abce')


if @@IDENTITY is not NULL
BEGIN
insert into LoginDetails(Username,Password) values
('User1','dfd845jdfgdf',@@IDENTITY)
END

COMMIT TRAN
END TRY

BEGIN CATCH
ROLLBACK TRAN
END CATCH

我按照下面的方法做了,非常感谢你的努力

        sqlString1 = "INSERT INTO Supplier(SupplierName, CompanyRegNo, SupplierCat, SupplierAdd, SupplierContact, SupplierEmail, SupplierStat, RegisterDate) VALUES ('" & txtname.Text & "' , '" & txtcompreg.Text & "', '" & drpcat.Text & "', '" & txtadd.Text & "', '" & txtcontact.Text & "', '" & txtemail.Text & "', 'Active' , '" & DateAndTime.Today & "' )"
        sqlString1 &= "INSERT INTO LoginDetails(Username, SupplierID, Password, Position) values ('" & txtusername.Text & "' ,(Select MAX(SupplierID) From Supplier), '" & txtcpass.Text & "', 'Supplier')"

你必须从什么代码开始?我们将帮助您解决问题,但不会从头开始为您编写代码。
BEGIN TRY
BEGIN TRAN 
insert into Supplier([Supplier Name]) values
('abce')


if @@IDENTITY is not NULL
BEGIN
insert into LoginDetails(Username,Password) values
('User1','dfd845jdfgdf',@@IDENTITY)
END

COMMIT TRAN
END TRY

BEGIN CATCH
ROLLBACK TRAN
END CATCH
        sqlString1 = "INSERT INTO Supplier(SupplierName, CompanyRegNo, SupplierCat, SupplierAdd, SupplierContact, SupplierEmail, SupplierStat, RegisterDate) VALUES ('" & txtname.Text & "' , '" & txtcompreg.Text & "', '" & drpcat.Text & "', '" & txtadd.Text & "', '" & txtcontact.Text & "', '" & txtemail.Text & "', 'Active' , '" & DateAndTime.Today & "' )"
        sqlString1 &= "INSERT INTO LoginDetails(Username, SupplierID, Password, Position) values ('" & txtusername.Text & "' ,(Select MAX(SupplierID) From Supplier), '" & txtcpass.Text & "', 'Supplier')"