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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:37:49+00:00 2026-06-09T15:37:49+00:00

I’m learning on how to access MySQL database using C# by following this guide

  • 0

I’m learning on how to access MySQL database using C#
by following this guide

In those codes, it uses List<string>[4] to store the Selected data because it has 4 columns

After some experiment, I found out that those codes store the data in a format like this:

//List[0] = Name1, Name2, Name3, ...

//List[1] = Age1, Age2, Age3, ...

//...

I want to access the data row by row and display it in ListView,

but if we use foreach, it will list the data column by column

foreach (List<string> entry in dataEntries){
    ListViewItem lvi = new ListViewItem(entry[0]); //refer to Name1 during first loop

    lvi.SubItems.Add(entry[1]); //Name2
    lvi.SubItems.Add(entry[2]); //Name3
    lvi.SubItems.Add(entry[3]); //and so on
    listView1.Items.Add(lvi);
}

Is there a way to display it row by row? Or is there any simpler workaround?

  • 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-09T15:37:51+00:00Added an answer on June 9, 2026 at 3:37 pm

    This part of the tutorial:

    public List< string >[] Select()
    {
        string query = "SELECT * FROM tableinfo";
    
        //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 >();
    
        //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["name"] + "");
                list[2].Add(dataReader["age"] + "");
            }
    
            //close Data Reader
            dataReader.Close();
    
            //close Connection
            this.CloseConnection();
    
            //return list to be displayed
            return list;
        }
        else
        {
            return list;
        }
    }
    

    is what you’re referring to, I suppose. It’s not a good way to store the results, and it’s rather poorly-written to boot. You could just as easily do:

    public List<object[]> Select() {
        string query = "SELECT * FROM tableinfo";
    
        // Create a list to store the result
        var result = new List<object[]>();
    
        // Open connection
        if (this.OpenConnection()) {
            // Create Command
            var 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()) {
                object[] items = new object[dataReader.FieldCount];
                dataReader.GetValues(items);
                result.Add(items);
            }
    
            // Close Data Reader
            dataReader.Close();
    
            // Close Connection
            this.CloseConnection();
    
            // Return list to be displayed
            return list;
        } else {
            // This is a bad thing.
            throw new ApplicationException("Could not connect to database.");
        }
    }
    

    And if I recall database access correctly, this is how you’re supposed to do it:

    public DataSet Select() {
        if(!this.OpenConnection()) {
            throw new ApplicationException("Could not connect to database.");
        }
    
        using(var query = new MySqlDataAdapter("SELECT * FROM tableinfo", this.connection)) {
            var data = new DataSet();
    
            query.Fill(data);
    
            return data;
        }
    }
    
    • 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
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,

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.