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 7558317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:20:22+00:00 2026-05-30T12:20:22+00:00

I am trying to run over all the fields of a row returned by

  • 0

I am trying to run over all the fields of a row returned by ExecuteReader(), see here:

SqlCommand cmd = new SqlCommand(@"select * from People;", conn);
rdr = cmd.ExecuteReader();
while (rdr.Read()) 
{
    foreach (Object i in rdr) 
    {
        Console.WriteLine(i.ToString() + " ");
    }
}

This outputs to the console:

System.Data.Common.DataRecordInternal

What should I put instead of object, as some of the fields are numerical (probably being internally cast to int) and some arevarchar (probably being internally cast to string). I did try string but that throws an error on the numerical fields. Likewise, using type var won’t compile.

Thanks.

  • 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-30T12:20:24+00:00Added an answer on May 30, 2026 at 12:20 pm

    When you iterate foreach (object i in rdr) you are iterating the rows, not the columns. See the documentation for the data reader’s GetEnumerator() method. That’s why the ToString override is returning System.Data.Common.DataRecordInternal: you’re calling ToString() on the whole data record, not just one of its values.

    To iterate the values of the reader’s current row, call the GetValues() method instead; this returns an array of objects corresponding to populates an array of objects with the values for the current row:

    while (rdr.Read())  
    {
        var items = new object[rdr.FieldCount];
        rdr.GetValues(items);
        foreach (object i in items)  
            Console.WriteLine(i.ToString() + " "); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently got a new computer, so I've been trying to move over all
I'm trying to run some statistics over the Stack Overflow data dump, and for
I'm currently trying to run a LINQ query over a MS SQL database. This
I am trying run a program from a qmake .pro file which modifies the
I'm trying to run tcpdump in the background while I send some traffic over
I am trying to syphon data over from one table to another. The problem
Resharper uses up alot of memory (over 1GB of ram) when trying to run
I am trying to retrieve fields from a table in a SQLite database. I've
I'm trying to run the example from this question about using the BMC remedy
I've been all over the web trying to find a way to get VIM

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.