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 7406925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:37:52+00:00 2026-05-29T05:37:52+00:00

I’m working on a basic mysql database class, with a couple of functions. One

  • 0

I’m working on a basic mysql database class, with a couple of functions. One of the functions should return a list of all rows and columns. The code is working, and it’s pretty dynamic, but it also looks like it could be improved. I was hoping someone could take a look at this code and tell me if there’s another solution to doing this?

One of the other solutions I tried was without lists, but it involved recreating an array for every row it had to return, so I dropped that and moved on to lists.

    public List<object[]> ReadAllRows(string sQuery)
    {
        List<object[]> oResults = new List<object[]>();

        try
        {
            MySqlCommand msCommand = new MySqlCommand
            {
                CommandText = sQuery,
                Connection = Connection
            };

            using (MySqlDataReader msReader = msCommand.ExecuteReader())
            {
                if(!msReader.HasRows)
                    return EmptyList;

                while (msReader.Read())
                {
                    object[] oRowValues = new object[msReader.FieldCount];
                    {
                        msReader.GetValues(oRowValues);
                    }

                    oResults.Add(new object[] { oRowValues });
                }
            }
        }
        catch (MySqlException ex)
        {
            LastError = "Error: " + ex;
            return EmptyList;
        }

        return oResults;
    }

If there are any questions, please feel free to ask

  • 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-05-29T05:37:53+00:00Added an answer on May 29, 2026 at 5:37 am

    I would highly recommend against doing this.

    First off, you should only keep a connection open as long as it takes to execute the command. Also, MySqlCommand implements IDisposable, so that should be wrapped in a using clause.

    One reason I recommend against it is that this functionality is already covered. A simple way is to have your methods just return a DataTable. DataTables are going to give you every bit of information you’ll need anyway (data type, rows, columns, etc) while yours is very limiting.

    Another reason has to do with maintaining an open connection: bad idea. Connection Pooling will take care of opening/closing connections very very quickly so it’s just not necessary. Also, in case your app crashes then you are going to be leaking connections. At some point you’ll end up with wonderful little database errors.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I am reading a book about Javascript and jQuery and using one of the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.