2016-08-02 74 views
-4

我想檢查一個用戶名與給定的密碼是否存在於我的數據庫中,如果存在,它應該返回true否則它應該返回false。C#不是所有的代碼路徑返回值

我現在的功能如下所示:

public bool user_check(string username, string password) 
{ 
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

    if (this.OpenConnection() == true) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, connection); 
     MySqlDataReader dataReader = cmd.ExecuteReader(); 
     if (dataReader.HasRows) 
     { 
      while (dataReader.Read()) 
      { 
       return true; 
      } 
     } 
     else 
     { 
      return false; 
     } 
     dataReader.Close(); 
     this.CloseConnection(); 
    } 
} 

但我收到以下錯誤信息:

錯誤CS0161 'database_connector.user_check(字符串,字符串)':不是所有的 代碼路徑返回值

我在做什麼錯?

+1

如果什麼獲取返回'OpenConnection'不返回TRUE;,例如? –

+0

返回時,它下面的任何內容都會被忽略。所以你也會得到一個錯誤,說在最後兩行代碼不能到達 – spatbord

回答

3

只需在最後加上return false;即可。你也不要,如果

public bool user_check(string username, string password) 
{ 
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

    if (this.OpenConnection()) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, connection); 
     MySqlDataReader dataReader = cmd.ExecuteReader(); 
     if (dataReader.HasRows) 
     { 
      while (dataReader.Read()) 
      { 
       return true; 
      } 
     } 
     dataReader.Close(); 
     this.CloseConnection(); 
    } 

    return false; 
} 

也看到,如果你有記錄,然後返回true永不關閉的連接和對象需要在內部的else return false。也許這樣做,而不是:

public bool user_check(string username, string password) 
{ 
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

    bool hasRecords = false; 

    if (this.OpenConnection()) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, connection); 
     MySqlDataReader dataReader = cmd.ExecuteReader(); 
     if (dataReader.HasRows) 
     { 
      while (dataReader.Read()) 
      { 
       hasRecords = true; 
       break; 
      } 
     } 
     dataReader.Close(); 
     this.CloseConnection(); 
    } 
    return hasRecords; 
} 

最後一件事:看看Parameterized Queires避免SQL注入

2

就在該方法的最後添加return false:

public bool user_check(string username, string password) 
{ 
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

    if (this.OpenConnection() == true) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, connection); 
     MySqlDataReader dataReader = cmd.ExecuteReader(); 
     if (dataReader.HasRows) 
     { 
      while (dataReader.Read()) 
      { 
       return true; 
      } 
     } 
     else 
     { 
      return false; 
     } 
     dataReader.Close(); 
     this.CloseConnection(); 
    } 
    return false; //<<---- This is where it does not know what to do if any above conditions fail. 
} 
1

如果此檢查返回false:

if (this.OpenConnection() == true) 

你退出,不返回任何東西。

1

Error CS0161發生時,指定其簽名返回類型的函數包含通過函數不返回的路徑值。在你的情況下,當this.OpenConnection()方法返回false時,函數不返回值。

爲了防止由編譯器所報告這個錯誤,所有的路徑應返回值:

public bool user_check(string username, string password) 
{ 
    string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

    if (this.OpenConnection() == true) 
    { 
     MySqlCommand cmd = new MySqlCommand(query, connection); 
     MySqlDataReader dataReader = cmd.ExecuteReader(); 
     if (dataReader.HasRows) 
     { 
      while (dataReader.Read()) 
      { 
       return true; 
      } 
     } 
     else 
     { 
      return false; 
     } 
     dataReader.Close(); 
     this.CloseConnection(); 
    } 
    return false; 
} 

我藉此機會讓你瞭解SQL注入

你的代碼是脆弱的到SQL注入,因爲您將用戶輸入連接到查詢中。我們鼓勵你使用參數化查詢,以瞭解更多信息有關此主題退房this link

+1

你的代碼仍然會得到異常..你忘記了它不會進入'OpenConnection == true'的情況,如果 –

+0

@ GiladGreen我打算在返回false的地方輸入我的答案,但忘記將其添加到代碼中。感謝您的編輯:) – Wazner

0

那是因爲你寫你的returnifwhile塊only.Besides我建議你使用參數化查詢

public bool user_check(string username, string password){ 
    string query = "SELECT username, password From swear_tool Where "+ 
        "[email protected] and [email protected]"; 
    if (this.OpenConnection() == true){ 
     using(MySqlCommand cmd = new MySqlCommand(query, connection)){ 
      cmd.Parameters.AddWithValue("@uname",usename); 
      cmd.Parameters.AddWithValue("@password",password); 
      using(MySqlDataReader dataReader = cmd.ExecuteReader()){ 
       if (dataReader.HasRows){ 
        while(dataReader.Read()){ 
         return true; 
        } 
       } 
      } 
     } 
     this.CloseConnection();      
    } 
    return false; 
} 

尼斯和清潔和安全和更短

0

當用戶存在時,上一個答案不會關閉連接。 試試這個:

public bool user_check(string username, string password) 
    { 
     string query = "SELECT username, password from swear_tool where username='" + username + "' and password = '" + password + "'"; 

     if (this.OpenConnection()) 
     { 
      try 
      { 
       using (MySqlCommand cmd = new MySqlCommand(query, connection)) 
       { 
        using (MySqlDataReader dataReader = cmd.ExecuteReader()) 
        { 
         if (dataReader.HasRows) 
         { 
          while (dataReader.Read()) 
          { 
           return true; 
          } 
         } 
        } 
       } 

      } 
      finally 
      { 
       this.CloseConnection(); 
      } 
     } 
     return false; 
    } 
相關問題