PDA

Click to See Complete Forum and Search --> : Data Readers


MeanMachine
Apr 29th, 2007, 10:05 AM
Hello All!

I'm a VB .NET programmer but I'm also doing some C# code as well. Could someone show me some code examples of how Data Readers are looped?

Here's how I do this in VB .NET:

con.Open 'Open connection

dr = com.ExecuteReader 'Use Command to Execute Reader

Do While dr.Read

ComboBox.Items.Add(dr!CustomerID) 'Read field Customer ID Key

End Loop
con.Close
con = Nothing 'Release object

Here's some of the understanding of C# sharp so far:

con.Open;

dr = com.ExecuteReader;

do while dr.Read :confused:

// Method here? :confused:

con.Close :confused:

vbNeo
Apr 29th, 2007, 12:07 PM
con.Open;

dr = com.ExecuteReader();

While(dr.Read()) {
comboBox.Items.Add(dr!CustomerID);
}

con.Close();
(not sure about this one though): con = null;

jmcilhinney
Apr 29th, 2007, 07:53 PM
This:con = Nothing 'Release objectdoes not do what you think it does. You should be doing this:con.Dispose()Also, I'm not 100% sure that C# supports this notation:dr!CustomerIDIf it does then cool, but if it doesn't then use this:dr("CustomerID")

penagate
Apr 29th, 2007, 08:33 PM
Shouldn't that be dr["CustomerID"]?

jmcilhinney
Apr 29th, 2007, 09:35 PM
Shouldn't that be dr["CustomerID"]?Indeed. :blush:

Mean Machine
May 1st, 2007, 03:29 AM
Thanks guys. I will give some of these a go.

PS Never like database programming the best of times. :wave:

penagate
May 1st, 2007, 07:55 AM
I used to hate database programming but really it's mostly because libraries insist on making it more complicated than necessary. .NET provides a large array of classes to ease data access but it can be hard to decide which ones you need (in other words, it almost provides too many). Once you get the hang of creating a basic data access layer you realise it's very simple and you can follow the same patterns in many of your applications.

Mean Machine
May 1st, 2007, 08:54 AM
Allot advice down under. Cheers!

PS I'm from England by the way. :thumb:

jmcilhinney
May 1st, 2007, 06:27 PM
Are you aware that you created and replied to this thread using two different user names?

MeanMachine
May 2nd, 2007, 04:34 PM
Indeed, had some internet problems along with email problems as well. :cry: