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

时间:2013-02-28 08:49:46

标签: c# sqlbulkcopy

我有两个数据库现在我必须将数据从一个数据库插入到另一个数据库循环中。我知道有点sql批量复制,我不知道如何使用sql批量复制一次插入一个数据。

这是我的表结构

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

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

我可以使用sqlbulk copy class处理从一个db到另一个db的数据插入。请建议。感谢

2 个答案:

答案 0 :(得分:1)

这很简单啊:)对不起我在VB.net的代码我希望你不介意

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

  1. 从db1获取数据并执行您想要执行的任何操作并准备好数据表中的数据
  2. 使用此功能将数据转储到第二个数据库
  3. 代码:

      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
    

答案 1 :(得分:0)

您不需要逐个迭代数据插入,您只需将集合传递给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();
        }