c# - How can I get the content of 2 columns and show this? -
public void showfirstfivehighscore() { string query = "select 'key', 'playername', 'highscore' playerpoints order 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(); } }
i try show columns "playername" , "highscore". maybe in messagebox? help? thanks.
first of all, don't need use single quotes ('
) column names when call them in select
part in query. need use character values.
second of all, mysqldatareader.getstring
method takes int
parameter zero-based column number. means can specify columns 1
, 2
, show them want.
key
reserved word in mysql. need quote `
character. however, best solution change name nonreserved word.
your executenonquery
meaningless select
statement. doesn't anything @ all.
you need use using
statement dispose mysqlcommand
, mysqldatareader
(also mysqlconnection
when write).
string query = "select `key`, playername, highscore playerpoints order 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))); } }
thank @soner. works. how can show 5 results in messagebox? not in 5 different messageboxes?
then can concatenate 5
rows in 1 string inside while statement , show outside of while statement. like;
string s = ""; while (reader.read()) { s += string.format("playername: {0} highscore: {1}\n", reader.getstring(1), reader.getstring(2))); } messagebox.show(s);
Comments
Post a Comment