From C#, you can try something like this:
// define the INSERT query - insert a firstname,surname into "Person",
// and insert a row into the Emplyoee table with the new ID
// created by the first insert
string insertStmt = @"BEGIN TRANSACTION
INSERT INTO dbo.Person(FirstName, Surname) VALUES(@FirstName, @Surname);
DECLARE @NewPersonId INT = SCOPE_IDENTITY();
INSERT INTO dbo.Employee(EmployeeId, HoursWorked) VALUES(@NewPersonId, @HoursWorked);
COMMIT TRANSACTION;"
// define connection and command for inserting data
using (SqlConnection conn = new SqlConnection(-your-connection-string-here-))
using (SqlCommand cmdInsert = new SqlCommand(insertStmt, conn))
{
// Define parameters - adapt datatype and max length as required
cmdInsert.Parameters.Add("@FirstName", SqlDbType.VarChar, 100);
cmdInsert.Parameters.Add("@Surname", SqlDbType.VarChar, 100);
cmdInsert.Parameters.Add("@HoursWorked", SqlDbType.Int);
// set parameter values
cmdInsert.Parameters["@FirstName"].Value = "John";
cmdInsert.Parameters["@Surname"].Value = "Doe";
cmdInsert.Parameters["@HoursWorked"].Value = 35;
// Open connection, execute query, close connection
conn.Open();
int rowsInserted = cmdInsert.ExecuteNonQuery();
conn.Close();
}
Update: as mentioned by @charlieface, you can write this code more concisely IF you're only ever inserting a single row - like this:
// define connection and command for inserting data
using (SqlConnection conn = new SqlConnection(-your-connection-string-here-))
using (SqlCommand cmdInsert = new SqlCommand(insertStmt, conn))
{
// Define and set parameters
cmdInsert.Parameters.Add("@FirstName", SqlDbType.VarChar, 100).Value = "John";
cmdInsert.Parameters.Add("@Surname", SqlDbType.VarChar, 100).Value = "Doe";
cmdInsert.Parameters.Add("@HoursWorked", SqlDbType.Int).Value = 35;
// Open connection, execute query, close connection
conn.Open();
int rowsInserted = cmdInsert.ExecuteNonQuery();
conn.Close();
}