Improving code readability when using iDB2 .Net connector for iSeries

By steenbach

I have done a few apps over the last years using IBM’s .Net data connector for iSeries (i5)

When you use the DataReader, you usually end up with a lot of places where you access the content of your datareader like this: ‘dr.GetString(8)’
The problem is that after some time you can’t remember which fields actually had number 8 in the array, and you have to go to your SQL statement to find out.

I came across the method “GetOrdinal” on the DataReader object and wondered what was the purpose for that, and after a little googling, I found out, that it actually solved one of my everyday problems (a small one, but anayway;))

So here is how it works.
In the scope of ‘while(dr.Read())’, you call the method with the DB2 field name, and it returns an integer telling the index of the field in the datareader array: 

int db2CustomerNo = dr.GetOrdinal(“ODKUND”);

and then in your code, you can access the field using your assigned integer like this:

dr.GetString(db2CustomerNo)

No rocket science – just a little trick, that makes your day a little bit easier;)

Tags: ,

Leave a Reply