[英]C# Insert Value Foreign Key
I have 2 tables which are tableCustomerLogin and tableCustomerRegister. 我有2个表,分别是tableCustomerLogin和tableCustomerRegister。 There is Foreign Key for the tableCustomerLogin ie cust_id .
tableCustomerLogin有外键,即cust_id 。
In the tableCustomerLogin, I have tableCustomerLogin 在tableCustomerLogin中,我有tableCustomerLogin
cust_login_id
cust_id
cust_email
cust_username
cust_password
and for tableCustomerRegister, 对于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
When customer register, the data will store in the tableCustomerRegister. 客户注册时,数据将存储在tableCustomerRegister中。 How to make it register in the tableCustomerLogin?
如何使其在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.";
}
well, first of all you need to change your query 好吧,首先您需要更改查询
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
return int, in this case it will return cust_id
as your query have OUTPUT INSERTED.cust_id
. ExecuteScalar
返回int,在这种情况下,它将返回cust_id
因为您的查询具有OUTPUT INSERTED.cust_id
。 Now You have your inserted cust_id
saved in tableCustomerRegister . 现在,您已将插入的
cust_id
保存在tableCustomerRegister中 。 Now all you need to do just write another query for save data into your tableCustomerLogin with foreign key cust_id
. 现在,您只需要编写另一个查询即可使用外键
cust_id
将数据保存到tableCustomerLogin中 。 like this, 像这样,
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
You can first insert
a tableCustomerRegister
record and then insert
another data the tableCustomerLogin
table. 您可以首先
insert
tableCustomerRegister
记录,然后在tableCustomerLogin
表中insert
另一个数据。 You would be better do this in the transaction block. 您最好在事务块中执行此操作。
The other way , You can add a trigger
to the tableCustomerLogin
table. 另一种方法是,您可以将
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
Probably the best solution is to follow the DRY principle, Dont Repeat Yourself. 最好的解决方案可能是遵循DRY原则,“不要重复自己”。
I think that you can store all information in a single table customer_table for example and then retrive the necessary data with a more simple logic from only this table. 我认为您可以将所有信息存储在单个表customer_table中,然后仅从该表中使用更简单的逻辑来检索必要的数据。
Instead, if you want to stay on your actual data structure, simply add new insert statement after firstseparate by semicolon 相反,如果您想保留实际的数据结构,只需在以分号分隔第一句之后添加新的insert语句即可。
how to insert data into multiple tables at once 如何一次将数据插入多个表
there are two different way. 有两种不同的方式。 you shold use SCOPE_IDENTITY
您应使用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 Returns the last identity value inserted into an identity column in the same scope
SCOPE_IDENTITY返回插入到同一作用域的标识列中的最后一个标识值
var newId= cmd.ExecuteScaler();
newId created new Primary Key ID for you cust_id
newId为您创建了新的主键ID cust_id
you have this ID(cust_id) and you can register in the Login table. 您具有此ID(cust_id),并且可以在“登录”表中注册。
INSERT INTO tableCustomerLogin (cust_login_id,**cust_id**,cust_email,cust_username,cust_password)
cust_id = newId
If Cust_id is auto-created in the tableCustomerRegister table, then you can save the same id in the tableCustomerLogin table (cust_id). 如果在tableCustomerRegister表中自动创建了Cust_id,则可以将相同的id保存在tableCustomerLogin表中(cust_id)。 then only your foreign Key relation work.
那么只有您的外键关系起作用。
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.";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.