我有两张桌子:
Login
有4个属性:
1. NRIC
2. NAME
3. PASSWORD
4. TYPE (USER / ADMIN)
DETAILS
有4个属性:
1. NRIC
2. NAME
3. PASSWORD
4. EMAIL
如果表LOGIN
已更新,我希望表DETAILS
更新。
例如,如果表password
中的DETAILS
已更新,则假设的表LOGIN
也应更新。
SQLCmd.CommandText = "UPDATE DETAILS set PASSWORD='" & TextBoxPassword.Text & "',EMAIL='" & TextBoxEmail.Text & "' WHERE NRIC='" & myVar2 & "'"
我如何更新它们?
答案 0 :(得分:2)
首先更新详细信息表。 然后提交更改。 然后更新登录表。 交易:D
答案 1 :(得分:2)
为此使用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
答案 2 :(得分:1)
MySQL = "DECLARE @NumberOfRowsUpdated INT" & vbcrlf
MySQL &= "" & vbcrlf
MySQL &= "UPDATE details" & vbcrlf
MySQL &= "SET password = '@password'," & vbcrlf
MySQL &= " email = '@email'" & vbcrlf
MySQL &= "WHERE NRIC = '@nric'" & vbcrlf
MySQL &= "" & vbcrlf
MySQL &= "SELECT @NumberOfRowsUpdated = @@ROWCOUNT" & vbcrlf
MySQL &= "" & vbcrlf
MySQL &= "IF @NumberOfRowsUpdated > 0" & vbcrlf
MySQL &= "BEGIN" & vbcrlf
MySQL &= " UPDATE LOGIN" & vbcrlf
MySQL &= " SET password = '@password'," & vbcrlf
MySQL &= " email = '@email'" & vbcrlf
MySQL &= " WHERE NRIC = '@nric'" & vbcrlf
MySQL &= "END" & vbcrlf
MySQL = MySQL.Replace("@password",TextBoxPassword.Text).Replace("@email", TextBoxEmail.Text).Replace("@nric", myVar2)
SqlCmd.Commandtext = MySQL
SqlCmd.ExecuteNonQuery
已发布答案的人已经提交并回复了#39;解决方案是错的。那些解决方案并不认为查询实际执行了#34; UPDATED&#34;细节。此解决方案可确保第一个查询实际上是&#34;更新&#34;在进行第二次查询之前有一行详细信息。
答案 3 :(得分:1)
这是比SQL解决方案更多VB的代码:
Dim intTotalRowsUpdated As Integer
SQLCmd.CommandText = "UPDATE DETAILS set PASSWORD='" & TextBoxPassword.Text & "',EMAIL='" & TextBoxEmail.Text & "' WHERE NRIC='" & myVar2 & "'"
intTotalRowsUpdated = SQLCmd.ExecuteNonQuery()
If intTotalRowsUpdated > 0 Then
SQLCmd.CommandText = "UPDATE login set PASSWORD='" & TextBoxPassword.Text & "',EMAIL='" & TextBoxEmail.Text & "' WHERE NRIC='" & myVar2 & "'"
SQLCmd.ExecuteNonQuery
End If
答案 4 :(得分:1)
您可以使用存储过程来执行此任务。 像下面的东西,
CREATE PROC proc_UpdateProcedure
@PASSWORD VARCHAR(100),
@NRIC INT
AS
DECLARE @Ret INT
BEGIN TRANSACTION
BEGIN TRY
-- UPDATE DETAILS TABLE
UPDATE DETAILS SET PASSWORD = @PASSWORD WHERE NRIC = @NRIC
-- UPDATE Login TABLE
UPDATE Login SET PASSWORD = @PASSWORD WHERE NRIC = @NRIC
COMMIT TRANSACTION
SET @Ret = 1
SELECT @Ret
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
SET @Ret = 0
SELECT @Ret
END CATCH
答案 5 :(得分:0)
如果你真的想要在更新一个表时更新另一个表,你可能需要考虑一个触发器:
create trigger trigUpdateDetails
on details
after update
as
begin
update login
set
password=ins.password
from login td
join inserted ins on td.nric = ins.nric
end
此触发器导致详细信息表的任何更新也更新登录表的密码字段,无论更新源自客户端应用程序还是源自数据库级别的语句。
更多信息:
http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
文章有点陈旧,但文章中的信息仍然相关。