title: Most Efficient Way To Get A Row Of Data From DB In ASP.NET
body: <p>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). </p>
  
  <p>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. </p>
  
  <pre><code>        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 &lt; dr.FieldCount; count++)
                       {
                           nvc[dr.GetName(count)] = dr.GetValue(count).ToString();
                       }
                   }
              }
          }
  
          dbCommand.Dispose();
          return nvc.Count != 0 ? nvc : null;
      }
  </code></pre>
  
  <p>Now my apporach for this kind of data access would normally be to get a method to return a datarow.  </p>
  
  <pre><code>       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;
      }
  </code></pre>
  
  <p>It does seem kind of wastefull to create a DataTable and then return its first datarow. </p>
  
  <p>Is there better way to do this?</p>
  
  <p>I'm thinking maybe a Dictionary of objects which I then manually cast each member of. </p>
  
  <p>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. </p>
  
  <p>That said this method is likely to be called for allot of data access queries on allot of sites on one box. </p>
  
  <p>Cheers</p>
  
  <p>Steve</p>