作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两张 table :
Login
有 4 个属性:
1. NRIC
2. NAME
3. PASSWORD
4. TYPE (USER / ADMIN)
DETAILS
有 4 个属性:
1. NRIC
2. NAME
3. PASSWORD
4. EMAIL
如果表 DETAILS
已更新,我希望表 LOGIN
被更新。
例如,如果表 DETAILS
中的 password
已更新,则假定表 LOGIN
也应更新。
SQLCmd.CommandText = "UPDATE DETAILS set PASSWORD='" & TextBoxPassword.Text & "',EMAIL='" & TextBoxEmail.Text & "' WHERE NRIC='" & myVar2 & "'"
如何同时更新它们?
最佳答案
为此使用SqlTransaction,以下是引用代码:
C#:
SqlConnection conn = new SqlConnection("[Your connection string]");
conn.Open();
SqlTransaction Trans = conn.BeginTransaction();
SqlCommand SQLCmd = new SqlCommand("",conn);
SQLCmd.Transaction = Trans;
try
{
SQLCmd.CommandText = "UPDATE DETAILS set PASSWORD='" & TextBoxPassword.Text & "',EMAIL='" & TextBoxEmail.Text & "' WHERE NRIC='" & myVar2 & "'";
SQLCmd.ExecuteNonQuery();
SQLCmd.CommandText = "UPDATE Login set PASSWORD='" & TextBoxPassword.Text & "' WHERE NRIC='" & myVar2 & "'";
SQLCmd.ExecuteNonQuery();
Trans.Commit();
}
catch
{
Trans.Rollback();
}
finally {
conn.Close();
}
VB:
Dim conn As New SqlConnection("[Your connection string]")
conn.Open()
Dim Trans As SqlTransaction = conn.BeginTransaction()
Dim SQLCmd As New SqlCommand("", conn)
SQLCmd.Transaction = Trans
Try
SQLCmd.CommandText = "UPDATE DETAILS set PASSWORD='" And TextBoxPassword.Text And "',EMAIL='" And TextBoxEmail.Text And "' WHERE NRIC='" And myVar2 And "'"
SQLCmd.ExecuteNonQuery()
SQLCmd.CommandText = "UPDATE Login set PASSWORD='" And TextBoxPassword.Text And "' WHERE NRIC='" And myVar2 And "'"
SQLCmd.ExecuteNonQuery()
Trans.Commit()
Catch
Trans.Rollback()
Finally
conn.Close()
End Try
关于sql-server - 如何在一个查询中更新 2 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577279/
我是一名优秀的程序员,十分优秀!