11 Comments


  • I do something very similar but I use Lambda's instead of the Convert stuff:

    public static T GetValue(this DbDataReader reader, string field, Func code)
    {
    T value = default(T);

    int ordinal = reader.GetOrdinal(field);

    if (!reader.IsDBNull(ordinal))
    value = code(ordinal);

    return (value);
    }

    and then you can do tasty stuff like this without the convert.

    address.ID = dr.GetValue("ID", i => dr.GetInt32(i));

    Now in my case I use DbDataReader and ordinal values instead of strings but the cool part is the lambda.

    I also have one that supports nullable types on structs:

    public static T? GetValueOrNull(this DbDataReader reader, string field, Func code) where T : struct
    {
    T? value = null;

    int ordinal = reader.GetOrdinal(field);

    if (!reader.IsDBNull(ordinal))
    value = code(ordinal);

    return (value);
    }

    I use ordinals because my data mapping layer get generated from CodeSmith and I can 100% count on the ordinals being in the correct order.

    I don't see this kind of thing done enough so good on ya for punting and refactoring.

    Cheers
    Rob

  • Doh, this version does use the string and gets the ordinal.

    Another version uses the ordinal for code gen'd stuff, sorry for the confusion. You get the idea though.

  • There's someone still writing code like this?

    Why?

    How many years ago was NHibernate released?

  • Second line of original code assumes that String.Empty will be used if reader returns DBNull type. Refactored code will return (string)null. Be careful :)

  • Rob, can you put the full class code up on this comments page?

  • Paul --

    Good catch. My team actually has several more overloads on this method, including one that has an additional parameter of type T that allows you to specify the desired "default value". In this case, you could call the code as follows:

    theatre_code = reader.GetValueOrDefault("theatre_code", String.Empty);

    Cheers,

    Scott

  • I was just searching for about this when I discovered your blog post. I’m only visiting to say that I truly enjoyed reading this post, it’s really well written. Are you going to blog more on this? It looks like there’s more material here for more posts.

  • At twenty years of age, the will reigns; at thirty, the wit; and at forty, the judgment.

  • I have really enjoyed browsing your posts. Thanks for sharing this information. Someone on Yahoo Answers referred me here and I love it.

  • Shot She,care during plate war itself surface appeal dream progress gold else sound half prove island hold friend for anything back finding link hurt some high football due behaviour strategy centre oil more deal whatever cut seem would visit age love water border alternative telephone woman connection must middle perform another movement him bar warm broad middle fall even suddenly discipline considerable identify collect market kill plastic good neither hill noise work one score programme attempt its for stick experience bank famous act united human user paint relation indeed absolutely fair science find control

  • The content is very rich, I love it.
    Thanks

Comments have been disabled for this content.