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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:37:08+00:00 2026-06-12T18:37:08+00:00

This isn’t related to a particular issue BUT is a question regarding best practise.

  • 0

This isn’t related to a particular issue BUT is a question regarding “best practise”.

For a while now, when I need to get data straight from the database I’ve been using the following method – I was wondering if there’s a faster method which I don’t know about?

DataTable results = new DataTable();
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Name"]))
{
    connection.Open();
    using (SqlCommand command = new SqlCommand("StoredProcedureName",connection))
    {
      command.CommandType = CommandType.StoredProcedure;
      /*Optionally set command.Parameters here*/
      results.Load(command.ExecuteReader());
    }
}
/*Do something useful with the results*/
  • 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-06-12T18:37:09+00:00Added an answer on June 12, 2026 at 6:37 pm

    There are indeed various ways of reading data; DataTable is quite a complex beast (with support for a number of complex scenarios – referential integrity, constraints, computed values, on-the-fly extra columns, indexing, filtering, etc). In a lot of cases you don’t need all that; you just want the data. To do that, a simple object model can be more efficient, both in memory and performance. You could write your own code around IDataReader, but that is a solved problem, with a range of tools that do that for you. For example, you could do that via dapper with just:

    class SomeTypeOfRow { // define something that looks like the results
        public int Id {get;set;}
        public string Name {get;set;}
        //..
    }
    ...
    var rows = connection.Query<SomeTypeOfRow>("StoredProcedureName",
        /* optionalParameters, */ commandType: CommandType.StoredProcedure).ToList();
    

    which then very efficiently populates a List<SomeTypeOfRow>, without all the DataTable overheads. Additionally, if you are dealing with very large volumes of data, you can do
    this in a fully streaming way, so you don’t need to buffer 2M rows in memory:

    var rows = connection.Query<SomeTypeOfRow>("StoredProcedureName",
        /* optionalParameters, */ commandType: CommandType.StoredProcedure,
        buffered: false); // an IEnumerable<SomeTypeOfRow>
    

    For completeness, I should explain optionalParameters; if you wanted to pass @id=1, @name="abc", that would be just:

    var rows = connection.Query<SomeTypeOfRow>("StoredProcedureName",
        new { id = 1, name = "abc" },
        commandType: CommandType.StoredProcedure).ToList();
    

    which is, I think you’ll agree, a pretty concise way of describing the parameters. This parameter is entirely optional, and can be omitted if no parameters are required.

    As an added bonus, it means you get strong-typing for free, i.e.

    foreach(var row in rows) {
        Console.WriteLine(row.Id);
        Console.WriteLine(row.Name);
    }
    

    rather than having to talk about row["Id"], row["Name"] etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This isn't the best question ever, but since search engines feel the need to
This isn't the best programming question but lets face it, the server fault guys
This isn't easy to explain, but I'll try my best. The issue has started
This isn't strictly a programming question, but I'm asking it here because it's certainly
This isn't a code question for once, but it definitely has me confused. Basically,
This isn't a very simple question, but hopefully someone has run across it. I
this isn't really an issue, but more of a concern that I would appreciate
This isn't a repeat of a previous question, I have found out the issue
This isn't really a technical question, but I essentially have two sites, a mobile
This isn't a classic programming language question, but it's keeping me busy so I

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.