1

I have an Excel file with up to 4000 rows and 10 columns of data, and I want to insert these records into the database.

Some columns are titles of another table of the database and I must find Id of them.

One of the columns is from table that has 2000 rows of data.

I have a foreach loop that checks the data for each row of the Excel file and obtains the foreign key.

I think I have two ways to get the Id:

  1. Query the database inside the loop and get the Id if that title exists in the second table, and if not, send a message to the user.
  2. Outside the loop, I retrieve the part of the second table that I need (including almost 1000 rows) and then search inside that list in the loop.

Which one is more optimal?

In each line, if the title is not in the second table or if that line has any problem, an error will be displayed to the user and no line will be inserted.

1
  • option 1 looks not good which would create too much db connections I think.
    – Tiny Wang
    Commented May 26, 2023 at 2:51

0

Browse other questions tagged or ask your own question.