0

I am new to MySQL database, I am using Visual Studio C# to connect to my database. I have got a following select method. How can I run it to check if it is working?

EDITED The open and close connection methods

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

Select method which is in the same class as the close and open connection as shown above

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

This method is in a separate class which has got all the database connection settings. Now that I want to call this method from my main class to test it to see if it's working, how can I do this?

4

2 回答 2

1

You should create an object instance of that DB class and then call the Select() method.
So, supposing that this DB class is named QuestionsDB you should write something like this:

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

However, before this, please correct this line

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

You could check if you have any record testing if the first list in your array list has more than zero elements.

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

However, said that, I will change your code adding a specific class for questions and using a list(of Question) instead of an array of list So, for example, create a class like this

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

and change your select to return a List(of Question)

 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;
于 2012-11-26T23:41:43.230 回答
0

You can test whether the method works by writing a unit test for it. A good unit testing frame work is Nunit. Before you call this you must create and open a connection to the DB:

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

as the other person said, you will want to fix the lists up.

于 2012-11-26T23:47:09.267 回答