2012-11-26 4 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

해당 DB 클래스의 개체 인스턴스를 만든 다음 Select() 메서드를 호출해야합니다.
그래서,이 같은 것을 작성해야 QuestionsDB이 DB 클래스의 이름은 것을 가정하면 :

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

그러나 이것을하기 전에,이 라인

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

어떤 기록이있는 경우가 확인할 수 있습니다 수정하시기 바랍니다 배열 목록의 첫 번째 목록에 0 개 이상의 요소가 있는지 테스트합니다.

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

안녕하세요. 도움에 감사드립니다. 이 테스트를 도와 줄 버튼을 만들었습니다. 버튼을 누르면 select 메소드가 실행됩니다. 하지만 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

관련 문제