I need to import data from an excel sheet into a database using C# WinForms. Each row is an 'Electricity Site' with a few columns of data going into the 'Sites' table. Each site also has a 'Contact' with its information (Name, Email, Phone), but that Contact may or may not exist already in the database. That contacts primary key (identity field) is a foreign key in the Sites table but the value is not known at runtime.
Here is some of the relevant data that the Excel file contains:
Here are the tables:
Contacts ( ContactID (identity) ContactName Email Phone ) Sites ( SiteKey (identity) SiteID ContactID (foreign key) )
If that contacts name + email + phone doesnt already exist in the table then it needs to be created and that new identity is needed to be inserted into that row for Sites (if Contact exists already then that ContactID is needed).
What would be the best way to approach this and get the fastest results. In most cases 1000+ rows are in the Excel sheet.
The easiest way is probably to bulkload the two excel files into staging tables with sql bulkcopy and then use a couple of "
insert into" statements that lookup the proper foreign key into your real tables.
However, since a 1000+ rows is not a lot (depeding on the + of course), so you could just load them both into memory and insert directly with proper keys into your target database.