How to get column names from a table in sqlite via PRAGMA (.net / c#)? -


i have been struggling right c# code getting values after pragma table_info query.

since edit code rejected in this post, made question other people otherwise waste hours fast solution.

assuming want datatable list of field of table:

 using (var con = new sqliteconnection(preparedconnectionstring))     {        using (var cmd = new sqlitecommand("pragma table_info(" + tablename + ");"))         {             var table = new datatable();              cmd.connection = con;             cmd.connection.open();               sqlitedataadapter adp = null;                 try                 {                     adp = new sqlitedataadapter(cmd);                     adp.fill(table);                     con.close();                     return table;                 }               catch (exception ex)               { }          }      } 

return result is:

  • cid: id of column
  • name: name of column
  • type: type of column
  • notnull: 0 or 1 if column can contains null values
  • dflt_value: default value
  • pk: 0 or 1 if column partecipate primary key

if want column names list can use (you have include system.data.datasetextension):

 return table.asenumerable().select(r=>r["name"].tostring()).tolist(); 

edit: or can avoid datasetextension reference using code:

using (var con = new sqliteconnection(preparedconnectionstring))       {           using (var cmd = new sqlitecommand("pragma table_info(" + tablename + ");"))           {               var table = new datatable();               cmd.connection = con;               cmd.connection.open();                sqlitedataadapter adp = null;               try               {                   adp = new sqlitedataadapter(cmd);                   adp.fill(table);                   con.close();                   var res = new list<string>();                   for(int = 0;i<table.rows.count;i++)                       res.add(table.rows[i]["name"].tostring());                   return res;               }               catch (exception ex){ }           }       }       return new list<string>(); 

there lot of pragma statements can use in sqlite, have @ link.

about using statement: it's simple, used sure disposable objects disposed whatever can happen in code: see this link or this reference


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -