存储过程未在表中插入值

分析毕加索

我已存储procedue其中第一update'S中的值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页面上,我将得到Label1ie的输出You are already confirmed.

谁能指导我我要怎么做呢?

史蒂夫

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

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

如下面马丁·史密斯Martin Smith)的评论所述,调用失败的原因是,如果没有正确设置CommandType,则不会将参数传递给存储过程,而是使用@ActivationCode参数的默认值来执行过程本身。

然后,我将使用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.";
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将存储过程的输出值插入表

用于插入的SQL Server存储过程会运行,但不会在表中插入值

使用触发器和存储过程中的值将值插入表中

存储过程在错误的列中插入值

如何从存储过程输入和临时表中将值插入表中

使用不带参数的存储过程将值插入表中

使用 oracle 后中断列值并使用存储过程插入到表中

使用存储过程将值列表插入到SQL Server表中

在同一存储过程中将值插入动态创建的表中

在同一存储过程中将值插入动态创建的表中

存储过程未在MVC5中返回值

存储过程插入联结表

使用存储过程插入表

Informix SQL /在表中插入存储过程的结果

将存储过程输出数据插入表的列中

将存储过程的结果以及额外的列插入表中

获取存储过程的refcursor输出并插入到临时表中

什么是将30个以上的参数值传递给存储过程以在表中插入值的更好方法?

T-SQL-存储插入到表中或更新表中记录的存储过程

使用过程 SQL SERVER 在表中插入值

SQL Server存储过程:遍历表,比较值,在值更改时插入到其他表

仅当先前值小于插入表 SQL Server 的新值时才开始存储过程

SQL语句/存储过程替换单个表中的NULL值

SQL使用存储过程在垂直表中合并值

如何访问在存储过程中声明的表的表列的值?

从存储过程插入临时表导致错误

我想分割逗号分隔的值,然后将每个值插入到mysql存储过程中的另一个表中

存储过程插入(选择和值)

SQL 2014 存储过程插入到表检查为空的表中