Inserting a new record to SQL DB using dataset RRS feed

  • Question

  • Hi All,

    I have a DB for contacts, and I have a button to update the contacts table (contacts will be read from the phone), what I need to do is the following just when I click the update button:

    1 - I want to see all the new contacts that are not stored in the SQL DB.
    2 - I want to see all the contacts that are in the SQL DB but not in the phone contacts list.

    How can I do that? all of the above because I don't want to duplicate the records that are stored originally in the SQL DB.

    My Application written in C#.NET, and I'm using the DataSet Object.

    Waiting your help.
    Sunday, December 6, 2009 5:32 AM


  • Hello,

    If we can compare the data row according to their primary key, LINQ to DataSet can help us to achieve what you want. 

    Here I assume we have got the DataTable from the DB and DataTable from the phone:
    MyComparer comparer = new MyComparer();

    var rowInBothDBandPhone = dtDB.AsEnumerable().Intersect(dtPhone.AsEnumerable(), comparer);

    var rowInPhoneButNotInDB = dtPhone.AsEnumerable().Except(rowInBothDBandPhone);

    var rowInDBButNotInPhone = dtDB.AsEnumerable().Except(rowInBothDBandPhone);


        public class MyComparer : IEqualityComparer<DataRow>


            public bool Equals(DataRow r1, DataRow r2)


                if (r1.Field<int>("PK") == r2.Field<int>("PK"))


                    return true;




                    return false;




            public int GetHashCode(DataRow r)


                return r.Field<int>("PK").GetHashCode();



    The MyComparer class can be implemented according to your own demand.

    For more references about LINQ to DataSet, please see


    Have a nice day!

    Best Regards,
    Lingzhi Sun

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, December 9, 2009 9:06 AM