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

  • SEARCH
  • Home
  • 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 9031187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:40:06+00:00 2026-06-16T07:40:06+00:00

I have an interface that pulls data from a MySQL database and displays it

  • 0

I have an interface that pulls data from a MySQL database and displays it in a console window. The code used to display the information is cycled using a while loop.

The issue I’m having is that the MySQL connections open and close on each loop and this causes a delay in displaying the information. I have tried opening a connection before the loop and closing it once the loop exits but I get an exemption telling me the connection need to be valid and open.

So the loop adds data from the database into the datalist List and then gets displayed in the console. Each loop also clears the list as the information on the server can change due to updates.

The other thing I’d like to move away from would be the Thread.Sleep() as it hangs the program (as designed I know) but I don’t know of other solutions.

Here is my code;

public void Loop()
    {
        public static string loopquery;
        public static List<DataList> datalist;

        var cki = new ConsoleKeyInfo();

        do
        {
            DateTime time = DateTime.Now.AddSeconds(1);

            while ((DateTime.Now < time))
            {
                Console.Clear();
                Travel.data.Clear();

                loopquery = "SELECT * FROM database)";
                Travel();

                Console.WriteLine("Hurray it's a loop!");
                Console.WriteLine(datalist[0].id);
                Console.WriteLine(datalist[0].name);

                if (Console.KeyAvailable)
                {
                    // Waits until the user presses a key, and puts it into our object key.
                    cki = Console.ReadKey(true);

                    if (cki.Key == ConsoleKey.UpArrow)
                    {
                        // Do Something
                    }
                    if (cki.Key == ConsoleKey.DownArrow)
                    {
                        // Do Something
                    }
                    if (cki.Key == ConsoleKey.LeftArrow)
                    {
                        // Do Something
                    }
                    if (cki.Key == ConsoleKey.RightArrow)
                    {
                        // Do Something
                    }
                    if (cki.Key == ConsoleKey.Escape)
                    {
                        break;
                    }
                }
                else
                {
                    Thread.Sleep(1000);
                }

            }
        } while (cki.Key != ConsoleKey.Escape);
    }

public void Travel()
    {
        //Open a connection
        databaseCon.Open();

        //Create Command
        var cmd = new MySqlCommand(Loop.loopquery, databaseCon);

        //Create a data reader and Execute the command
        var dataReader = cmd.ExecuteReader();

        try
        {
            //Read the data and store them in the list
            while (dataReader.Read())
            {
                 var data= new DataList();
                    data.id = (dataReader.GetInt32(0));
                    data.name = (dataReader.GetString(1));

                    Loop.datalist.Add(data);
            }
        }
        catch (MySqlException ex)
        {
            Console.WriteLine("Error: {0}", ex);
            Console.ReadLine();
        }
        finally
        {
            if (dataReader != null)
            {
                //close Data Reader
                dataReader.Close();
            }

            if (databaseCon != null)
            {
                //close Connection
                databaseCon.Close();
            }
        }
    }

Is there a more efficient way to do this? I don’t want to hammer the server with hundreds of queries as this client will be used by a number of people.

  • 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-16T07:40:07+00:00Added an answer on June 16, 2026 at 7:40 am

    As you pointed out, it might not be efficient for many clients to do hundreds of queries directly against the database in a loop.

    There isn’t specifics on your app, but one way to avoid a bunch of clients hammering the SQL server in a loop would be to add an application tier between the clients and the database server. You could open an API endpoint (REST etc…) for the clients to query. The application tier server could periodically query and hold the data in memory for all the clients to query.

    That of course only makes sense if the many clients are accessing the same data to share or a cacheable set of data is feasible to hold in memory. It also depends on how latent that data can be to determine when you have to evict data from the cache or update.

    Yet another option is to create two way communications with something like a socket server where the server calls back to the client when data changes.

    If an application tier is also used for writes, then there’s other options to invalidate data in the cache on change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that pulls data from the web, parses them, and compiles
We have an old Access 2000 database that we need to pull data from
I have an IRepository interface that inherits from IRepository<TObject> . I also have a
I have an app that needs to pull data from a server and insert
I'm trying to write a script to sequentially pull data from printers that have
I have a .NET 3.5 C# library that uses OleDb to pull data from
Assume I have some program that pulls airline data. Each airline data uses a
I have an interface that has a generic method with two type parameters. I
I have an interface that has large numbers of controls, see image below. Interface
I have an interface that defines the ability to persist an entity to a

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.