2011-12-17 29 views
0

如何迭代通過OleDbDataReader並將其元素放入ArrayListOleDbDataReader以ArrayList在C#

這裏是我的代碼:

// ... 

ArrayList list = new ArrayList(); 

while(myReader.Read()) 
{ 
    foreach(string s in myReader) // I got an Exception here 
    { 
     list.Add(s); 
    } 
} 

// ... 

Label lbl = new Label(); 
lbl.Text = list[i] as string; 

這裏是例外:

System.InvalidCastException: Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.String'. 
+0

我不明白這一點。你想做什麼?將所有可用列中的字符串放入列表中,而不考慮列名或類型? – GSerg 2011-12-17 12:27:07

回答

1

試試這個:

while (myReader.Read()) 
{ 
    list.Add(myReader.GetString(0)); 
} 
+0

我不知道爲什麼我得到這個錯誤:'不能將類型'字符'轉換爲'字符串' – 2011-12-17 12:27:16

+0

用您的新代碼編輯您的問題,但沒有看到它不可能回答! – 2011-12-17 12:28:18

+2

這個答案顯然是錯誤的。 'GetString'返回一個字符串,並且一個字符串中的'foreach'循環遍歷字符串的字符。 – GSerg 2011-12-17 12:28:50