locked
Does loading SqlDataReader data into object is possible in c# RRS feed

  • Question

  • User-826336654 posted

    Hi All,

    I want to know does loading SqlDataReader data into "default" object is possible? If yes, then how to make the code below to work.

    The code doesn't give any error but showing no data. 

            private object dtData()
            {
                
                object obj = new object();
    
                string connstring = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
    
                using (SqlConnection conn = new SqlConnection(connstring))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT ChildId, [Name], [Password], [DateOfBirth] FROM ChildInformation", conn))
                    {
                        conn.Open();
    
                        SqlDataReader reader = cmd.ExecuteReader();
    
                        obj = (object)reader;
                    }
                }
    
                return obj;
            }
    

    Thanks

    Sunday, January 14, 2018 3:20 PM

Answers

All replies

  • User541108374 posted

    Hi,

    give the dynamic keyword a try instead of object.

    On the other hand, what's the purpose of you trying this? Likely there's a better way.

    I would suggest you make a custom class and fill up that while looping over the reader. This custom class would have the needed properties like this:

    public class ChildInformation
    {
        public int ChildId {get;set;}
        public string Name {get;set;}
        public string Password {get;set;}
        public DateTime DateOfBirth {get;set;}
    }

    Kris.

    Monday, January 15, 2018 9:28 AM
  • User-707554951 posted

    Hi adamturner34

    Check the following article about Generically Populate List of Objects from SqlDataReader

    https://www.codeproject.com/Articles/827984/Generically-Populate-List-of-Objects-from-SqlDataR

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 15, 2018 9:40 AM
  • User753101303 posted

    Hi,

    Or if you want to use a DataReader you would have likely to keep it open and using an option to close the connection when the reader is closed. You are binding to the DataSource property of a control ? (so that we can create a quick test if you need it).

    Another option is to use "Entity Framework".

    Monday, January 15, 2018 11:59 AM
  • User1120430333 posted

    Another option is to use the DTO pattern with using ADO.NET and a datareader.

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Monday, January 15, 2018 1:20 PM
  • User-826336654 posted


    <g class="gr_ gr_20 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="20" data-gr-id="20">You are</g> binding to the DataSource property of a <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="7" data-gr-id="7">control ?</g> (so that we can create a quick test if you need it).

    Hi <g class="gr_ gr_69 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="69" data-gr-id="69">PatriceSc</g>,

    Can you please give <g class="gr_ gr_167 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="167" data-gr-id="167">quick test</g>, thanks.

    Monday, January 15, 2018 1:47 PM