数据插入循环使用sql bulkcopy类c#

本文关键字:bulkcopy sql 插入 循环 数据 | 更新日期: 2023-09-27 18:01:36

我现在有两个数据库,我必须从一个db插入数据到另一个db在循环。我知道位SQL大容量复制,我不知道如何使用SQL大容量复制一次插入一个数据。

这是我的表结构

CREATE TABLE [CandidateApplication](
 [EmailID] [varchar](300) NOT NULL,
 [Name] [varchar](300) NULL,
 [FileName] [varchar](500) NULL,
 [IsDownloaded] [bit] NOT NULL
)

基本上我需要从一个db与子句像IsDownloaded=0获取数据,然后我需要在循环中迭代。在循环中,我将从FileName字段获取文件路径并下载该文件。如果文件下载成功,那么我必须将数据从db1保存到db2,并将字段IsDownloaded=1更新到db1。

我可以处理数据插入从一个数据库到另一个数据库与在循环使用sqlbulk复制类。请建议。由于

数据插入循环使用sql bulkcopy类c#

这是简单的家伙:)对不起,我的代码在VB.net我希望你不要介意它

注意:重要的是数据表和目标表中的数据类型。它们应该相同

  1. 从db1中获取数据,做任何你想做的事情,并在datatable中准备数据
  2. 使用此函数将数据转储到第二个数据库
代码:

  Public Shared Function BulkSave(ByVal dt As DataTable) As Boolean
    Dim mydb As New CSdatabase
    Try
    Dim connectionString = "Connection String"
    '' so there is no need to map columns. 
        Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(connectionString)
            bulkCopy.BatchSize = 25000
            bulkCopy.BulkCopyTimeout = 300
            bulkCopy.ColumnMappings.Add("EmailID", "EmailID")
            bulkCopy.ColumnMappings.Add("Name", "Name")
            bulkCopy.ColumnMappings.Add("FileName", "FileName")
            bulkCopy.ColumnMappings.Add("IsDownloaded", "IsDownloaded")
            bulkCopy.DestinationTableName = "dbo.CandidateApplication"
            bulkCopy.WriteToServer(dt)
        End Using
    Catch ex As Exception
        Throw ex
    Finally
        'mydb.closeConnection() ' Close your conneciton here
    End Try
    Return True
End Function

您不需要逐个遍历数据插入,您所需要做的就是将一个集合传递给WriteToServer()方法。

的一个例子是:

HashSet<SomeObject> dataLines = new HashSet<SomeObject>(); 
foreach (var entity in someCollection)
        {
            if(entity == somecondition)
            {
                  dataLines.Add(new SomeObject
                              {
                                  CollectionItem1 = entity.Property1,
                                  CollectionItem2 = entity.Property2,
                                  CollectionItem3 = entity.Property3,
                                  CollectionItem4 = entity.Property4,
                                  CollectionItem5 = entity.Property5,
                                  CollectionItem6 = entity.Property6,
                                  CollectionItem7 = entity.Property7,
                              });
            }
        }
        try
        {
            _context.Connection.Open();
            EntityConnection connection = _context.Connection as EntityConnection;
            SqlConnection sqlConnection = null;
            if (connection != null)
            {
                sqlConnection = connection.StoreConnection as SqlConnection;
            }
            if (sqlConnection != null)
            {
                SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnection);
                bulkInsert.DestinationTableName = "SomeTable";
                bulkInsert.ColumnMappings.Add("CollectionItem1", "Column1");
                bulkInsert.ColumnMappings.Add("CollectionItem2", "Column2");
                bulkInsert.ColumnMappings.Add("CollectionItem3", "Column3");
                bulkInsert.ColumnMappings.Add("CollectionItem4", "Column4");
                bulkInsert.ColumnMappings.Add("CollectionItem5", "Column5");
                bulkInsert.ColumnMappings.Add("CollectionItem6", "Column6");
                bulkInsert.ColumnMappings.Add("CollectionItem7", "Column7");
// dataLines is a collection of objects
                bulkInsert.WriteToServer(dataLines.AsDataReader());
                _context.SaveChanges();
            }
        }
        finally
        {
            _context.Connection.Close();
        }