我想要做的是使用数据表从我的mySQL
获取数据,以便在我的表单上的Listbox
中显示。
如何以最高分的顺序显示球员姓名和分数?
到目前为止,我设法得到球员名字或球员得分。
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";
我确实试图轻松使用两个单独的列表框,但意识到我无法按任何顺序排列它们。
以下是改变后的保罗
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;
答案 0 :(得分:1)
你可以用你的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;
答案 1 :(得分:0)
我们假设持有得分的字段是“得分”,您的查询可能是
string sqlStr =“SELECT * FROM按分数DESC分数高分数”;
基本上您需要按分数降序排列记录。这应该很简单
如果您有更多问题,请告诉我