The .NET Upsert Nuget package is for combined insert and update operations of strongly typed collections into SQL Server.
Install-Package Upsert -Version 1.0.1
Click here for the .NET Upsert Nuget page.
CREATE SCHEMA Sales;
CREATE TABLE Sales.Customer (
CustomerID INT NOT NULL PRIMARY KEY,
Name VARCHAR(20) NOT NULL,
City VARCHAR(20) NOT NULL,
State CHAR(2) NOT NULL
);
INSERT INTO Sales.Customer (CustomerID, Name, City, State)
VALUES (1, 'Joe Smith', 'Philadelphia', 'PA'),
(2, 'Mary Jones', 'New York', 'NY'),
(3, 'Mike Andersen', 'Raleigh', 'NC');
public class Customer
{
public int CustomerID {get;set;}
public string Name {get;set;}
public string City {get;set;}
public string State {get;set;}
}
using Spearing.Utilities.Data.Upsert;
string connStr = @"Data Source=ServerName;Initial Catalog=DatabaseName;User Id=SomeUser; Password=password1;";
List customers = new List()
{
new Customer() { CustomerID = 1, Name = "Joseph Smith", City = "Philadelphia", State = "PA" },
new Customer() { CustomerID = 4, Name = "Jane West", City = "Denver", State = "CO" }
};
customers.Upsert("Sales", "Customer", connStr);
string connStr = @"Data Source=ServerName;Initial Catalog=DatabaseName;User Id=SomeUser; Password=password1;";
List customers = new List()
{
new Customer() { CustomerID = 1, Name = "J. Smith", City = "Philly", State = "XX" },
new Customer() { CustomerID = 5, Name = "Terry Jenkins", City = "Los Angeles", State = "CA" }
};
customers.Upsert("Sales", "Customer", connStr, "Name", "State");