2010-08-12 43 views
1

讓我們覺得我的SQL查詢使用OleDbDataReader

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

我想使用OleDbDataReader來檢索該行檢索值。

我用這樣的方式

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

但概率是在這裏參加在那裏,所以如果我給列名狀高於其拋出了我的異常,我無法使用索引或我不能改變SQL查詢那麼有什麼方法可以使用列名來檢索數據嗎?

回答

1

您是否嘗試過只用

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

yeah ..tried ..但拋出一個異常,它不適當也coz連接表也可能有同名的禮拜儀式..所以它不會有效。 – Bhaswanth 2010-08-12 11:29:27

+0

選擇很好。保持原樣。返回的結果應該有2列,custid和col1。 – 2010-08-12 11:30:39

+0

在這種情況下,我dnt有查詢中的TestTable.custid ...認爲我們有它..coz我的查詢是動態的我不知道它是如何來到..所以我不喜歡寫儘可能一般。 – Bhaswanth 2010-08-12 11:33:09

0

我想你想要的是......

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

其中,(0)是指在列的從零開始的序號位置查詢

+0

我不能使用索引爲基礎因爲我不知道如何準備查詢,所以我不知道列的順序。 – Bhaswanth 2010-08-12 11:37:42

0

如果您不知道查詢將返回什麼,您將不得不:

獲取FieldCount(結果集中的列數),然後遍歷DataReader每一行中的字段(列)以檢索數據。

您需要以下medthods的一種或多種:

  • GetFieldType(INT) - 返回列的類型。
  • GetDataTypeName(int) - 返回後端數據庫的名稱 列類型。
  • GetName(int) - 返回列名稱。