Need help to update records into the database

I am writing a console app to insert and update records from a file  into a database. I am able to insert but not sure on updating the exiting records. code below:

string sql = "INSERT INTO sc_lienholder(lienholder_code,name,address,address2,city,state,zipcode,elt_status,is_active,insert_dtim,insert_person) VALUES(@lienholder_code,@name,@address,@address2,@city,@state,@zipcode,@elt_status,@is_active,@insert_dtim,@insert_person)";
 using (SqlCommand cmd = new SqlCommand(sql, cn))
                        for (int index = 0; index < list.Count; index++)
                                string[] items = list[index].Substring(53).Split(new char[] { ',' });
                                string item = list[index].Substring(0, 51);
                                cmd.Parameters.AddWithValue("@name", item.Replace("'", ""));
                                cmd.Parameters.AddWithValue("@address", items[4]);
                                cmd.Parameters.AddWithValue("@address2", items[5]);
                                cmd.Parameters.AddWithValue("@city", items[6]);
                                cmd.Parameters.AddWithValue("@state", items[7]);
                                cmd.Parameters.AddWithValue("@zipcode", items[8]);
                                cmd.Parameters.AddWithValue("@lienholder_code", items[9].Trim()); //sf SC-77 03/09/2015
                                if (items[10].Trim() == "E") //sf SC-77 03/23/2015
                                    cmd.Parameters.AddWithValue("@elt_status", "Y"); //sf SC-77 03/23/2015
                                    cmd.Parameters.AddWithValue("@elt_status", "N"); //sf SC-77 03/23/2015
                                cmd.Parameters.AddWithValue("@is_active", "Y");
                                cmd.Parameters.AddWithValue("@insert_dtim", DateTime.Now);
                                cmd.Parameters.AddWithValue("@insert_person", "Batch Job");
                                Logger.Instance.LogEvent(String.Format("{0} lienholders copied", index));
                            catch (Exception ex)
                                Logger.Instance.LogException(String.Format("Exception: {0}", ex.Message), ex);
                                Emailer.SendMail(ex.Message, "Error in South Carolina Lien Console Application");
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Walter RitzelSenior Software EngineerCommented:
The code will need to have some changes:
1) You will need to create the update statement in the same way you have created the insert: UPDATE <table name> SET <field1> = @field1, ... WHERE <field key> = @value;
2) You will need to identify your primary key;
3) for each line read from file, you will need to query the database to check if it exists. If it not exists, use your insert command; if it exists, use your update command.

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
MeinhoonaaAuthor Commented:
how do I do that? need example. I know the theory.
MeinhoonaaAuthor Commented:
I can take care of update statement. Need help with item # 3.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

MeinhoonaaAuthor Commented:
string sqlUpdate = "UPDATE  sc_lienholder SET name= @name, address = @address, address2 = @address2, city  = @city, state = @state, zipcode = @zipcode, elt_status = @elt_status, is_active = @is_active, where lienholder_code = @lienholder_code";
MeinhoonaaAuthor Commented:
for some reason if exist query not working.

string sqlcheck = "select * FROM sc_lienholder WHERE lienholder_code = @lienholder_code";
Vitor MontalvãoMSSQL Senior EngineerCommented:
Your UPDATE statement looks good to me.
You don't need to check before since the UPDATE query has the WHERE clause so it only update the records  with the criteria lienholder_code = @lienholder_code.
Walter RitzelSenior Software EngineerCommented:
Check SQL seems ok to me. Please post your full code for us to analyze.
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

From novice to tech pro — start learning today.