none
SqlDataReader avoiding boxing and unboxing RRS feed

  • Question

  • I am trying to create a clean solution which can generate variables based on what is read in from a SqlDataReader.  Since SqlDataReader contains a function SqlDataReader.GetFieldType(), in therory I know the type of the fields and can call the appropriate SqlDataReader.GetX() method.

    What I am not understanding is how to create a variable dynamically.  I want to be able to implement some code where someone can modify some xml to do a query and then I want to give the person a way through xml to access the data.  What I do not want to do is store everything as type object or create a custom class each time I implement the code.

    Any ideas?

    • Moved by Leo Liu - MSFT Monday, February 6, 2012 7:06 AM Moved for better support. (From:Visual C# General)
    Sunday, February 5, 2012 12:27 AM

Answers

  • Hi Mike Dovell,

    DataReader doesn't know the type of the record it read, so it always return an object type. If you want it to return a specific type of value, I suggest you to use it's GetXXX method, DataReader has lots of GetXXX method which can return the specific type.

    More information about it, please refer here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 29, 2012 9:48 AM
    Moderator

All replies

  • Do you need more info or is there no way to do what I am asking?  I just don't think that creating a class model of the database each time I re-use my code is a viable option.  I just don't have any idea where to look for what I am asking.
    Sunday, February 5, 2012 2:12 AM
  • *Bump
    Sunday, February 5, 2012 3:20 PM
  • Hi Mike,

    I am moving your thread into the  ADO.NET Managed Providers Forum for dedicated support.
    Have a nice day, 
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, February 6, 2012 7:06 AM
  • Hi Bump,

    Welcome to MSDN Forum.

    Do you mean you want to query out datas from database through SqlDataReader and dynamically create a variable to store the data? I'm not clear about the XML file you mentioned, do you want to query out the datas and create a xml to store them? If I misunderstand, please feel free to correct me.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, February 6, 2012 7:24 AM
    Moderator
  • Hi Bump,

    Have you sovled the issue?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, February 9, 2012 1:59 AM
    Moderator
  • No, I eventually just treated everything as string.
    Sunday, February 26, 2012 4:45 PM
  • Hi Mike,

    I'm not clear about the question, could you please clarify more clearly? What you treated as string?

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Monday, February 27, 2012 8:18 AM
    Moderator
  • What I was trying to do is use the data reader to pull down a sql record and then to avoid boing and unboxing by using the SQL types in C# that match the SQL server types.  I eventually just called the getstring methods and treated all fields as string.

    With the original question, what I was trying to do is use reflection and  to generate a class "x" based on the sql record field definition.  Instead of creating a class to match the variables in sql server.

    I wanted this to be a re-useable solution which did not have to rely on always creating a class definition to match a table definition.


    • Edited by Mike Dovell Wednesday, February 29, 2012 1:00 AM
    Wednesday, February 29, 2012 12:59 AM
  • Hi Mike Dovell,

    DataReader doesn't know the type of the record it read, so it always return an object type. If you want it to return a specific type of value, I suggest you to use it's GetXXX method, DataReader has lots of GetXXX method which can return the specific type.

    More information about it, please refer here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, February 29, 2012 9:48 AM
    Moderator