![](/img/trans.png)
[英]Can I use claims data to insert CreatedBy user data into custom tables?
[英]Can I insert data to a column in 3 different tables?
這是我的代碼。 我有3張桌子叫
帳戶,WaterDB,ElectricDB和GasDB。
-在下面的代碼中,我將信息插入帳戶表。
在我的帳戶WaterDB,ElectricDB和GasDB中,我有一列名為username。
是否可以將用戶名也插入其他表的“用戶名”列中?
我在帳戶表中插入信息的代碼:
protected void registerBtn_Click1(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["userDatabaseConnectionString1"].ConnectionString);
conn.Open();
string insertQuery = "insert into accounts (username,email,password,postalcode,role) values (@Uname ,@email ,@pswd ,@pstlc,@role)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Uname", registerUsernameTB.Text);
com.Parameters.AddWithValue("@email", registerEmailTB.Text);
com.Parameters.AddWithValue("@pswd", registerUserPswdTB.Text);
com.Parameters.AddWithValue("@pstlc", postalCodeTB.Text);
com.Parameters.AddWithValue("@role", role.Text);
com.ExecuteNonQuery();
Response.Redirect("Loginx.aspx");
Response.Write("Registration is successful");
conn.Close();
}
希望這些(帳戶,WaterDB,ElectricDB和GasDB)是同一數據庫中的不同表。 然后,您可以在一條語句中運行多個插入查詢
例如:
string insertQuery = "insert into accounts (...) values (...); insert into othertable (...) values (...)"
或者,您可以將觸發器寫入其他插入刪除操作或其他操作。
或者,您也可以編寫一個存儲過程以插入到多個表中
我並不是說這就是您應該如何處理您的注冊系統,只是回答您的問題。
只需創建3個連接-每個數據庫1個-並在每個數據庫上運行相同的代碼即可。 您可以重用很多代碼,只需為每個代碼更改數據庫連接對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.