I want to add data in two tables in one go.
I have two tables, UserCredentials(userid,username,password,userrole) and EmployeeRecord(userid,username,firstname,lastname,desgination,nationality)
UserId is identity in both tables generated in DB, I am looking for a way that I can get userID in EmployeeRecord at which record is inserted so that I can insert login credentials at same userId in UserCredentials table. Both records need to be inserted at same time.
This is what my code looks like right now:
create procedure enteremployeerecord (
@firstName varchar(100),
@lastName varchar(100),
@desigNation varchar(50),
@natioNality varchar(50),
@userName varchar(50),
@userPassword varchar(50),
@userRole varchar(50)
)
as
begin
insert into EmployeeRecord(username,firstname,lastname,designation,nationality)
values (@userName,@firstName,@lastName,@desigNation,@natioNality)
insert into UserCredentials(username,password,userrole)
values (@userName,@userPassword,@userRole)
And here is LINQ SQL code in C# (Not sure though how it will change for two queries )
EmployeeRecordDBClassDataContext con = new EmployeeRecordDBClassDataContext();
con.enteremployeerecord(firstNameTextBox.Text, lastTextBox.Text, desginationTextBox.Text,nationalityTextBox.Text,userNameTextBox.Text, passwordEmployeeTextBox.Text, userRoleTextBox.Text)
I am using VS 2012 & SQL Server 2012
Trigger- MethodMan