c# - Extract data from datatable -


i extract data sheets in workbook using following code :

foreach (var sheetname in getexcelsheetnames(connectionstring))             {                 if (sheetname.contains("_"))                 {                 }                 else                 {                     using (oledbconnection con = new oledbconnection(connectionstring))                     {                          var datatable = new datatable();                         string query = string.format("select  * ,{0} sheetname [{0}]", sheetname);                         con.open();                         oledbdataadapter adapter = new oledbdataadapter(query, con);                         try                         {                             adapter.fill(datatable);                             ds.tables.add(datatable);                         }                         catch { }                     }                 } 

i can't figure how data stocked in datatable : sheetname added column ? how can extract ?

  foreach (datatable dt in ds.tables)                     {                             using (sqlconnection con = new sqlconnection(consstring))                             {                                 con.open();                                 (int = 0; < dt.rows.count; i++)                                 {                                      (int j = 0; j < dt.columns.count; j ++)                                     { //what should write here ?                                     }                                  }                               } 

in order sheet name, using oledb, need use code looks (thanks post , answer):

datatable dtsheets = con.getoledbschematable(oledbschemaguid.tables, null);      list<string> sheets= new list<string>();      foreach (datarow dr in dtsheets.rows)      {         if (dr["table_name"].tostring().contains("$"))//checks whether row contains '_xlnm#_filterdatabase' or sheet name(i.e. sheet name ends $ sign)         {              sheets.add(dr["table_name"].tostring());         }       } 

below how access values datatable:

var somevalue = dt.rows[i][j] 

you need item @ column index (j) of row, @ row index (i), of current datatable (dt).

conversely, can use name of column well.

var somevalue = dt.rows[i]["columnname"] 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -