Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6908643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:35:19+00:00 2026-05-27T08:35:19+00:00

Friends, I have a dynamic sql query that I would like to execute and

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:35:20+00:00Added an answer on May 27, 2026 at 8:35 am

    If the caller will know the type, make is a generic method, and create a T per row…

    Or, since dapper-dot-net already does that:

    var list = connection.Query<T>(command [, args]).ToList();
    

    If the caller can’t know the T either, you could use ExpandoObject to populate a List<dynamic>, casting the expando to IDictionary<string,object> to populate it with key/value pairs.

    Or, since dapper-dot-net already does that:

    var list = connection.Query(command [, args]).ToList();
    

    Then the caller can use:

    foreach(var item in list) {
        Console.WriteList("{0}, {1}, {2}", item.marks, item.subject, item.grade);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database table that i want to allow my friends to update.
Hi friends I have a table like this ID bid sub_bid cid sub_cid 1
I have a class that uses either a static list (firstFriend in the example)
I have a list of friends, (BOB, TINA, LISA, TERRY, MIKE) and each listed
I have two professional programmer friends who are going to teach me, and they
I have a working WAMP environment (Apache Friends). I decided to try Subversion and
friends, i have created custom title bar using following titlebar.xml file with code <?xml
Friends, I have a strange need and cannot think my way through the problem.
friends, i have a EditText on simple activity with a button. when every i
I have a Person class. A person class contains a collection of Friends (also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.