What is the matter with this code?

This code produces no errors, but it does not do what I want. It's supposed to check one table for two field values and add them to another table if both values do not already exist within a record of the other table. I'm definitely doing something wrong, but I'm surprised it doesn't bomb.
SqlConnection myConnection = new SqlConnection("server=localhost;" +
                                            "Trusted_Connection=yes;" +
                                            "database=mydb;" +
                                            "connection timeout=30;" +
                                            "MultipleActiveResultSets=True");
            myConnection.Open();
SqlDataReader AReader = null;
                SqlDataReader TReader = null;
                SqlCommand checkA = new SqlCommand("Select PhoneNumber, Address FROM Addresses", myConnection);
                SqlCommand checkT = new SqlCommand("Select PhoneNumber, Address FROM Temp Where Billable = 1", myConnection);
                
                AReader = checkA.ExecuteReader();
                TReader = checkT.ExecuteReader();
                
                
                while (TReader.Read())
                {
                    while (AReader.Read())
                    {
                        if (TReader["PhoneNumber"] != AReader["PhoneNumber"] || TReader["Address"] != AReader["Address"] )
                        {
                            SqlCommand insertA = new SqlCommand("INSERT INTO Addresses (PhoneNumber, Address) Values ('" + TReader["PhoneNumber"] + "', '" + TReader["Address"] + "')", myConnection);
                            insertA.ExecuteNonQuery();
                        }
                    }
                }

Open in new window

LVL 9
BobHavertyComhAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Have you stepped into the code ?
The most obvious reason nothing gets inserted is that there is nothing to insert.
0
Rgonzo1971Commented:
Hi,

you write

 if (TReader["PhoneNumber"] != AReader["PhoneNumber"] || TReader["Address"] != AReader["Address"] )

Do you mean  
if (TReader["PhoneNumber"] != AReader["PhoneNumber"] && TReader["Address"] != AReader["Address"] )

Regards
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why don't you do it in one step by using a single T-SQL command?
SqlConnection myConnection = new SqlConnection("server=localhost;" +
                                            "Trusted_Connection=yes;" +
                                            "database=mydb;" +
                                            "connection timeout=30;" +
                                            "MultipleActiveResultSets=True");
            myConnection.Open();

SqlCommand insertA = new SqlCommand("INSERT INTO Addresses (PhoneNumber, Address)
SELECT PhoneNumber, Address 
FROM Temp 
WHERE Billable = 1
   AND NOT EXISTS (SELECT 1 
                     FROM Addresses
                     WHERE Addresses.PhoneNumber = Temp.PhoneNumber
                                 OR Addresses.Address =Temp.Address)", myConnection);

insertA.ExecuteNonQuery();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BobHavertyComhAuthor Commented:
Absolutely EXACTLY what I was looking for. I saw statements such as this and I like that it's pure SQL. I just couldn't quite figure out how to apply it to my situation and you did that perfectly. Wish I could give you more than "A" 500 points. Thanks again.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Glad I could help.
Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.