Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. reading from sqldatareader

reading from sqldatareader

Scheduled Pinned Locked Moved C#
questionperformance
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pgraeve
    wrote on last edited by
    #1

    What is the BEST / HIGHEST PERFORMANCE method of reading data from a SqlDataReader and returning a "default" value if the value from the reader is null. I have several different methods that all work but all involve calling a seperate void to test the values or catch the null exception. I just keep thinking there has to be a better way - i.e., one "slick" and simple line of code; but I can't seem to find it???

    H 1 Reply Last reply
    0
    • P pgraeve

      What is the BEST / HIGHEST PERFORMANCE method of reading data from a SqlDataReader and returning a "default" value if the value from the reader is null. I have several different methods that all work but all involve calling a seperate void to test the values or catch the null exception. I just keep thinking there has to be a better way - i.e., one "slick" and simple line of code; but I can't seem to find it???

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      if (value == null) return defaultValue;

      Don't rely on exceptions for this purpose because they are expensive to catch and thus decrease performance. Also, value types (like numeric types, booleans, DateTime, etc.) can't be null, so you'll already get their default value (0, false, etc.) or DBNull depending on how things are set up).

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups