2016-03-15 161 views
0

我正在使用此代码来验证用户,但它始终显示'未找到用户'。是否在where子句中使用完整的select命令。答案每次都一样。虽然它总体上显示了一些结果,但我无法匹配一个结果。SQL Server不显示任何输出

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

你是否确定'select *'返回'username'作为第一列,'userpassword'作为第二列?没有可能是第一个用户ID或类似的?无论哪种方式,为了清楚起见,您应该不会按列号选择*和索引。 –

+0

只需进行调试,并在每次比较时输入一些日志消息,以便您可以知道查询结果是否正确。 – Tirma

回答

3

你应该使查询完整的,例如:

cmd.CommandText = "Select * from Applicant where UserName = @userName"; 

只有这样,加入(同名)的参数在查询中取代。