简体   繁体   中英

Bulk inserting with two related tables from Excel

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:

SiteID

ContactName

Email

Phone

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM