c# - How to dynamically add lables for every name in MYSQL database? -
i have mysql database , trying first name , last name of every student put in database , dynamically show them label in wpf form, here got far
string connstr = "server=localhost; database=login; uid=root; pwd=password"; mysqlconnection connc = new mysqlconnection(connstr); mysqlcommand command; connc.open(); // label[] labels = new label[n]; try { command = connc.createcommand(); command.commandtext = "select first_name, last_name students"; command.executereader(); messagebox.show("s"); } catch (exception ex) { messagebox.show("something went wrong: " + ex.tostring()); } { connc.close(); }
so how add entry's in database label?
executenonquery
method executes query. can't values it.
you need use executereader
@ least values. can read column values in while statement mysqldatareader.read()
method. method reads query row row.
also use using
statement dispose mysqlconnection
, mysqlcommand
, mysqldatareader
.
using(mysqlconnection connc = new mysqlconnection(connstr)) using(mysqlcommand command = new mysqlcommand("select first_name, last_name students", connc)) { using(mysqldatareader reader = command.executereader()) { while (reader.read()) { // reader[0] gets first column first_name // reader[1] gets second column last_name // label assingments.. } } }
Comments
Post a Comment