2017-09-06 25 views
0

我是新来的c#和精简版。我编写了一个模拟登录过程的简单控制台应用程序。我试图用参数来使用dapper查询登录表。即使用户和密码是正确的,我仍然无法通过。我会感谢任何方向来完成成功的登录测试。用短小精灵查询登录表

enter image description here

public class Login 
    { 
     public string UserName { get; set; } 
     public string Password { get; set; } 

     public Login() 
     { 
     } 
     public Login(string username, string password) 
     { 
      UserName = username; 
      Password = password; 
     } 
    } 
    public static class Helper 
    { 
     public static string Con(string name) 
     { 
      return ConfigurationManager.ConnectionStrings[name].ConnectionString; 
     } 
    } 

    public class DataAccess 
    { 
     public List<Login> GetLogin(string username, string password) 
     { 
      using (IDbConnection connection = new SqlConnection(Helper.Con("Stock"))) 
      { 
       var output = connection.Query<Login>("sp_GetLogin @UserName, @Password", new { UserName = username, Password = password }, commandType:CommandType.Text).ToList(); 
       return output; 
      } 
     } 
    } 
    static void Main(string[] args) 
    { 

     string txtusername; 
     string txtpassword; 

     Console.WriteLine("Enter your Username:"); 
     txtusername = Console.ReadLine(); 
     Console.WriteLine("Enter your Password:"); 
     txtpassword = Console.ReadLine(); 
     Console.WriteLine("Username typed: {0}, Password typed: {1}", txtusername, txtpassword); 

     Login login = new Login(); 


     DataAccess obj = new DataAccess(); 

     obj.GetLogin(txtusername, txtpassword); 

     if (obj != null) 
     { 
      if (login.Password == txtpassword) 
      { 
       Console.WriteLine("User credentials successfull"); 
      } 
      else Console.WriteLine("Password don't match"); 
     } 
     else Console.WriteLine("Username don't match"); 
     Console.ReadLine(); 
    } 

回答

0
Login login = new Login(); 

    DataAccess obj = new DataAccess(); 

    obj.GetLogin(txtusername, txtpassword); 

哦lawd。

DataAccess obj = new DataAccess(); 

    Login login = obj.GetLogin(txtusername, txtpassword); 

你真的需要学习如何调试,通过你的代码的每一行步,检查发生了什么事情,为什么它不是你所期望的。 http://idownvotedbecau.se/nodebugging

+0

我做过调试。我得到它运行 – mauricio

0

您没有使用DataAccess.GetLogin的返回值。

var login = obj.GetLogin(txtusername, txtpassword).FirstOrDefault(); 

然后进行凭证检查。