我们定期需要导入如下所示的 CSV:
Name,SpecID,TestResult1,TestResult2,TestResult3
Alex,ASD123,3.23,452.2,232
Craig,DFG444,453.56,345.3,23
数据以这种方式存储:
SPECIMENTABLE (name,specid,SPECIMENTABLEID)
Alex,ASD123,1
Craig,DFG444,2
和
RESULTTABLE (testresult,result,SPECIMENTABLEID)
TestResult1,3.23,1
TestResult2,452.2,1
TestResult3,232,1
TestResult1, 453.56,2
etc
我像这样转储数据:
public void DumpQuickLabDump()
{
// T-SQL Connection
string connection = "Data Source=gaia;Initial Catalog=SalesDWH;Integrated Security=True";
// Get the data into the DataTable
//dtData = GetData(...);
// Create an object of SqlBulkCopy
SqlBulkCopy objSBC = new SqlBulkCopy(connection);
// Specify the destination table
objSBC.BulkCopyTimeout = 0;
objSBC.BatchSize = 10000;
objSBC.DestinationTableName = "SpecimenTable";
// Write the data to the SQL Server
objSBC.WriteToServer(QuickLabDump);
}
public void DumpTestResults()
{
// T-SQL Connection
string connection = "Data Source=gaia;Initial Catalog=SalesDWH;Integrated Security=True";
// Get the data into the DataTable
//dtData = GetData(...);
// Create an object of SqlBulkCopy
SqlBulkCopy objSBC = new SqlBulkCopy(connection);
// Specify the destination table
objSBC.BulkCopyTimeout = 0;
objSBC.BatchSize = 10000;
objSBC.DestinationTableName = "ResultTable";
// Write the data to the SQL Server
objSBC.WriteToServer(TestResults);
}
有时客户会提交一个 CSV 文件给我上传,然后几天后,他们会导入另一个 CSV,但其中会有一定比例的相同记录。
如何避免重复数据? (请记住,两个表是从一个 CSV 文件中填充到数据库中的)
解决方案可以是 .NET 或 sql。
非常感谢
最佳答案
您不能直接使用 SqlBulkCopy
做您想做的事。但是,您可以将行批量复制到工作表中,然后使用 MERGE
更新或插入语句。
但这确实需要您的源信息具有足够的信息来唯一标识每一行。
例如,假设 SpecimenTable
是 objSBC.DestinationTableName
,它被设置为 StagingSpecimenTable
。 StagingSpecimenTable 是 SpecimenTable 结构的副本。然后在批量复制之后,您可以使用 SqlCommand 执行此语句
MERGE SpecimenTable AS target
USING (SELECT name,specid FROM StagingSpecimenTable)
AS source (StagingSpecimenTable)
ON ( target.specid = source.specid )
WHEN MATCHED
THEN UPDATE SET target.mame= source.name
WHEN NOT MATCHED
THEN INSERT (name, specid )
VALUES (source.name, source.specid )
然后您必须删除或截断 StagingSpecimenTable 以及对 ResultTable 的类似操作
关于c# - 导入唯一的数据 - c#/sql server 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8719545/