C# OleDbDataReader到ArrayList在C中#
如何遍历OleDbDataReader并将其元素放入C# OleDbDataReader到ArrayList在C中#,c#,arraylist,datareader,.net,oledbdatareader,C#,Arraylist,Datareader,.net,Oledbdatareader,如何遍历OleDbDataReader并将其元素放入ArrayList 这是我的密码: // ... 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
ArrayList
这是我的密码:
// ...
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'.
试试这个:
while (myReader.Read())
{
list.Add(myReader.GetString(0));
}
我不明白。你想干什么?将所有可用列中的字符串放入列表,而不管列名称或类型如何?我不知道为什么会出现此错误:
无法将类型“char”转换为“string”
使用新代码编辑您的问题,而不知道无法回答!这个答案显然是错误的GetString
返回一个字符串,字符串中的foreach
在字符串的字符之间循环。如果您不知道数据类型,可以使用myReader.GetValue(0)+“”