Click here to Skip to main content
13,090,299 members (76,349 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i want to check record in DB, if that record is present in DB then just update , if not present then insert thst ,, plz suggest me through c# code
Posted 10-May-13 17:28pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


For checking record form db you can use SlqDataReader if you in SQL Server platform, and for update you can use ExecuteNonQuery method in SqlCommand object for example:

public class Class1
        public void Execute()
            SqlConnection readerconn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            readerconn .Open();
            SqlCommand cmd = new SqlCommand("SELECT ID , Date FROM Table1", readerconn );
            SqlDataReader reader = cmd.ExecuteReader();
            SqlConnection writerconn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            writerconn .Open();
            SqlCommand writerCommand = new SqlCommand("", writerconn );
            while (reader.Read()) // here we are checking record is existing or no
                int ID = reader.GetInt32(0);
                DateTime Date = reader.GetDateTime(1);
                 //here we want to update record ;)
                writerCommand.CommandText = "UPDATE Table1 SET Date = '" + EventDate.AddDays(1).ToString() + "' WHERE ID = " + ID.ToString();

If you want get more information about Update,Delete,Insert and Reading record with ADO.NET please refer here:
Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]

Best Regards.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There are multiple ways to do that. Here are I am specifying two.

First Approach:

1) First you check the database using select query if the target record is already present in the database or not.

2) If the record is present then execute update query. Otherwise, execute create query.

Here, the main drawback is to execute two queries for the such operation - one for select, another for insert/update. If your database is not in the same server where business logic is running, two sql round trips are to be performed. From the performance point of view this is not a good approach.

Second Approach:

1) Add suitable unique/primary key constraint in the table where you are planing to update/insert. This will restrict from inserting multiple rows with same information in these columns.

2) Write two queries one after another. First update query, then insert query for the same table. When the command will be executed, update query would run first, then insert query.

3) Now two possibilities:

a) Record is already present in the database : In this case, 1st query (update) would execute and 2nd query (insert) will throw exception since record is already there. This is a specific exception like 'unique/primary key violation'. So, you may easily capture that and ignore because intended update on the existing record is done.

b) Record is not present in the database : In this case, 1st query (update) won't affect anything since record is not present in the database. On the other hand, 2nd query would be executed and new row would be added.

So, we are solving the same issue using one set of queries (one round trip). Try to convince yourself that if we change the order of these two queries, this wont work expectedly during update.

Let me know if you anyone has worked on some other better method.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100