C#插入值外键

时间:2018-10-24 05:51:45

标签: c# sql ado.net

我有2个表,分别是tableCustomerLogin和tableCustomerRegister。 tableCustomerLogin有外键,即 cust_id

在tableCustomerLogin中,我有tableCustomerLogin

    cust_login_id
    cust_id
    cust_email
    cust_username
    cust_password

,对于tableCustomerRegister,

    tableCustomerRegister
    cust_id
    cust_fullname
    cust_username
    cust_email
    cust_password
    cust_mobile_number
    cust_image
    cust_address1
    cust_address2
    cust_city
    cust_postcode
    cust_create_acc_time

客户注册时,数据将存储在tableCustomerRegister中。如何使其在tableCustomerLogin中注册?

string sql = @"INSERT INTO tableCustomerRegister VALUES (@cust_fullname, @cust_username, @cust_email, @password, @cust_mobile_phone, @cust_address1, @cust_address2, @cust_image, @cust_city, @cust_state, @cust_postcode, @cust_create_acc_time, @role, @enabled)";

            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@cust_fullname", txtFirstName.Text + " " + txtLastName.Text);
            cmd.Parameters.AddWithValue("@cust_username", txtUsername.Text);
            cmd.Parameters.AddWithValue("@cust_email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@password", passwordhash);
            cmd.Parameters.AddWithValue("@cust_mobile_phone", txtMobilePhone.Text);
            cmd.Parameters.AddWithValue("@cust_address1", txtAddress1.Text);
            cmd.Parameters.AddWithValue("@cust_address2", txtAddress2.Text);
            cmd.Parameters.AddWithValue("@cust_image", txtProfilePicture.Text);
            cmd.Parameters.AddWithValue("@cust_city", ICityString());
            cmd.Parameters.AddWithValue("@cust_state", ddState.SelectedValue.ToString());
            cmd.Parameters.AddWithValue("@cust_postcode", txtPostcode.Text);
            cmd.Parameters.AddWithValue("@cust_create_acc_time", DateTime.Now);
            cmd.Parameters.AddWithValue("@role", "user");
            cmd.Parameters.AddWithValue("@enabled", enabled);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                lblStatus.Text = "Status: Data successfully saved.";
            }

5 个答案:

答案 0 :(得分:1)

好吧,首先您需要更改查询

string sql = @"INSERT INTO tableCustomerRegister  OUTPUT INSERTED.cust_id VALUES (@cust_fullname, @cust_username, @cust_email, @password, @cust_mobile_phone, @cust_address1, @cust_address2, @cust_image, @cust_city, @cust_state, @cust_postcode, @cust_create_acc_time, @role, @enabled)";
 SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@cust_fullname", txtFirstName.Text + " " + txtLastName.Text);
        cmd.Parameters.AddWithValue("@cust_username", txtUsername.Text);
//so on (all your parameters)
var custid  = (int)cmd.ExecuteScalar()

ExecuteScalar返回int,在这种情况下,它将返回cust_id,因为您的查询具有OUTPUT INSERTED.cust_id。现在,您已将插入的cust_id保存在 tableCustomerRegister 中。现在,您只需要用外键cust_id编写另一个查询即可将数据保存到 tableCustomerLogin 中。像这样

string Sql2 = "INSERT INTO tableCustomerLogin (column_names) VALUES (parameters values)";
SqlCommand cmd = new SqlCommand(sq2, conn);
cmd.Parameters.AddWithValue("@cust_id",custid);  //as foreign key
//all other Parameters

答案 1 :(得分:1)

您可以首先insert记录tableCustomerRegister,然后insert将另一数据tableCustomerLogin表中。您最好在交易块中执行此操作。

另一种方法是,您可以将trigger添加到tableCustomerLogin表中。

CREATE TRIGGER trg_tableCustReg ON tableCustomerRegister
FOR INSERT
AS   
  /*
   *  if CustLoginID is a identity , no dont need to add  
   */ 
    INSERT INTO tableCustomerLogin 
            (cust_login_id, cust_id, cust_email, cust_username, cust_password)
        Select
            'CustLoginID', 
            cust_id , 
            cust_email, 
            cust_username, 
            user_password
            FROM inserted

go

答案 2 :(得分:0)

最好的解决方案是遵循DRY原则,不要重复自己。

我认为您可以将所有信息存储在单个表customer_table中,然后仅从该表中使用更简单的逻辑来检索必要的数据。

相反,如果您想保留实际的数据结构,只需在以分号分隔第一个分隔符后添加新的插入语句

how to insert data into multiple tables at once

答案 3 :(得分:0)

有两种不同的方式。您应使用SCOPE_IDENTITY

string sql = @"INSERT INTO tableCustomerRegister VALUES (@cust_fullname, @cust_username, @cust_email, @password, @cust_mobile_phone, @cust_address1, @cust_address2, @cust_image, @cust_city, @cust_state, @cust_postcode, @cust_create_acc_time, @role, @enabled) SELECT SCOPE_IDENTITY()";
  

SCOPE_IDENTITY 返回插入到   同一范围内的身份列

var newId= cmd.ExecuteScaler();
  

newId为您 cust_id

创建了新的主键ID。

您具有此 ID(cust_id),并且可以在“登录”表中注册。

INSERT INTO  tableCustomerLogin (cust_login_id,**cust_id**,cust_email,cust_username,cust_password)


cust_id = newId 

答案 4 :(得分:0)

如果在tableCustomerRegister表中自动创建了Cust_id,则可以在tableCustomerLogin表(cust_id)中保存相同的ID。那么只有您的外键关系起作用。

     try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        SqlCommand get_custid_cmd = new SqlCommand("select @@identity", conn);
                        int cust_id = Convert.ToInt32(get_custid_cmd.ExecuteScalar());
                        string sql_insert = @"INSERT INTO tableCustomerLogin VALUES (@cust_id, @cust_username, @cust_email, @password)";

                SqlCommand cmd_insert = new SqlCommand(sql_insert, conn);
                cmd_insert.Parameters.AddWithValue("@cust_id",cust_id);
                cmd_insert.Parameters.AddWithValue("@cust_username", txtUsername.Text);
                cmd_insert.Parameters.AddWithValue("@cust_email", txtEmail.Text);
                cmd_insert.Parameters.AddWithValue("@password", passwordhash);
                cmd_insert.ExecuteNonQuery();
                            lblStatus.Text = "Status: Data successfully saved.";

                    }