简体   繁体   中英

Using SQLBulkCopy to Insert into Related Tables

I am using SQL Bulk copy to read data form Excel to SQL DB. In the Database, I have two tables into which I need to insert this data from Excel. Table A and Table B which uses the ID(primary Key IDENTITY) from Table A to insert corresponding row records into Table B .
I am able to insert into one table ( Table A ) using the following Code.

using (SqlConnection connection = new SqlConnection(strConnection)) {
    connection.Open();
    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connection)) {
        bulkCopy.DestinationTableName = "dbo.[EMPLOYEEINFO]";
        try {
            // Write from the source to the destination.
            SqlBulkCopyColumnMapping NameMap = new SqlBulkCopyColumnMapping(data.Columns[0].ColumnName, "EmployeeName");
            SqlBulkCopyColumnMapping GMap = new SqlBulkCopyColumnMapping(data.Columns[1].ColumnName, "Gender");
            SqlBulkCopyColumnMapping CMap = new SqlBulkCopyColumnMapping(data.Columns[2].ColumnName, "City");
            SqlBulkCopyColumnMapping AMap = new SqlBulkCopyColumnMapping(data.Columns[3].ColumnName, "HomeAddress");

            bulkCopy.ColumnMappings.Add(NameMap);
            bulkCopy.ColumnMappings.Add(GMap);
            bulkCopy.ColumnMappings.Add(CMap);
            bulkCopy.ColumnMappings.Add(AMap);

            bulkCopy.WriteToServer(data);
        }
        catch (Exception ex) {
            Console.WriteLine(ex.Message);
        }
    }
}

But then I am not sure how to extend it for two tables which are bound by Foreign Key relationship.Especially, Table B uses the Identity value from Table A Any example would be great. I googled it and none of the threads on SO couldn't give a Working example.

AFAIK bulk copy can only be used to upload into a single table. In order to achieve a bulk upload into two tables, you will therefore need two bulk uploads. Your problem comes from using a foreign key which is an identity. You can work around this, however. I am pretty sure that bulk copy uploads sequentially, which means that if you upload 1,000 records and the last record gets an ID of 10,197, then the ID of the first record is 9,198! So my recommendation would be to upload your first table, check the max id after the upload, deduct the number of records and work from there!

Of course in a high use database, someone might insert after you, so you would need to get the top id by selecting the record which matches your last one by other details (assuming a combination of (upto) all fields would be guaranteed to be unique). Only you know if this is likely to be a problem.

The alternative is not to use an identity column in the first place, but I presume you have no control over the design? In my younger days, I made the mistake of using identities, I never do now. They always find a way of coming back to bite!

For example to add the second data:

DataTable secondTable = new DataTable("SecondTable");
secondTable.Columns.Add("ForeignKey", typeof(int));
secondTable.Columns.Add("DataField", typeof(yourDataType));

...

Add data to secondTable.

(Depends on format of second data)

int cnt = 0;
foreach (var d in mySecondData)
{
    DataRow newRow = secondTable.NewRow();
    newRow["ForeignKey"] = cnt;
    newRow["DataField"] = d.YourData;
    secondTable.Rows.Add(newRow);
}

Then after you found out the starting identity (int startID).

for (int i = 0; i < secondTable.Rows.Count; i++)
{
    secondTable["ForeignKey"] = secondTable["ForeignKey"] + startID;
}

Finally:

bulkCopy.DestinationTableName = "YourSecondTable";
bulkCopy.WriteToServer(secondTable);

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