我正在使用此代码验证用户,但它始终显示“未找到用户”#39;。我是否使用带有where子句的完整select命令。答案每次都一样。虽然它总体上显示了一些结果,但我无法匹配一个结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Configuration;
public partial class Applicant : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection cn = new SqlConnection(myConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cn.Open();
SqlDataReader conReader = null;
cmd.CommandText = "Select * from Applicant ";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
// cmd.Parameters.AddWithValue("@userName", myid);
// cmd.Parameters.AddWithValue("@UserPassword", mypass);
try
{
conReader = cmd.ExecuteReader();
bool _userfound = false;
while (conReader.Read())
{
if (conReader[0].ToString() == myid.ToString() && conReader[1].ToString() == mypass.ToString())
{
_userfound = true;
break;
}
}
if (_userfound)
Response.Write("User Found");
else
Response.Write("User not Found");
}
catch (Exception ex)
{
Console.Write(ex);
}
finally
{
cn.Close();
}
}
}
}
}
答案 0 :(得分:3)
您应该完成查询,例如:
cmd.CommandText = "Select * from Applicant where UserName = @userName";
只有这样才能在查询中添加一个(同名的)参数。