C# MySql DataTable fill in Array -


i got problem.

i want write programm insert stuff mysql database. in first time make every column in database own string , used make sql query. in tables got more 100 columns. , dont want make 100 variables. decided make datatable , array. dont know how fill names of columns array. here part of code that.

        private void button_npccreate_click(object sender, eventargs e)     {         //umwandlung von c# variable in mysql variable         mysqlcommand command = new mysqlcommand();         command.parameters.addwithvalue("@server", server);         command.parameters.addwithvalue("@port", port);         command.parameters.addwithvalue("@user", user);         command.parameters.addwithvalue("@password", mysqlpassword);         command.parameters.addwithvalue("@world", db_world);           //connection zu mysql server         string myconnectionstring = "server=" + server + ";port=" + port + ";database=" + db_world + ";uid=" + user + ";pwd=" + mysqlpassword + ";";         mysqlconnection connection = new mysqlconnection(myconnectionstring);         mysqlcommand cmd;         cmd = connection.createcommand();         string readquery = "select * quest_template;";         mysqldataadapter read_adapter = new mysqldataadapter(readquery, connection);         datatable tableread = new datatable();         connection.open();         read_adapter.fill(tableread);         connection.close();         int columns = 0;           if(tableread.rows.count > 0)         {         foreach(datacolumn dc in tableread.columns)         {             columns++;         }         }         else         {             messagebox.show("the table empty. please check database.");         }          string [] columns_array = new string[columns];         foreach (datacolumn dc in tableread.columns)         { // here must fill array think. dont know how                      this.         }      } 

you should take account emmad kareem remarks.

the problem objective columns may contain values of different types. therefore, cannot collect cells in bidimensional array (one dimension columns, 1 row). example, in code below, if want merge col0array , col1array in single 2 dim array, need declare array "object[,]", gives no advantage vs datatable.

int   [] col0array = new int   [tableread.rows.count] ; // first column type int string[] col1array = new string[tableread.rows.count] ; // second column type string (int i=0;tableread.rows.count;i++) {    col0array[i]=(int   )tableread.rows[i][0] ;    col1array[i]=(string)tableread.rows[i][1] ; } 

Comments