使用外键插入两个表

时间:2018-01-26 16:23:31

标签: sql-server vb.net

我在数据库中添加数据时出现问题。我有两张桌子;第一个是tblemployee_information,另一个是tblemployee_account。我的问题是,如何将tblemployee_account中的employee_id保存为外键?它正在工作,但是在我添加数据后,tblemployee_account中的员工ID为NULL。

表格

CREATE TABLE tblemployee_information (
    employee_id int NOT NULL IDENTITY(1,1) PRIMARY KEY
    employee_first_name varchar(100) NOT NULL,
    employee_middle_name varchar(100) NOT NULL,
    employee_last_name varchar(100) NOT NULL,
    employee_contact varchar(100) NOT NULL,
    employee_address varchar(200) NOT NULL  
);

--Table Structure for Employees Account
CREATE TABLE tblemployee_account (
    employee_account_id int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    employee_email varchar(100) NOT NULL,
    employee_username varchar(50) NOT NULL,
    employee_password varchar(50) NOT NULL,
    employee_id int REFERENCES tblemployee_information(employee_id)
);

添加数据的代码

Public Sub addEmployee()
        Try
            dbConnection()
            insert_query = "INSERT INTO tblemployee_information(employee_first_name, employee_middle_name, employee_last_name, employee_contact, employee_address) VALUES (@employee_first_name, @employee_middle_name, @employee_last_name, @employee_contact, @employee_address)"
            command1 = New SqlCommand
            With command1
                .Connection = connection
                .CommandText = insert_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@employee_first_name", Register.txtFirstName.Text)
                .Parameters.AddWithValue("@employee_middle_name", Register.txtMiddleName.Text)
                .Parameters.AddWithValue("@employee_last_name", Register.txtLastName.Text)
                .Parameters.AddWithValue("@employee_contact", Register.txtContact.Text)
                .Parameters.AddWithValue("@employee_address", Register.txtAddress.Text)
                .ExecuteNonQuery()
            End With

            'This command will insert the email, username & password from another table
            insert_query = "INSERT INTO tblemployee_account(employee_email, employee_username, employee_password) VALUES (@employee_email, @employee_username, @employee_password)"
            command2 = New SqlCommand
            With command2
                .Connection = connection
                .CommandText = insert_query
                .Parameters.Clear()
                .Parameters.AddWithValue("@employee_email", Register.txtEmail.Text)
                .Parameters.AddWithValue("@employee_username", Register.txtUsername.Text)
                .Parameters.AddWithValue("@employee_password", Register.txtPassword.Text)
                .ExecuteNonQuery()
            End With
        Catch ex As SqlException
            MsgBox(ex.Message)
        Finally
            connection.Close()
            command1.Dispose()
            command2.Dispose()
            MsgBox("Data Added Successfully.")
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

如果我正在构建,我会在存储过程中进行两次插入。这是你的程序的样子。

但这里存在一个主要问题。您以纯文本格式存储密码。这是处理密码的绝对可怕方式。他们需要随时进行盐渍和散列。这确实超出了你的问题的范围,但如果我不提及并强调这是一个多大的问题,那将是一个不完整的回应。你必须解决这个问题!!!

create procedure Employee_Inserts
(
    @employee_first_name varchar(100),
    @employee_middle_name varchar(100),
    @employee_last_name varchar(100),
    @employee_contact varchar(100),
    @employee_address varchar(200), 
    @employee_email varchar(100),
    @employee_username varchar(50),
    @employee_password varchar(50)
) as begin

    set nocount on;

    INSERT INTO tblemployee_information
    (
        employee_first_name
        , employee_middle_name
        , employee_last_name
        , employee_contact
        , employee_address
    ) 
    VALUES 
    (
        @employee_first_name
        , @employee_middle_name
        , @employee_last_name
        , @employee_contact
        , @employee_address
    )

    INSERT INTO tblemployee_account
    (
        employee_account_id
        , employee_email
        , employee_username
        , employee_password
    ) 
    VALUES
    (
        SCOPE_IDENTITY()
        , @employee_email
        , @employee_username
        , @employee_password
    )
end