c# - SQL Query returning all table columns -


sqlcommand query = new sqlcommand(); query.connection = dbconn; try {     query.commandtext = "select email,lastsync users email in ('alexander@contoso.com')";     sdr = query.executereader();     while(sdr.read()) if(!sdr.isdbnull(1)) syncedusers.add(sdr.getstring(0));     sdr.close(); } catch(exception e) {     logger.log(e.message);     list<string> fields = new list<string>();     if(sdr!=null) (int = 0; < sdr.fieldcount; i++)     {         fields.add(sdr.getname(i));     }     logger.log(string.join(",",fields)); } 

this (not always) throws error:

unable cast object of type 'system.int32' type 'system.string'. 

and 1 time looked, logging contained list of all fields of table, not 2 requested fields:

id,email,lastsync,... 

although query 2 columns only. id int, email string, assume error thrown in getstring(0). error not reliably reproducible, did not yet while vs debugging enabled.

now, long reader open, field list cannot change, or think. possible under circumstances sql server returns more fields requested?

it sql server express 2012.

i know can "solved" using column names instead of indexes, before do, want make sure issue. cannot reproduce issue across machines deployed code to, machines seem affected (it doesn't seem depend on # of cores available machine, nor on operating system or .net version)

edit: have same problem when using executescalar(), on same table. doesn't happen other tables, far can see. try string using query

select displayname users email=@email 

using executescalar(), , result 4, id of row requested. seems right row, not right columns.

this weird!


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -