You haven't mentioned what technology you're using to execute the query, so I'll assume you're using a basic technology that doesn't automatically map to classes.
It's fairly easy to do. All you do is this:
- Iterate over every row you received
- Create a new
KeyValuePair
object with the types appropriate to the columns (typically this is string, string
or int, string
)
- Set the values of the
KeyValuePair
to the values of each column you read from the reader.
- If you want to do something else with the
KeyValuePair
later, try adding it to a List<KeyValuePair>
during the iteration
Edit Ok, so based on your edits, I'm guessing you're new to the whole thing. You mentioned you're using DB2. You haven't been explicit as to which library you're using to read from DB2, so I'm going to assume it's the library provided by IBM. I'll provide information based on the DB2DataReader class documentation from IBM.
The following should work (provided I haven't typoed). I've included comments to help along the way:
// this variable will have the data read from the server after all is said and done
List<KeyValuePair<int, string>>> data = new List<KeyValuePair<int, string>>();
// create the connection (replace connection string as necessary)
using (DB2Connection conn = new DB2Connection("database connection string"))
{
// specify the query, create and open the command object
string qry = "Select distinct Value, Descrip from cool";
DB2Command cmd = new DB2Command(qry, conn);
conn.Open();
// calling ExecuteReader() sends the query to the database and starts
// the process of reading the data
using(DB2DataReader read = cmd.ExecuteReader())
{
// you have to call Read() for every row. It will return false if there
// are no more rows left
while(read.Read())
{
// calling the various Get methods is how you get the data out of the reader
// note that you pass in the index of the column for each call
int intVal = read.GetInt32(0);
string strVal = read.GetString(1);
// now you're ready to create your KeyValuePair object
KeyValuePair<int, string> tmp = new KeyVauePair<int, string>(intVal, strVal);
// And add it to the list we defined at the start of this
data.Add(tmp);
}
}
}
// and now we've got all of the data from your query wrapped up into a nice list
// called 'data'. Do with it whatever you planned