[英]How can I get the content of 2 columns and show this?
public void ShowFirstFiveHighScore()
{
string query = "SELECT 'key', 'PlayerName', 'HighScore' FROM PlayerPoints ORDER BY HighScore DESC LIMIT 5";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, _connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(reader.GetString(...);
}
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
我嘗試顯示“ PlayerName”和“ HighScore”列。 也許在MessageBox中? 有幫助嗎? 謝謝。
首先,在查詢的SELECT
部分中調用列名時,不需要使用單引號( '
)。 您需要將其與字符值一起使用。
第二, MySqlDataReader.GetString
方法將int
作為參數,它是從零開始的列號。 這意味着您可以使用1
和2
指定列,並根據需要顯示它們。
KEY
是MySQL中的保留字 。 您需要用`
字符引用。 但是 ,最好的解決方案是將名稱更改為非保留字。
您的ExecuteNonQuery
與SELECT
語句ExecuteNonQuery
。 它什么也沒做。
您需要使用using
語句來處置MySqlCommand
和MySqlDataReader
(在編寫時也為MySqlConnection
)。
string query = "SELECT `key`, PlayerName, HighScore FROM PlayerPoints ORDER BY HighScore DESC LIMIT 5";
using(MySqlCommand cmd = new MySqlCommand(query, _connection))
using(MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
MessageBox.Show(string.Format("PlayerName: {0} HighScore: {1}",
reader.GetString(1),
reader.GetString(2)));
}
}
謝謝@Soner。 有用。 如何在MessageBox中顯示所有五個結果? 不在五個不同的MessageBox中?
然后,你可以連接你5
在你的while語句中一個字符串行,並顯示它while語句之外。 喜歡;
string s = "";
while (reader.Read())
{
s += string.Format("PlayerName: {0} HighScore: {1}\n",
reader.GetString(1),
reader.GetString(2)));
}
MessageBox.Show(s);
public void ShowFirstFiveHighScore()
{
string query = "SELECT 'key', PlayerName, HighScore FROM PlayerPoints ORDER BY HighScore DESC LIMIT 5";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, _connection);
MySqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show(string.Format("{0}-{1}"),reader.GetString(1),
reader.GetString(2));
}
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.