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

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

此方法位于一个单独的类中,该类已获取所有数据库连接设置。现在我想从我的主类中调用这个方法来测试它是否有效,我该怎么做?

最佳答案

您应该创建该数据库类的对象实例,然后调用 Select() 方法。
因此,假设该数据库类名为 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;

关于c# - 如何测试mySql数据库是否正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13574910/

相关文章:

c# - 处理WhenAll时,可以优雅地处理异常

c# - Objective-C 中是否有与 C# 的 yield 关键字相似的地方

PHP和MYSQL支持版本升级

mysql - 从 sample.sql 文件导入的数据的日志文件

c# - Azure C# KeyVaultErrorException : Operation returned an invalid status code 'Forbidden'

c# - 使用私有(private)自定义字段 (_customfieldname) 创建 WordPress 帖子不起作用

mysql - 在 MySQL WorkBench 中打开现有数据库

php - 如何为分页器排序顺序使用函数表达式?

mysql - 将数据库从本地主机更改为外部主机

mysql - Laravel 5.1,Eloquent,ManyToMany 和评论