× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On Fri, 2005-06-10 at 09:44, Rohan Sootarsing wrote:
> Hi I am Using a dataset to return data but want to convert this to
> datareader but can't find much doumentation can anyone help me here is
> my current code
> 
> 
> iDB2Connection connDB2=new iDB2Connection(
>   "DataSource=TDC01;" +
>   "userid=RDS;password=sootarsing;DefaultCollection=QGPL;");
> DataSet ds = new DataSet();
> iDB2DataAdapter adpt = new iDB2DataAdapter();
> string szSQLstmt = "Select * from ctx4nthg where REF ='" +@filter+"'";
> adpt.SelectCommand = new iDB2Command(szSQLstmt,connDB2);
> adpt.Fill(ds);
> return ds;

DataReader is very simple to use:

<code>
[create the connection and SQL string as above...]
iDB2Command cmd = new iDB2Command( sqSQLstmt , connDB2 );
iDB2DataReader reader = cmd.ExecuteReader();
while ( reader.Read() )
{
        // Process the row
}
reader.Close();
</code>

Some notes for use: 
1) A DataReader has an active database connection, which means it will
stay open until you close it.  (This is unlike a DataSet which is
disconnected).  As a rule of thumb, issue the .Close() method as soon as
possible.

2) To obtain the field data from the current row, you access it like an
array variable, using either the field name or the index number
(0-based):

        string field1 = (string)reader[ "FIELD1" ];
        // or
        string field1 = (string)reader[ 0 ];

If you don't know the index numbers, you can retrieve them by using the
GetOrdinal() method:

        int field1Ordinal = reader.GetOrdinal( "FIELD1" );

Performance will be much better using ordinal values, so I typically
capture the ordinals outside the loop and then use them to access the
fields:

<code>
iDB2Command cmd = new iDB2Command( sqSQLstmt , connDB2 );
iDB2DataReader reader = cmd.ExecuteReader();

int field1Ordinal = reader.GetOrdinal( "FIELD1" );
int field2Ordinal = reader.GetOrdinal( "FIELD2" );
int field3Ordinal = reader.GetOrdinal( "FIELD3" );
while ( reader.Read() )
{
        string field1 = (string)reader[ field1Ordinal ];
        int field2 = (int)reader[ field2Ordinal ];
        string field3 = (string)reader[ field3Ordinal ];
}
reader.Close();
</code>

3. Finally, if you are retrieving a single field, look at ExecuteScalar
- it should be lightning fast.

Hope this helps,

Joel Cochran
http://www.rpgnext.com


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.