2012-11-26 32 views
0

我是MySQL數據庫的新手,我使用Visual Studio C#連接到我的數據庫。我有一個下面的選擇方法。我如何運行它來檢查它是否工作?如何測試以查看mySql數據庫是否正常工作?

編輯的打開和關閉連接方法

//Open connection to database 
    private bool OpenConnection() 
    { 
     try 
     { 
      // connection.open(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      //When handling errors, your application's response based 
      //on the error number. 
      //The two most common error numbers when connecting are as follows: 
      //0: Cannot connect to server. 
      //1045: Invalid user name and/or password. 
      switch (ex.Number) 
      { 
       case 0: 
        MessageBox.Show("Cannot connect to server."); 
        break; 

       case 1045: 
        MessageBox.Show("Invalid username/password, please try again"); 
        break; 
      } 
      return false; 
     } 
    } 

    //Close connection 
    private bool CloseConnection() 
    { 
     try 
     { 
      connection.Close(); 
      return true; 
     } 
     catch (MySqlException ex) 
     { 
      MessageBox.Show(ex.Message); 
      return false; 
     } 
    } 

選擇方法,該方法是在同一類作爲關閉和打開連接如上所示

public List<string>[] Select() 
    { 
     string query = "SELECT * FROM Questions"; 

     //Create a list to store the result 
     List<string>[] list = new List<string>[3]; 
     list[0] = new List<string>(); 
     list[1] = new List<string>(); 
     list[2] = new List<string>(); 
     list[3] = new List<string>(); 
     list[4] = new List<string>(); 
     list[5] = new List<string>(); 
     list[6] = new List<string>(); 
     list[7] = 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["id"] + ""); 
       list[1].Add(dataReader["difficulty"] + ""); 
       list[2].Add(dataReader["qustions"] + ""); 
       list[3].Add(dataReader["c_answer"] + ""); 
       list[4].Add(dataReader["choiceA"] + ""); 
       list[5].Add(dataReader["choiceB"] + ""); 
       list[6].Add(dataReader["choiceC"] + ""); 
       list[7].Add(dataReader["choiceD"] + ""); 
      } 

      //close Data Reader 
      dataReader.Close(); 

      //close Connection 
      this.CloseConnection(); 

      //return list to be displayed 
      return list; 
     } 
     else 
     { 
      return list; 
     } 
    } 

此方法是在一個單獨的已獲得所有數據庫連接設置的類。現在我想從我的主類調用這個方法來測試它是否工作,我該怎麼做?

回答

1

您應該創建該數據庫類的對象實例,然後調用Select()方法。
所以,假設這個DB類名爲QuestionsDB你應該寫這樣的事情:

QuestionDB questionDAL = new QuestionDB(); 
List<string>[] questions = questionDAL.Select(); 

然而,在此之前,請更正此行

List<string>[] list = new List<string>[8]; // you need 8 lists for your db query 

你可以檢查,如果您有任何記錄測試數組列表中的第一個列表是否具有多於零個元素。

if(questions[0].Count > 0) 
    ... // you have read records. 

不過,他說,我會改變你的代碼添加了對問題進行具體的類和使用(問題的)一個列表,而不是名單 所以陣列,例如,創建一個這樣

public class Question 
{ 
    public string ID; 
    public string Difficulty; 
    public string Question; 
    public string RightAnswer; 
    public string AnswerA; 
    public string AnswerB; 
    public string AnswerC; 
    public string AnswerD; 
} 

,改變你的選擇返回一個列表(問題的)

List<Question> list = new List<Question>; 
...... 
while (dataReader.Read()) 
{ 
     Question qst = new Question(); 
     qst.ID = dataReader["id"] + ""; 
     qst.Difficulty = dataReader["difficulty"] + ""; 
     qst.Question = dataReader["qustions"] + ""; 
     qst.RightAnswer = dataReader["c_answer"] + ""; 
     qst.AnswerA = dataReader["choiceA"] + ""; 
     qst.AnswerB = dataReader["choiceB"] + ""; 
     qst.AnswerC = dataReader["choiceC"] + ""; 
     qst.AnswerD = dataReader["choiceD"] + ""; 
     list.Add(qst); 
} 
return list; 
+0

嘿,感謝您的幫助。我做了一個按鈕來幫助我進行這個測試。當按下按鈕時,它將運行選擇方法。但我在MySqlDataReader dataReader = cmd.ExecuteReader();有錯誤。說連接必須有效並打開 – user1781232

+0

然後'this.OpenConnection'中的代碼沒有按預期工作。可能你應該顯示該方法的代碼,以瞭解發生了什麼。 – Steve

+0

請查看上面的代碼,其中包含openConnection的編輯版本。 – user1781232

0

您可以測試是否該方法的工作原理是編寫單元測試它。一個好的單元測試框架工作是Nunit。在你打電話給你之前,你必須創建並打開一個到DB的連接:

//Open connection 
    if (this.OpenConnection() == true) 
    { 

正如對方說的那樣,你會想要修正清單。

+0

我已經這樣做了,我在我的DBConn類中有此方法。但即使如此,當我從另一個類中調用select方法時,它說連接必須是有效的並且是打開的。任何想法,我哪裏錯了? – user1781232

相關問題