Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert SqlDataReader to a List of unknown types because of dynamic query
    primarykey
    data
    text
    <p>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.</p> <pre><code>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 } </code></pre> <p>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?</p> <p>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?</p> <p>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?</p> <p>So I can use a List &lt; DataRow > x =result.Select().ToList(), but I do not want to use this either.</p> <p>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.</p> <p>Eg <strong>Select studentname,studentid from student;</strong> 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.</p> <p>When the query changes to <strong>select marks,subject,grade from marks where studentid=1432</strong>, then I want to construct a list whose type contains int,string,char and then fill the list with the rows of the result.</p> <p>Is there a way to do it? </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload