Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8890993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:37:35+00:00 2026-06-14T22:37:35+00:00

I am new to MySQL database, I am using Visual Studio C# to connect

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:37:37+00:00Added an answer on June 14, 2026 at 10:37 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Visual Studio 2008 C# .NET 3.5 project using MySql 5.1.53 and
I'm new to creating applications using Visual Studio 2010 and MySQL. I am creating
I'm trying to add a row into a mysql database table using visual studio
Can we create a new MySQL database( not a table, I want a new
I am new to MySql database. I've large table(ID,...). I select ID frequently with
I am new in creating application using Visual Studio 2010 and Microsoft Access 2007.
I am using the latest version of MySQL connector to .NET and Visual Studio
just got a slight problem here with updating a MySql DataGridView from visual studio
I am creating a database application using Visual C# Express and MySQL. The issue
In Short: I'm using VB.NET 2008 to connect to a Visual Foxpro 6 Database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.