C# sql数据库上的错误";必须声明标量变量";

C# sql数据库上的错误";必须声明标量变量";,c#,asp.net,sql,C#,Asp.net,Sql,我的登录系统有问题。我制作了一个简单的系统,它工作得很好,但我想让它更高级,并根据用户的UserType显示不同的起始页。但是,我遇到了以下错误:“必须声明标量变量@UserType。” 使用系统; 使用系统数据; 使用System.Data.SqlClient; 使用系统配置; 使用加密的公共分部类登录:System.Web.UI.Page { 受保护的void btnsupmit\u单击(对象发送者,事件参数e) { SqlConnection con=新的SqlConnection(Con

我的登录系统有问题。我制作了一个简单的系统,它工作得很好,但我想让它更高级,并根据用户的
UserType
显示不同的起始页。但是,我遇到了以下错误:“必须声明标量变量@UserType。”

使用系统;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
使用加密的公共分部类登录:System.Web.UI.Page
{
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“conStr”].ConnectionString);
con.Open();
SqlCommand cmd=新的SqlCommand(
“从dbo.UserInfo中选择*,其中Login=@Login和UserType=@UserType和Password=@Password和UserType=@UserType”,con);
cmd.Parameters.AddWithValue(“@Login”,txtUserName.Text);
cmd.Parameters.AddWithValue(“@Password”,txtPWD.Text+”.123”);
SqlDataAdapter da=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
da.填充(dt);
如果(dt.Rows.Count>0)
{
int usertype=Convert.ToInt32(dt.Rows[0][“usertype”]);
if(usertype==1)
{
响应重定向(“StartPage.aspx”);
}
else if(usertype==2)
{
重定向(“differentstartpage.aspx”);
}
}
其他的
{
ClientScript.RegisterStartupScript(Page.GetType(),“验证”,
“警报(‘无效用户名和密码’)”;
}
}
}

您已将@Login和@Password作为参数传入查询,但尚未将@UserType作为参数传入查询。

错误状态:必须声明标量变量“@UserType”

换句话说,SQL命令有一个用于
@UserType
的参数,但参数集合中没有声明任何参数

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");
@UserType
参数添加另一个参数:

cmd.Parameters.AddWithValue("@UserType", "User Type");
此外,SQL还包含对for
@UserType
的重复引用:

select * from dbo.UserInfo 
where Login=@Login 
      and UserType=@UserType 
      and Password=@Password 
      and UserType=@UserType
应该是:

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

你的意思是说
和UserType=@UserType
两次吗?为什么不将cmd.Parameters.AddWithValue(“@UserType”…?奇怪的是,我确实将cmd.Parameters.AddWithValue(“@UserType”,“User Type”);添加到了我的程序中,但登录确实停止了。(删除UserType使登录再次工作)你知道可能有什么问题吗?你必须为
@UserType
提供一个实际值;在这个例子中,
“User Type”
应该替换为value.cmd.Parameters.AddWithValue(“@UserType”,SqlDbType.Int);这是正确的方法吗?在重读您的问题时,您可能不想使用
@UserType
参数;相反,您正试图使用
UserType
根据登录进行重定向。如果是这样,那么只需从SQL命令中删除
@UserType
,而不添加任何其他参数。太好了!如果您这样做了倾向于,请随意投票并接受这个答案:)
select * from dbo.UserInfo 
where Login=@Login 
      and Password=@Password
      and UserType=@UserType