在SQL数据库&QUOT错误,必须声明标量变量" [英] Error on sql database "Must declare the scalar variable"

查看:206
本文介绍了在SQL数据库&QUOT错误,必须声明标量变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的登录系统的问题。我做了一个简单的系统,该系统运行良好,但我想让它更先进,并根据用户的用户类型显示不同的起始页。不过,我遇到以下错误:必须声明标量变量@UserType

 使用系统;
使用System.Data这;
使用System.Data.SqlClient的;
使用System.Configuration;公共部分类LoginwithEncryption:System.Web.UI.Page
{    保护无效btnSubmit_Click(对象发件人,EventArgs的发送)
    {
        SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [构造]的ConnectionString);
        con.Open();
        CMD的SqlCommand =新的SqlCommand(
            SELECT * FROM dbo.UserInfo哪里登录= @登录和用户类型= @用户类型和密码= @密码和用户类型= @的UserType,CON);
        cmd.Parameters.AddWithValue(@登录,txtUserName.Text);
        cmd.Parameters.AddWithValue(@密码,txtPWD.Text +123);
        SqlDataAdapter的大=新SqlDataAdapter的(CMD);
        DataTable的DT =新的DataTable();
        da.Fill(DT);        如果(dt.Rows.Count大于0)
        {
            INT用户类型= Convert.ToInt32(dt.Rows [0] [的UserType]);
            如果(用户类型== 1)
            {
                的Response.Redirect(StartPage.aspx);
            }
            否则如果(用户类型== 2)
            {
                的Response.Redirect(DiferentStartPage.aspx);
            }        }
        其他
        {
            ClientScript.RegisterStartupScript(Page.GetType(),验证,
                < SCRIPT LANGUAGE =JavaScript的'>警报('无效的用户名和密码)LT; / SCRIPT>中);
        }    }
}


解决方案

由于错误状态:必须声明标量变量@UserType

在换句话说,SQL命令有 @UserType 参数,但没有在参数集合中声明的参数。

  CMD的SqlCommand =新的SqlCommand(
SELECT * FROM dbo.UserInfo哪里登录= @登录和用户类型= @用户类型和密码= @密码和用户类型= @的UserType,CON);
cmd.Parameters.AddWithValue(@登录,txtUserName.Text);
cmd.Parameters.AddWithValue(@密码,txtPWD.Text +123);

添加另一个参数为 @UserType 参数:

  cmd.Parameters.AddWithValue(@用户类型,用户类型);

此外,SQL包含重复引用为 @UserType

 从dbo.UserInfo选择*
其中,登录= @登录
      和用户类型= @用户等级和积分
      和密码= @密码
      和用户类型= @用户等级和积分

也许应该是:

 从dbo.UserInfo选择*
其中,登录= @登录
      和密码= @密码
      和用户类型= @用户等级和积分

I have a problem with my login system. I made a simple system which worked well, but I wanted to make it more advanced and display a different start page depending on a user's UserType. However, I'm encountering the following error: "Must declare the scalar variable @UserType."

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class LoginwithEncryption : System.Web.UI.Page
{

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand(
            "select * from dbo.UserInfo where Login =@Login and UserType=@UserType and Password=@Password and UserType=@UserType", con);
        cmd.Parameters.AddWithValue("@Login", txtUserName.Text);
        cmd.Parameters.AddWithValue("@Password", txtPWD.Text+".123");


        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            int usertype = Convert.ToInt32(dt.Rows[0]["UserType"]);


            if (usertype == 1)
            {
                Response.Redirect("StartPage.aspx");
            }
            else if (usertype == 2)
            {
                Response.Redirect("DiferentStartPage.aspx");
            }

        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation",
                "<script language='javascript'>alert('Invalid UserName and Password')</script>");
        }

    }
}

解决方案

As the error states: Must declare the scalar variable "@UserType"

In other words, the SQL command has a parameter for @UserType, but there are no parameters declared in the parameters collection.

SqlCommand cmd = new SqlCommand(
"select * from dbo.UserInfo where Login =@Login and UserType=@UserType and Password=@Password and UserType=@UserType", con);
cmd.Parameters.AddWithValue("@Login", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPWD.Text+".123");

Add another parameter for the @UserType parameter:

cmd.Parameters.AddWithValue("@UserType", "User Type");

Also, the SQL contains a duplicate reference to for @UserType:

select * from dbo.UserInfo 
where Login=@Login 
      and UserType=@UserType 
      and Password=@Password 
      and UserType=@UserType

Should probably be:

select * from dbo.UserInfo 
where Login=@Login 
      and Password=@Password
      and UserType=@UserType 

这篇关于在SQL数据库&QUOT错误,必须声明标量变量&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆