Skip some columns in SqlBulkCopy

前端 未结 4 2022
广开言路
广开言路 2021-02-07 15:58

I\'m using SqlBulkCopy against two SQL Server 2008 with different sets of columns (going to move some data from prod server to dev). So wa

4条回答
  •  一向
    一向 (楼主)
    2021-02-07 16:54

    Ed Harper, this is what it looks like without pseudo code (in this case from DataTable dt (fully defined) to an existing table in the db:

    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString))
    {
        bulkCopy.DestinationTableName = "dbo.DepartmentsItems";
    
        // Write from the source to the destination.
        foreach (DataColumn c in dt.Columns)
        {
            bulkCopy.ColumnMappings.Add(c.ColumnName, c.ColumnName);
        }
    
        bulkCopy.WriteToServer(dt);
        return dt.Rows.Count;
    }
    

提交回复
热议问题