我已存储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
页面上,我将得到Label1
ie的输出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] 删除。
我来说两句