如何测试mySql数据库是否正常工作?

时间:2012-11-26 23:22:48

标签: c# mysql database visual-studio-2010

我是MySQL数据库的新手,我使用Visual Studio C#连接到我的数据库。我有一个以下的选择方法。如何运行它来检查它是否正常工作?

已编辑打开和关闭连接方法

//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;
        }
    }

此方法位于一个单独的类中,该类具有所有数据库连接设置。既然我想从我的主类中调用这个方法来测试它是否有效,我该怎么办呢?

2 个答案:

答案 0 :(得分:1)

您应该创建该DB类的对象实例,然后调用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;

答案 1 :(得分:0)

您可以通过为其编写单元测试来测试该方法是否有效。一个好的单元测试框架工作是Nunit。在调用此方法之前,您必须创建并打开与DB的连接:

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

正如另一个人所说,你会想要修复这些清单。