1

Let us think my sql query is

  select customerDetials.custid,TestTable.col1 from CustomerDetails INNER JOIN TestTable  on CustomerDetails.custid=TestTables.custid where CustomerDetails.custid>0

I wanna use OleDbDataReader to retrieve rows of this .

I used this way

while (dataReader.Read())
{
       string str= dataReader["customerDetials.custid"].ToString();
}

but the prob is here join is there so if I give column name like above its throwing me an exception and I can't use index or I cant change the sql query .So is there any way to retrieve the data using the column name?

RAS
  • 8,100
  • 16
  • 64
  • 86
Bhaswanth
  • 283
  • 6
  • 14

3 Answers3

1

Have you tried using just

while (dataReader.Read()) { 
    string str= dataReader["custid"].ToString();    
}
Adriaan Stander
  • 162,879
  • 31
  • 289
  • 284
  • yeah ..tried ..but throwing an exception and its not proper also coz join table might also have a col with same name rite ..so it wont be valid. – Bhaswanth Aug 12 '10 at 11:29
  • The select is fine. leave that as is. The result returned should have 2 columns, custid and col1. – Adriaan Stander Aug 12 '10 at 11:30
  • in this case i dnt have TestTable.custid in query ...think we have it ..coz my query is dynamic i dnt kno how it comes ..so i shud write as general as possible. – Bhaswanth Aug 12 '10 at 11:33
0

If you don't know what the query will return, you will have to:

Get the FieldCount (number of columns in the result set) and then loop through the fields (columns) in each row of the DataReader retrieving the data.

You will need one or more of the following medthods:

  • GetFieldType(int) - returns the Type of the column.
  • GetDataTypeName(int) - returns the Name of the back-end database column type.
  • GetName(int) - returns the Column Name.
Derek Johnson
  • 927
  • 1
  • 11
  • 15
0

I THINK what you want is...

string str = dataReader.GetInt32(0).ToString();

where the (0) refers to the zero-based ordinal position of the columns as in the query

DRapp
  • 47,638
  • 12
  • 72
  • 142
  • i cannot use index based coz i dnt how the query will be prepared so i will not know the order of columns . – Bhaswanth Aug 12 '10 at 11:37