SQL命令不会重定向

本文关键字:重定向 命令 SQL | 更新日期: 2023-09-27 18:15:50

我创建了这段代码,但没有按照它应该的方式运行。

应该发生的是,当查询在数据库中返回匹配的值时,它应该重定向页面。但是无论我做什么,它总是返回值为false,并且永远不会正确地重定向。

至少我是这么想的,任何帮助都太好了。

我已经为这个工作了3天了,我快要疯了。如果有更好的方法,我洗耳恭听。

var SuserId = User.Identity.GetUserId();
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = "select Count(ID) from AspNetStripeUsers where ID=@ID";
cmd.Parameters.AddWithValue("@ID", SuserId);
int result = (int)cmd.ExecuteScalar();
string wewe = result.ToString();
if (wewe == SuserId)
{
    Response.Redirect("~/Services/MS/OT/igive");
}

SQL命令不会重定向

int result = (int)cmd.ExecuteScalar();它总是返回一个int型值与userID不同因为它会从数据库中检索第一个值所以检查它是否有值这意味着条件为真

所以上面Nayan给出的代码是正确的

if (result>0)
{
    Response.Redirect("~/Services/MS/OT/igive");
}
else
{
Console.Writeline("The user id is not valid";
}

首先比较UserId和UserId的计数,这可能会产生问题。

int result = (int)cmd.ExecuteScalar();
if (result>0)
{
    Response.Redirect("~/Services/MS/OT/igive");
}
else
{
//User not registered.
}
int result = (int)cmd.ExecuteScalar();
if (result>0)
{
    Response.Redirect("");
}