c# - Is it possible to fill a normal array with values from the sql server? -


i want create method returns array of class know how arraylist need fill 'normal' array[].

i'm doing arraylist

public arraylist getlistproductfamily() {              arraylist arrayproductfamily = new arraylist();      iconnection conn = connutil.getconnection();     string query = get_query;      productfamily productfamily = new productfamily();      try     {         conn.executereader(query);          if (conn.datareader.read())         {             productfamily.cidproductfamily = db.loadstring(conn.datareader, "cidproductfamily");             productfamily.cproductfamily = db.loadstring(conn.datareader, "cproductfamily");             productfamily.lactive = convert.toint32(db.loadint(conn.datareader, "lactive"));              arrayproductfamily.add(productfamily);         }     }     catch (exception ex)     {         logger.error("error", ex);     }      return arrayproductfamily; } 

public productfamily[] getlistproductfamily() {     list<productfamily> arrayproductfamily = list<productfamily>();      iconnection conn = connutil.getconnection();     string query = get_query;       try     {         var datareader = conn.executereader(query);          while (datareader.read())         {             productfamily productfamily = new productfamily();             productfamily.cidproductfamily = db.loadstring(datareader, "cidproductfamily");             productfamily.cproductfamily = db.loadstring(datareader, "cproductfamily");             productfamily.lactive = convert.toint32(db.loadint(datareader, "lactive"));              arrayproductfamily.add(productfamily);         }     }     catch (exception ex)     {         logger.error("error", ex);     }      return arrayproductfamily.toarray(); } 

its pretty simple, replace arraylist in class list<productfamily> , return .toarray() list.

the use of arraylist depreciated since introduction of generics, , should avoid as possible on use of list<t> strong typing.

you not iterating through reader, , i'm not sure db.loadxxx does, i'm assuming takes idbdatareader , loads specified column name. these can replaced datareader.getint32 or datareader.getstring, depending on want read.

it should noted want check dbnull, otherwise throw exception , exit out. want move try/catch inside while loop 1 single record doesn't cause reader fail.

you want use reader in using loop, i'll leave implement.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -