您好我正在运行两种身份验证形式。我运行的第一个身份验证是一个AD身份验证工作正常。第二个是确认用户名在SQL数据库中。我正在使用此代码来验证SQL中是否存在用户名。
我收到错误
运算符'=='不能应用于'string'和'System.Data.SqlClient.SqlDataReader'类型的操作数
使用此代码......
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("select Username from dbo.users", con);
con.Open();
SqlDataReader sqlresult = cmd.ExecuteReader();
if (txtLoginID.Text == sqlresult)
{
Response.Redirect("account/orders.aspx");// Authenticated user redirects to default.aspx
}
else
{
Response.Redirect("default.aspx");// Authenticated user redirects to default.aspx
}
con.Close();
}
}
答案 0 :(得分:0)
这里有几个问题。首先关闭您的查询:
select Username from dbo.users
拉出表中的每条记录。这不是检查用户名是否存在的正确方法。你需要这样的东西:
select Username from dbo.users WHERE UserName = @UserName
其中@UserName
是parameter,其中包含txtLoginID.Text
的值。完成此操作后,您可以使用:
if(sqlresult.HasRows)
{
Response.Redirect("account/orders.aspx");// Authenticated user redirects to default.aspx
}
else
{
Response.Redirect("default.aspx");// Authenticated user redirects to default.aspx
}
您还应该查看using
语句。
如果您使用上述解决方案并不重要,那么您获得错误的原因是非常自我解释的。您正在尝试将字符串值与SqlDataReader对象进行比较。这是不可能的。