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 173943

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:25:58+00:00 2026-05-11T13:25:58+00:00

I’m writing a method to return an ‘asset’ row from the database. It contains

  • 0

I’m writing a method to return an ‘asset’ row from the database. It contains strings, ints and a byte array (this could be an image/movie/document).

Now for most row access I use the following method which returns a NameValueCollection as it is a light weight object, easy to use and cast int and strings.

        public static NameValueCollection ReturnNameValueCollection(Database db, DbCommand dbCommand)     {          var nvc = new NameValueCollection();          using (IDataReader dr = db.ExecuteReader(dbCommand))         {             if (dr != null)             {                  while (dr.Read())                  {                      for (int count = 0; count < dr.FieldCount; count++)                      {                          nvc[dr.GetName(count)] = dr.GetValue(count).ToString();                      }                  }             }         }          dbCommand.Dispose();         return nvc.Count != 0 ? nvc : null;     } 

Now my apporach for this kind of data access would normally be to get a method to return a datarow.

       public static DataRow ReturnDataRow(Database db, DbCommand dbCommand)     {         var dt = new DataTable();          using (IDataReader dr = db.ExecuteReader(dbCommand))             if (dr != null) dt.Load(dr);          dbCommand.Dispose();         return dt.Rows.Count != 0 ? dt.Rows[0] : null;     } 

It does seem kind of wastefull to create a DataTable and then return its first datarow.

Is there better way to do this?

I’m thinking maybe a Dictionary of objects which I then manually cast each member of.

Would be interesting to see how others have tackled this. I know this kinda falls into the field of micro optimisation and as long as I’m not returning DataSets for each row query (wish I had a pound for everytime I saw that in a line of code) it should be ok.

That said this method is likely to be called for allot of data access queries on allot of sites on one box.

Cheers

Steve

  • 0 0 Answers
  • 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. 2026-05-11T13:25:59+00:00Added an answer on May 11, 2026 at 1:25 pm

    how’s it going?

    Is there a reason you don’t have object containers that represent a row in your database? Creating a custom object is much easier to deal with in other tiers of your solution. So, going with this approach, there are two very viable solutions to your problems.

    Say you have a custom object that represents a Product in your database. You’d define the object like this:

    public class Product {     public int ProductID { get; set; }     public string Name { get; set; }     public byte[] Image { get; set; } } 

    And you’d fill a collection of of Products (Collection) like this:

    var collection = new Collection<Product>();  using (var reader = command.ExecuteReader()) {     while (reader.Read()) {         var product = new Product();          int ordinal = reader.GetOrdinal('ProductID');         if (!reader.IsDBNull(ordinal) {             product.ProductID = reader.GetInt32(ordinal);         }          ordinal = reader.GetOrdinal('Name');         if (!reader.IsDBNull(ordinal)) {             product.Name = reader.GetString(ordinal);         }          ordinal = reader.GetOrdinal('Image');         if (!reader.IsDBNull(ordinal)) {             var sqlBytes = reader.GetSqlBytes(ordinal);             product.Image = sqlBytes.Value;         }          collection.Add(product);     } } 

    Notice that I’m retrieving a value via the reader’s Getx where x is the type that I want to retrieve from the column. This is Microsoft’s recommended way of retrieving data for a column per http://msdn.microsoft.com/en-us/library/haa3afyz.aspx (second paragraph) because the retrieved value doesn’t have to be boxed into System.Object and unboxed into a primitive type.

    Since you mentioned that this method will be called many, many times, in an ASP.NET application, you may want to reconsider such a generic approach as this. The method you use to return a NameValueCollection is very non-performant in this scenario (and arguably in many other scenarios). Not to mention that you convert each database column to a string without accounting for the current user’s Culture, and Culture is an important consideration in an ASP.NET application. I’d argue that this NameValueCollection shouldn’t be used in your other development efforts as well. I could go on and on about this, but I’ll save you my rants.

    Of course, if you’re going to be creating objects that directly map to your tables, you might as well look into LINQ to SQL or the ADO.NET Entity Framework. You’ll be happy you did.

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

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Anyone can create a certificate using Makecert.exe. But it obviously… May 12, 2026 at 12:54 am
  • Editorial Team
    Editorial Team added an answer In my experience (same problem as you), it's 90% a… May 12, 2026 at 12:54 am
  • Editorial Team
    Editorial Team added an answer Often adding an alert will do funky stuff when javascript… May 12, 2026 at 12:54 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.