我想做的是使用一个数据表从我的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;
最佳答案
你可以用你的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;
关于c# - 获取球员姓名和得分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15420443/