c# - Rendering data from MySQL in a WinForms applications -
i have application i'm trying create, i'm stuck.
i trying access site's mysql query within code.
right trying test it, make sure works, i'm having no luck that.
i think i'm overlooking simple, i'm not sure. help?
class dbconnect { private mysqlconnection connection; private string server; private string database; private string uid; private string password; public list<string>[] list = new list<string>[1]; public dbconnect() { initialize(); } private void initialize() { server = "localhost"; database = "xxx"; uid = "xxx"; password = "xxx"; string connectionstring; connectionstring = "server=" + server + ";" + "database=" + database + ";" + "uid=" + uid + ";" + "password=" + password + ";"; connection = new mysqlconnection(connectionstring); } //this open connection mysql private bool openconnection() { try { connection.open(); return true; } //if there problem connecting, display 1 of following connections catch (mysqlexception ex) { switch (ex.number) { case 0: messagebox.show("cannot connect server. contact admin"); break; case 1045: messagebox.show("invalid username/password, please try again"); break; } return false; } } //if connection fails connect mysql server, client close connection. private bool closeconnection() { try { connection.close(); return true; } catch (mysqlexception ex) { messagebox.show(ex.message); return false; } } //select statement public list<string>[] select() { int firstnumber = 2058; **string query = "use `db_order` select `order_id` `order_address` order_id=2058";** //create list store results list[0] = new list<string>(0); //open's connection if (this.openconnection() == true) { //creates command query. mysqlcommand cmd = new mysqlcommand(query, connection); //creates data reader, , executes command. mysqldatareader datareader = cmd.executereader(); while (datareader.read()) { list[0].add(datareader["order_id"] + ""); } //close data reader datareader.close(); this.closeconnection(); //returns list displayed return list; } else { return list; } } }
here second class (i got rid of isn't part of problem):
public partial class form1 { private void initializecomponent() { // custname // this.custname.autosize = true; this.custname.location = new system.drawing.point(200, 30); this.custname.name = "custname"; this.custname.size = new system.drawing.size(137, 13); this.custname.tabindex = 0; this.custname.text = "customer\'s name goes here" + new dbconnect().list[0]; } #endregion }
update
you add list datareader
this
list[0].add(datareader["order_id"] + "");
so, means have multi-dimensional
(2-dimension exact) array of list. therefore access have select()[0][0]
, check below:
dbconnect() dbcon = new dbconnect(); this.custname.text = dbcon.select()[0].count>0 ? "customer\'s name goes here" +dbcon.select()[0][0].tostring() : "no customer show";
Comments
Post a Comment