c#
  • mysql
  • login
  • phpmyadmin
  • 2013-03-14 116 views 1 likes 
    1

    我有這樣一個樣表:驗證登錄與phpMyAdmin

    image

    我創建dbconnect類選法,它是這樣的:

    public List<string>[] Select(string username, string password) 
    { 
        string query = "SELECT * FROM ms_user where username = '" + username + 
         "' and password = '" + password + "'"; 
    
        //Create a list to store the result 
        List<string>[] list = new List<string>[2]; 
        list[0] = new List<string>(); 
        list[1] = new List<string>();   
    
        //Open connection 
        if (this.OpenConnection() == true) 
        { 
         //Create Command 
         MySqlCommand cmd = new MySqlCommand(query, connection); 
         //Create a data reader and Execute the command 
         MySqlDataReader dataReader = cmd.ExecuteReader(); 
    
         //Read the data and store them in the list 
         while (dataReader.Read()) 
         { 
          list[0].Add(dataReader["username"] + ""); 
          list[1].Add(dataReader["password"] + "");      
         } 
    
         //close Data Reader 
         dataReader.Close(); 
    
         //close Connection 
         this.CloseConnection(); 
    
         //return list to be displayed 
         return list; 
        } 
        else 
        { 
         return list; 
        } 
    } 
    

    如何使用這個方法登錄?由於該方法正在返回一個列表,而不是truefalse以檢查數據庫中是否存在該值。

    +0

    沒有明顯的研究努力,投票關閉 – 2013-03-14 08:21:50

    +0

    你是說你需要返回列表和真或假嗎? (可以使用'out'參數) 你的代碼可以通過在if語句之外移動列表的方式進行改進,那麼你不需要else – Sayse 2013-03-14 08:22:34

    +0

    ,因爲我不知道如何處理返回的列表:( – Cignitor 2013-03-14 08:22:35

    回答

    0

    我會在閱讀器中添加一個變量,只要找到並匹配用戶帳戶,該變量就會增加。

    x int = 0; 
        if (this.OpenConnection() == true) 
        { 
         //Create Command 
         MySqlCommand cmd = new MySqlCommand(query, connection); 
         //Create a data reader and Execute the command 
         MySqlDataReader dataReader = cmd.ExecuteReader(); 
    
         //Read the data and store them in the list 
         while (dataReader.Read()) 
         { 
          list[0].Add(dataReader["username"] + ""); 
          list[1].Add(dataReader["password"] + ""); 
          x++; 
         } 
    
         //close Data Reader 
         dataReader.Close(); 
    
         //close Connection 
         this.CloseConnection(); 
    
         //return list to be displayed 
         return x; 
        } 
    

    然後,使用了程序中的控制結構,其檢查,如果x> 0,如果它是,登錄用戶。

    +0

    如果他返回一個列表,他已經擁有列表的大小 – Sayse 2013-03-14 08:25:51

    +0

    它表示'不能將int類型隱式轉換爲系統集合通用列表字符串' – Cignitor 2013-03-14 08:29:37

    0

    哇,看起來你需要學習構建查詢和sql注入。

    還有密碼,散列,加密和鹽。

    然後例外。

    然後刪除不符合類型化語言的(+「」),並且大多數情況都是不好的形式。

    Then == true test is bad style,redundant。

    1
    Boolean loginSuccessful = Select(username, password).Count > 0; 
    

    但是,請看看如何存儲密碼的數據庫資源(例如,this one)和SQL注入(例如,this one)。

    0

    我認爲,而不是直接回答你的問題(因爲它是微不足道的 - 你只是返回null而不是List如果用戶沒有找到),你會更好地獲得一些資源更多的閱讀材料。

    我認爲,如果你讀了一些關於SQL注入,你會特別受益: http://www.unixwiz.net/techtips/sql-injection.html

    是讀下面的教程後 - 這是相當不錯: http://zetcode.com/db/mysqlcsharptutorial/

    最後,我建議至少要等到當你將值傳遞給你的SQL時,你會得到一些經驗,你總是堅持使用MySqlParameter。看看這個問題StackOverflow - >Parameterized Query for MySQL with C#

    相關問題