【问题标题】:stored procedure is not inserting values in table存储过程未在表中插入值
【发布时间】:2023-03-16 15:58:01
【问题描述】:

我已经存储了首先updateUserSignUp 表中的值,然后是insertUserKeyPoints 表中的值的过程,但我的过程没有执行。

这是我的存储过程:

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.";
    }
}

当我执行此代码时,它会在没有insertupdate 的情况下运行该过程,并且在我的.aspx 页面上我得到Label1 的输出,即You are already confirmed.

谁能指导我这哪里出了问题?

【问题讨论】:

  • 您的 SP 存在一些并发问题。
  • 关于上述内容,您可以替换整个IF EXIST5 内容,然后无论如何都进行更新,然后使用OUTPUT 子句将所需的值直接插入UserKeyPoints。然后检查@@ROWCOUNT 以了解要返回的值。所以基本上存储的过程变成了两个语句,它解决了并发问题。
  • @MartinSmith 好的。这样我会好很多。谢谢。让我们研发这项技术。
  • @MartinSmith 因为我已经解决了我的问题,但我很高兴我找到了替代方法。从现在开始,我会尽量让我注意并发问题。

标签: c# asp.net sql-server stored-procedures


【解决方案1】:

我看到的第一个问题是缺少的 CommandType 设置为 StoredProcedure。
这是允许框架代码正确解释您的字符串的基础。

SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
cmd.CommandType = CommandType.StoredProcedure;

正如Martin Smith下面的评论中所解释的,调用失败的原因是没有正确设置CommandType参数没有传递给storedprocedure,但是过程本身是使用@的默认值执行的987654324@参数

然后,我将使用 ExecuteScalar 而不是使用 SqlDataAdapter 来编写对存储过程的调用,以仅返回数据表中的单行和单列

SqlCommand cmd = new SqlCommand("proc_getActivationCode1", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ActivationCode", Request.QueryString["token"].ToString());
object result = cmd.ExecuteScalar();
if(result != null)
{
    int resultValue = Convert.ToInt32(result);
    if (resultValue == 1)
        SendEmail objMail = new SendEmail();
    else
        Label1.Text = "You are already confirmed.";
}

【讨论】:

  • 是的。缺少的命令类型意味着存储的过程仍然被执行(由单个单词proc_getActivationCode1 组成的批处理将被视为过程调用)但参数永远不会传递给它。
  • 我确信这会导致对命令文本的误解,因此不会执行该过程,但我可能是错的,你有参考添加到答案吗?
  • 哦,这个网站太棒了,永远不会停止教授新东西并改变你未经检验的信念。你当然是对的,刚刚测试了 sp_help 提示,它也可以在没有设置 CommandType 的情况下工作。你不介意我用你的评论完成答案吗?
  • @Steve 我删除了存储过程中的IF EXIST 部分,并使用改进后的代码更新了我的代码。无需更新,因为 id 所做的一切都已在您的答案中。 :)
  • @Steve 还有一件事我也同意这个网站永远不会停止教授新东西。你总能从这里学到越来越多的东西!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
相关资源
最近更新 更多