【发布时间】:2023-03-16 15:58:01
【问题描述】:
我已经存储了首先update 是UserSignUp 表中的值,然后是insert 中UserKeyPoints 表中的值的过程,但我的过程没有执行。
这是我的存储过程:
CREATE PROC [dbo].[proc_getActivationCode] @ActivationCode VARCHAR(1000)=''
AS
BEGIN
IF EXISTS(SELECT ActivationCode
FROM UserSignUp
WHERE ActivationCode = @ActivationCode
AND Activate = 'False')
BEGIN
DECLARE @UserId INT
SET @userid= (SELECT AutoID
FROM UserSignUp
WHERE ActivationCode = @ActivationCode)
UPDATE UserSignUp
SET Activate = 'Confirm Code'
WHERE ActivationCode = @ActivationCode
INSERT INTO UserKeyPoints
(KeyPoints,
UserId)
VALUES (500,
@userid)
SELECT 1
END
ELSE
BEGIN
SELECT 2
END
END
这是我正在执行存储过程的 c# 代码。
if (Request.QueryString["token"] != null)
{
Label1.Text = Request.QueryString["token"];
con.Open();
SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
cmd.Parameters.AddWithValue("@ActivationCode", Request.QueryString["token"].ToString());
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dr.Close();
con.Close();
if (dt.Rows[0][0].ToString() == "1")
{
//Label1.Text = "You are confirmed successfully. Please Click here for Login: ";
SendEmail objMail = new SendEmail();
}
else
{
Label1.Text = "You are already confirmed.";
}
}
当我执行此代码时,它会在没有insert 和update 的情况下运行该过程,并且在我的.aspx 页面上我得到Label1 的输出,即You are already confirmed.
谁能指导我这哪里出了问题?
【问题讨论】:
-
您的 SP 存在一些并发问题。
-
关于上述内容,您可以替换整个
IF EXIST5内容,然后无论如何都进行更新,然后使用OUTPUT子句将所需的值直接插入UserKeyPoints。然后检查@@ROWCOUNT以了解要返回的值。所以基本上存储的过程变成了两个语句,它解决了并发问题。 -
@MartinSmith 好的。这样我会好很多。谢谢。让我们研发这项技术。
-
@MartinSmith 因为我已经解决了我的问题,但我很高兴我找到了替代方法。从现在开始,我会尽量让我注意并发问题。
标签: c# asp.net sql-server stored-procedures