OracleDataReade and HashTable question

T

Tony!

Below is a snippet of code I'm working on. Don't I need some sort of
MoveNext command at the end of the While to move to the next record in
the read? I tried the reader.NextResult, but looking at online
examples, I see that in VB examples, but not in the C# examples..
Does C# somehow not need it?

Also, the 2nd question in my post here is that I created a HashTable
to hold the Agent's ID# (used as the Key in the HashTable) and the
total # of records found (used as the Value)

Thinking I can then loop throughthe HashTable contents and pull each
agent id and how many recs they had from there do more stuff with that
info.

But, googling makes me nowbelieve I was thinking in error and
something like a multidimensional array or an arraylist may be better.
I don't have much experience with either yet.... I'm a newbie

It seems to pull from a HashTable, you need to already have the Key
value in hand to get the value paired with that key, right? There is
no way to loop through and get both the key and the value associated
with it?

// Create the OracleCommand object
cmdQuery = "select agent_id, count(*) as TOTAL
from ORA_DBASE.WEEKLY_DVC_TEMP group by agent_id";

// Create the OracleCommand object
OracleCommand cmd = new OracleCommand(cmdQuery,
con);

writeLog(DateTime.Now.ToString() + " cmd
Oraclecommand created");

// Execute command, create OracleDataReader object
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show("\r\n\r\n" + ("Agent : " +
reader.GetInt32(0).ToString() + "\r\n" +
"RecordCount : " +
reader.GetInt32(1).ToString() + "\r\n"));

AgentCounts.Add(reader.GetInt32(0),
reader.GetInt32(1));
reader.NextResult();

}


Any help or advice would be appreciated :)

Thanks,

Tony!
 
A

Arne Vajhøj

Tony! said:
Below is a snippet of code I'm working on. Don't I need some sort of
MoveNext command at the end of the While to move to the next record in
the read? I tried the reader.NextResult, but looking at online
examples, I see that in VB examples, but not in the C# examples..
Does C# somehow not need it?

Also, the 2nd question in my post here is that I created a HashTable
to hold the Agent's ID# (used as the Key in the HashTable) and the
total # of records found (used as the Value)

Thinking I can then loop throughthe HashTable contents and pull each
agent id and how many recs they had from there do more stuff with that
info.

But, googling makes me nowbelieve I was thinking in error and
something like a multidimensional array or an arraylist may be better.
I don't have much experience with either yet.... I'm a newbie
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
MessageBox.Show("\r\n\r\n" + ("Agent : " +
reader.GetInt32(0).ToString() + "\r\n" +
"RecordCount : " +
reader.GetInt32(1).ToString() + "\r\n"));

AgentCounts.Add(reader.GetInt32(0),
reader.GetInt32(1));
reader.NextResult();

}

re 1)

..Read() moves the reader to next record.

You do not need .NextResult() which moves the reader to the
next result set (output from another SELECT statement).

re 2)

If the id's are consecutive from 0 to N, then you can use
ArrayList instead of Hashtable.

BTW, Dictionary<> and List<> have replaced Hashtable and
Arraylist since .NET 2.0 !

Arne
 
T

Tony!

re 1)

.Read() moves the reader to next record.

You do not need .NextResult() which moves the reader to the
next result set (output from another SELECT statement).

re 2)

If the id's are consecutive from 0 to N, then you can use
ArrayList instead of Hashtable.

BTW, Dictionary<> and List<> have replaced Hashtable and
Arraylist since .NET 2.0 !

Arne

Thanks for the reply. I went through the list of methods again and
found the .read. cool.

I ended up using Dictionary<> after googling it following seeing your
reply :)

was able to loop through it like

foreach (KeyValuePair<int, int> pair in AgentCounts)
{
MessageBox.Show("Key: " + pair.Key + " Value: " +
pair.Value);
}

Now I'll dig in and replace the MessageBox.Show I used for testing
with real useful code..

Thanks for the help!

Tony!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top