使用Fastmember批量插入仅选定的列

时间:2016-07-19 12:28:46

标签: c# asp.net sql-server fastmember

我有一个应用程序,用于从文件向具有多个计算列的表中插入大量数据(每个文件最多约250,000条记录)。有没有办法选择fastmember插入数据的哪些列,所以我不尝试写入计算列?

2 个答案:

答案 0 :(得分:1)

npm install leaflet --save

答案 1 :(得分:0)

模特课:

class ExampleModel
{
    public int property1 { get; set; }
    public string property2 { get; set; }
    public string property3 { get; set; }       
}

型号列表:

private List<ExampleModel> listOfObject = new List<ExampleModel>()
{
    new ExampleModel { property1 = 1, property2 = 'Rudra',  property3 = 'M'},
    new ExampleModel { property1 = 2, property2 = 'Shivam', property3 = 'M'}
};

使用Fastmember批量插入列映射:

  using (var bcp = new SqlBulkCopy(SQLConnectionString))
  using (var reader = ObjectReader.Create(listOfObject))
  {
      bcp.DestinationTableName = "[dbo].[tablename]";
      bcp.ColumnMappings.Add("property1", "tableCol1");
      bcp.ColumnMappings.Add("property2", "tableCol2");
      bcp.ColumnMappings.Add("property3", "tableCol3");
      bcp.WriteToServer(reader);
  }

记住:

使用标识字段插入数据时不要忘记使用KeepIdentity。

using (var bcp = new SqlBulkCopy(SQLConnectionString, SqlBulkCopyOptions.KeepIdentity))

使用自动增量标识字段插入数据,删除自动增量列映射字段。 like property1是数据库中的自动增量列,因此在插入数据时跳过此列。

      using (var bcp = new SqlBulkCopy(SQLConnectionString))
      using (var reader = ObjectReader.Create(listOfObject))
      {
          bcp.DestinationTableName = "[dbo].[tablename]";
          bcp.ColumnMappings.Add("property2", "tableCol2");
          bcp.ColumnMappings.Add("property3", "tableCol3");
          bcp.WriteToServer(reader);
      }