2016-09-29 108 views
0

因此,我爲我的公司創建了一個小型應用程序,讓用戶登錄並訪問特定數據,但是,在服務器端查詢數據庫,MySqlDataReader不讀取,正如我在下面的代碼:MySqlDataReader does not read

public string HandleLogin(string uname, string pwd) 
    { 
     MySqlDataReader READER; 
     int count = 0; 
     int clearance = 0; 
     if (uname == "" || pwd == "") 
      return ""; 
     CONN = new MySqlConnection(); 
     CONN.ConnectionString = Config.CONNSTRING; 
     string query = "select * from members where username='" + uname + "' and password='" + pwd + "'"; 
     try 
     { 
      CONN.Open(); 
      COMMAND = new MySqlCommand(query, CONN); 
      READER = COMMAND.ExecuteReader(); 
      while(READER.Read()) 
      { 
       count = count + 1; //<-- this should happen, but it doesnt 
       if (count == 1) 
        clearance = READER.GetInt32("clearance"); 
       else 
        return ""; 
      } 
      if (count == 1) 
       return Convert.ToString(clearance); 
      else 
       return ""; 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine(ex.Message); 
      return ""; 
     } 
     finally 
     { 
      CONN.Dispose(); 
     } 
    } 

我相信它應該讀1行,要求它發現行的量,它說0,但我三重檢查所有的價值觀和嚴絲合縫的數據庫值,但它讀取無查詢的行。這可能是一個非常愚蠢的錯誤,但我無法找到問題。

+0

打印並驗證sql文本是否正確。在uname/pwd變量中查找任何空格。 – RajN

+0

您是否檢查過連接是否打開? – Clock

+0

@RajNagalingam這是正確的,@ devlincarnate有,但密碼在數據庫中被散列,並在服務器收到它和@ SomeUser嗯謝謝,我一定會考慮它 –

回答

0

於是,我再一次放在一個斷點,並重新檢查變量,變量是很好的爲Console.WriteLine,但是,在查詢它看起來像這樣:

"select * from members where username='matthias' and password='M20UjefsltnFCTDsCL1temvFOtBcnN8gNtGbQevVTkkG8jCXZlbxQ3ykOTx0fV4Ls2VnAzOzx2F5CTY+jEDSzg==\0\0\0\0'" 

但它應該是: "select * from members where username='matthias' and password='M20UjefsltnFCTDsCL1temvFOtBcnN8gNtGbQevVTkkG8jCXZlbxQ3ykOTx0fV4Ls2VnAzOzx2F5CTY+jEDSzg=='"

由於客戶端和服務器之間的數據加密以及填充,它將\ 0 \ 0 \ 0 \ 0添加到最後,因爲那些想要幫助我的人,我應該早些看到這一點,我解決了它在查詢前添加此功能:

pwd = pwd.Replace("\0", "");