Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:55:33+00:00 2026-05-26T14:55:33+00:00

I have an application that reads and writes to table in SQL Server 2005.

  • 0

I have an application that reads and writes to table in SQL Server 2005. I know the basics of reading and writing to a table.

If there is no Id then I create a new entry. If there is an Id I update the existing entry.

What is the best way to write the SqlCommand string based upon if it already exists or is new?

I can’t see writing two methods when it would then take updating two places when it came time for changes.

Any tips or tricks would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:55:34+00:00Added an answer on May 26, 2026 at 2:55 pm

    Here is full sample of working code you need , all you need to do is check if record already exists then update it or just insert it.

    create table MyEntity(id int,val varchar(20))
    Go
    create proc SaveMyEntity(@id int, @val varchar(20))
    as
    If Exists (Select ID from MyEntity where Id = @ID)
    Update MyEntity
           Set val = @val
           Where ID = @ID
    else
    Insert into MyEntity values(@ID,@val)
    
    -- here is proof of insertion/updation
    
    Go 
    SaveMyEntity 1,'a'
    Go
    SaveMyEntity 1,'b'
    Go
    SaveMyEntity 2,'a'
    Go
    select * from myEntity
    

    Results will be

     id val
    1   b
    2   a
    

    Then you can use this stored proc from your SqlCommand in C# (recommended)

    Or your can just use this SQL statement from your C# SqlCommand (not recommended and tested)

    If Exists (Select ID from MyEntity where Id = @ID)
    Update MyEntity
           Set val = @val
           Where ID = @ID
    else
    Insert into MyEntity values(@ID,@val)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that reads a table from a database. I issue an
I have a SQL Server 2008 database with a table that contains a FILESTREAM
We have an application which stores its data in SQL Server. Each table has
I have an application that reads a CSV file with piles of data rows.
I have an application that reads a database and outputs alerts to any dependencies
I have an application that reads lines from a file and runs its magic
I have an application that reads data from a com port using javax.comm. The
I have an iPhone application that reads RSS feeds from a YouTube channel. However,
I have a Silverlight application that reads its content from an XML file. The
I have a windows forms application that reads and updates an XML file with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.