c# - Fetch rows from database using ExecuteStoreQuery, without knowing the number of columns in the table -
i'm trying manual sql queries against sqlite database using executestorequery method on objectcontext.
the catch don't know how many columns in table i'm querying. ideally, each fetched row string[] object.
i've looked @ example 2 here: http://msdn.microsoft.com/en-us/library/vstudio/dd487208(v=vs.100).aspx
it's close want do, except don't know structure of telement i'm fetching, can't define struct in example.
below of code (not compiling due ???? telement). code below trying fetch table info, in case know structure of rows, in general don't.
is there way executestorequery? or there different way of doing it, while still using existing connection of objectcontext (rather opening new sql connection db)?
public void printcolumnheaders(nwrevaldatabaseentities entities, string tablename) { string columnlistquery = string.format("pragma table_info({0})", tablename); var result = entities.executestorequery<????>(columnlistquery); foreach (string[] row in result) { string columnheader = row[1]; // column header in second column of table console.writeline("column header: {0}", columnheader); } }
i got working based on gert arnold's comment. also, took me effort figure out need sqliteconnection, not entityconnection directly objectcontext. answer this question helped me that.
the working code below:
public static void printcolumnheaders(nwrevaldatabaseentities entities, string tablename) { var sc = ((system.data.entityclient.entityconnection)entities.connection).storeconnection; system.data.sqlite.sqliteconnection sqliteconnection = (system.data.sqlite.sqliteconnection)sc; sqliteconnection.open(); system.data.common.dbcommand cmd = sc.createcommand(); cmd.commandtype = system.data.commandtype.text; cmd.commandtext = string.format("pragma table_info('{0}');", tablename); system.data.common.dbdatareader reader = cmd.executereader(); if (reader.hasrows) { object[] values = new object[reader.fieldcount]; while (reader.read()) { int result = reader.getvalues(values); string columnheader = (string)values[1]; // table_info returns row each column, column header in second column. console.writeline("column header: {0}", columnheader); } } sqliteconnection.close(); }
Comments
Post a Comment