0

I have a C# console app that needs to insert/update records. At this time, these records are represented by a C# object that looks like this:

public class Record
{
    public Guid Id { get; set; }

    public string Name { get; set; }

    public int Count { get; set; }

    public void Save()
    {

    }
}

In my C# code, I have a loop that goes through a List<Record>. This loop updates the Count if a Record with a specific Id exists. If a Record with the specific Id does not exist, I want to add a new Record. I might have hundreds of Record objects that need to get saved to the database.

Currently, my app is using the System.Data.SqlClient. I know how to insert / update records one-at-a-time. However, since I'm saving hundreds of Record objects, this approach is slow. I'd like to batch them together and run the SQL once to speed things up.

How do I do that from C# with System.Data.SqlClient?

Fábio Nascimento
  • 2,644
  • 1
  • 21
  • 27
Some User
  • 5,257
  • 13
  • 51
  • 93
  • Sounds like a very common task to me that should have thousands of high quality hits on Google. – Uwe Keim Jan 08 '19 at 15:01
  • Tried Googling for 'SQL transactions' ? – Grim Jan 08 '19 at 15:01
  • This may give you what you are looking for: [merge](https://stackoverflow.com/questions/1197733/does-sql-server-offer-anything-like-mysqls-on-duplicate-key-update)] – Terry Tyson Jan 08 '19 at 15:05

0 Answers0