c# - Select 5 rows from database store each one in an array? -


i have database stores invoice information. each invoice can have 5 jobs relation, each job has unique id number.

i've performed select statement selecting jobs relevant single invoice.

i have tried many ways read selected job id , store them in jobarray. prefer have selecting using loop of ways i've tried use textboxes (which replaced array)

this recent code;

     sqlcecommand cmdcountjobs = new sqlcecommand("select count(job_id)as invcount invoice invoice_number = " + maxinvoice + " ", cn);         cmdcountjobs.connection = cn;         reader = cmdcountjobs.executereader();         while (reader.read())         {             countvalue = convert.toint32(reader["invcount"].tostring());         }                  sqlcecommand cmdjobsearch = new sqlcecommand("select job_id id invoice invoice_number = " + maxinvoice + " ", cn);         cmdjobsearch.connection = cn;         reader = cmdjobsearch.executereader();          sqlcedataadapter da = new sqlcedataadapter(cmdjobsearch);         jobarray[0] = (reader.read()) ? reader["id"].tostring() : "";         jobarray[1] = (reader.read()) ? reader["id"].tostring() : "";         jobarray[2] = (reader.read()) ? reader["id"].tostring() : "";         jobarray[3] = (reader.read()) ? reader["id"].tostring() : "";         jobarray[4] = (reader.read()) ? reader["id"].tostring() : "";            } 

could me this?

why use array? use list(of int) store id numbers using normal loop.
, given nature of lists don't need know before hand number of jobs set array size, have invoice 4 jobs or 1 6, logic of code not need check this.

    list<int> jobs = new list<int>();     sqlcecommand cmdjobsearch = new sqlcecommand("select job_id id invoice " +                                  "where invoice_number = @ivc", cn);     cmdjobsearch.connection = cn;     cmdjobsearch.parameters.addwithvalue("@ivc", maxinvoice);     reader = cmdjobsearch.executereader();      while(reader.read())          jobs.add(convert.toint32(reader["id"])); 

also note have changed query avoid string concatenation. not case practice use parametrized queries avoid sql injection

of course, if have need use array somewhere in remainder of code, can reference list array

   textbox1.text = jobs[0].tostring(); 

or convert list array with

   int[] ids = jobs.toarray(); 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -