简体   繁体   中英

Get the players name and score

What I want to do is use a Data Table to get data from my mySQL to show in a Listbox on my form.

How can I get a players name and score to show up in order of highest score?

So far i managed to get a players name or a players score to show up.

MySqlConnection myConn = new MySqlConnection(connStr);

string sqlStr = "SELECT * FROM highscore";

MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);

DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DataSource = dTable;
lstScores.DataSource = dTable;
lstNames.DisplayMember = "Name";

I did try to use two separate list boxes out of ease, but realized i wouldn't be able to arrange them in any order.

Below is after the changes Paul

                 MySqlConnection myConn = new MySqlConnection(connStr);

             string sqlStr = "SELECT Name + ' ' + Score as NameAndScore " + "FROM highscore ORDER BY Score DESC";

             MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);

             DataTable dTable = new DataTable();
             dAdapter.Fill(dTable);
             dAdapter.Dispose();
             lstNames.DisplayMember = "NameAndScore";
             lstNames.DataSource = dTable;

You could do quite a bit with your SQL:

MySqlConnection myConn = new MySqlConnection(connStr);

string sqlStr = "SELECT CONCAT(Name, ' ', Score) as NameAndScore " +
                "FROM highscore ORDER BY Score DESC";

MySqlDataAdapter dAdapter = new MySqlDataAdapter(sqlStr, myConn);

DataTable dTable = new DataTable();
dAdapter.Fill(dTable);
dAdapter.Dispose();
lstNames.DisplayMember = "NameAndScore";
lstNames.DataSource = dTable;

Let's suppose that the field that holds the score is 'score', your query could be

string sqlStr = "SELECT * FROM highscore order by score DESC";

Basically You need to order your records in descending order by score. This should be simple

let me know if you have more issues

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM