none
Table Adapters and Data Sets RRS feed

  • Question

  • Hello,

    A coworker is implementing a WCF SQL connection service layer as follows:

    He defined a DataContract.  He then did the following:

    Create a  DataTableAdapter to pull a DataSet and then iterates through the Data Set to populate a List of DataContracts that he returns.

    Basically, he's using a DataSet to populate a list that he returns.  I find that very inefficient.

    I told him that he should just go directly to the SqlDataReader and populate the DataContract List as that would be extremely more efficient.

    Any guidance would be appreciated.

    Thanks in advance.




    • Edited by Hiline1961 Thursday, November 27, 2014 4:32 AM
    Thursday, November 27, 2014 4:26 AM

Answers

  • Hi Hiline1961,

    >>I told him that he should just go directly to the SqlDataReader and populate the DataContract List as that would be extremely more efficient.

    Yes, I agree with you.

    For example first we have defined the following DataContract:

        [DataContract]
        public class Test
         {
            [DataMember]
            public string Name
            {
                get;
                set;
            }
    
            [DataMember]
            public string ID        
           {
                get;
                set;
            }
        }

    Then we can use the SqlDataReader and bind the the data to the DataContract List as following:

    List<Test> Testlist =new List<Test>();

    ......

    using (SqlDataReader sqlReader = cmd.ExecuteReader())
      {

         if (sqlReader.HasRows)
        {
           while (sqlReader.Read())
           {
                 Test TestDome = new Test();
                 TestDome.Name = sqlReader["name"].ToString();
                 TestDome.ID = sqlReader["id"].ToString();
                 Testlist.Add(TestDome);
           }
        }
        return Testlist;

    }

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Hiline1961 Monday, December 1, 2014 11:26 PM
    Friday, November 28, 2014 5:45 AM
    Moderator