Filling an arraylist from a datareader
I have a problem with how to fill an array list from a data reader
string queryDTL = " SELECT * FROM tbl1 ";
connection.Connect();
cmd = new OracleCommand(queryDTL, connection.getConnection());
dr_DTL = qcmd2.ExecuteReader();
ArrayList RecordsInfo = new ArrayList();
while (dr_DTL.Read())
{
RecordsInfo = dr_DTL["number"].ToString();
}
The problem is the datareader contain alot of info other than the number but I don't know how to put them in their correct position.
I am still a beginner in this开发者_Python百科 sorry if it sounds stupid.
You can't put a String
in an ArrayList
. You have to add the string to the list.
Ex. :
ArrayList RecordsInfo = new ArrayList();
while (dr_DTL.Read())
{
RecordsInfo.Add(dr_DTL["number"].ToString());
}
If you want a list of String the best way is using List<String>
.
Ex. :
List<String> RecordsInfo = new List<String>();
while (dr_DTL.Read())
{
RecordsInfo.Add(dr_DTL["number"].ToString());
}
I think you would be better off with a DataTable and an adapter:
SqlConnection Conn = new SqlConnection(MyConnectionString);
Conn.Open();
SqlDataAdapter Adapter = new SqlDataAdapter("Select * from Employees", Conn);
DataTable Employees = new DataTable();
Adapter.Fill(Employees);
GridView1.DataSource = Employees;
GridView1.DataBind();
Of course in your case, use the Oracle versions of the objects.
Hi you have to do RecordInfo.Add
, currently you are reassigning the whole arraylist...
Also if you are retrieving too many columns, just query the columns you need, in your case number
, not simply SELECT *
...
精彩评论