Friends, I have a dynamic sql query that I would like to execute and return a list. From most of my internet searches on goole, I found that the type of the list has to be known to convert the sqldatareader to a list. How can I do that when I do not know what query will be executed.
Execute(String query)
{
SqlConnection con=new SqlConnection(connection);//connection from elsewhere
SqlCommand cmd = new SqlCommand(query);
cmd.connection=con;
con.Open();
SqlDataReader result=cmd.ExecuteReader();
//How to convert result to a list when i do not know the table structure
}
Now I want to convert the result to a list. However I do not know the details of the table which the query works on.The query can vary, it can query any table. So under these conditions how can I convert the result to a list. Is it possible?
This leaves me with one simple question, if I do not know the table details, then what will be the entries of the list or it will be a list of what?
To answer that question, I ask myself another question, is it possible to have a list where each list entry will correspond to one row of the SqlDataReader result?
So I can use a List < DataRow > x =result.Select().ToList(), but I do not want to use this either.
I want each entry of the list to map to each row of the result and at the same time be composed of the atomic datatypes which make a row.
Eg Select studentname,studentid from student; This will return me a SqlDataReader result and I want to construct a list whose type contains a string and an int and then populate the list with the rows of the result.
When the query changes to select marks,subject,grade from marks where studentid=1432, then I want to construct a list whose type contains int,string,char and then fill the list with the rows of the result.
Is there a way to do it?
PS The user knows how to pick the values if I return a list as detailed above, since he created the query and he knows how many columns to expect.