Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advice on Data Structure

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
Hello, I am facing the following issue:
I am using DataReader to get a table from a db
Now in my app I need to reference different rows of that table (based on primary keys) many times, so I will have to go back and forth.
I found that once you do Read() on DataReader you cannot go back to the row.
Any idea if I can transfer the data from DataReader when I first create it to some data structure that will allow me to get the data from rows (based on column names, etc) like from DataReader, but will also let me go back and forth?

so do somethig like
while dr.Read() { ...move data to some data structure}

and that data structure would have the same structure as the table, like

columnname colname colname colname
primarykey field1 field2 field3
primarykey ..... ..... .....

so I can get data based on colname and primary key (like from a regular table)

thanks for any advice!!!
 
Re-create the table structure in a C++ structure:

typedef struct {
<type1> primaryKey;
<type2> field2;
<type3> field3;
//and so forth and so on for all the fields in the table
}TableStruct

Note that the <type1>, <type2> etc are types according to the column data types in that table (i.e. if column1 is INT then <type1> is integer, if column2 is varchar then <type2> is string, etc...)

Create a new hashtable.

While parsing the retrieved data using the DataReader, for each record allocate a new TableStruct instance and fill it with the retreived data. After filling the whole structure, add it to the hashtable using the <primarykey> as key and the newly filled structure as the object.


Then you can parse the hashtable using foreach. To use for and while you need to use keys collection which is very simple.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top