简体   繁体   English

C#BatchCopy外键

[英]c# batchcopy foreign key

I'm reading .txt files and inserting its data into datarows, these datarows are combined in a dataview and need to get pushed to a SQL database to a specific table, but it's lacking a foreign key and I don't know how to get it of another table. 我正在读取.txt文件并将其数据插入数据行,这些数据行被组合在一个数据视图中,需要被推送到一个SQL数据库到一个特定的表,但是它缺少一个外键而且我不知道如何获取它在另一张桌子上。

The fields of the DataTable.Row should be stored in the [Fouten] table. DataTable.Row的字段应存储在[Fouten]表中。 These results need to be linked to the first table [Treinen] by the TreinId that's inserted as foreign key in the [Fouten] table. 这些结果需要通过作为外键插入到[Fouten]表中的TreinId链接到第一个表[Treinen]。

But how can I retrieve this Id that I need to insert into the [Fouten] table that's available in the [Treinen] table? 但是,如何获取需要插入[Treinen]表中可用的[Fouten]表中的ID?

Or should I use a different method to push a bunch of rows at the same time to the SQL database? 或者我应该使用不同的方法同时将一堆行推送到SQL数据库?

Maybe I can execute a stored procedure from here? 也许我可以从这里执行存储过程? that woudlve solve it too? 那个问题也解决了吗?

dt.Rows.Add(new object[] { datum, foutcode, omschrijving, module[module.Length - 1], tijd, teller, absentOfPresent});


using (SqlBulkCopy sbc = new SqlBulkCopy(GetConnectionString(), SqlBulkCopyOptions.KeepIdentity))
{
    sbc.DestinationTableName = "dbo.Fouten";
    sbc.BatchSize = 8000;

    sbc.ColumnMappings.Add("Datum", "Datum");
    sbc.ColumnMappings.Add("Foutcode", "FoutCode");
    sbc.ColumnMappings.Add("Omschrijving", "Omschrijving");
    sbc.ColumnMappings.Add("Module", "Module");
    sbc.ColumnMappings.Add("Time", "Time");
    sbc.ColumnMappings.Add("Teller", "Teller");
    sbc.ColumnMappings.Add("Mnemo", "Mnemo");
    //Retrieve TreinId from other table
    //Select TreinId Where Name = '1302';
    sbc.ColumnMappings.Add("??????", "TreinId");

    sbc.WriteToServer(dtInsertRows);
  }

在此输入图像描述

I don't think you can do the lookup at the same time as the bulk copy. 我认为您不能与批量复制同时进行查找。 Two other options: 另外两个选择:

1) Bulk copy the file data into a TEMPORARY table and then execute a SELECT..INTO query that joins the temporary table and the "TREINEN" table and inserts the data (with the foreign key) into the "FOUTEN" table 1)将文件数据批量复制到TEMPORARY表中,然后执行SELECT..INTO查询,该查询将临时表和“ TREINEN”表连接在一起,并将数据(带有外键)插入“ FOUTEN”表中

2) Perform a LINQ query in C# with the data table you already have joined with a second data table containing the data from "TREINEN", giving you a new data table that has the foreign key, and then copy that into the Fouten table 2)在C#中使用已经连接了第二个数据表的LINQ查询,其中第二个数据表包含来自“ TREINEN”的数据,为您提供一个具有外键的新数据表,然后将其复制到Fouten表中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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