2012-06-18 26 views
5

Tôi cố gắng để chèn một danh sách chung để SQL Server với SqlBulkCopy,SqlBulkCopy đậm đà bản sắc Insert trong bảng điểm đến

Và tôi gặp khó khăn khi hóm hỉnh nhận dạng Dòng

tôi wan t bảng điểm đến của tôi để tạo ra lĩnh vực nhận dạng Làm thế nào tôi nên xử lý này, đây là mã của tôi

using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
       { 
        bulkCopy.BatchSize = (int)DetailLines; 
        bulkCopy.DestinationTableName = "dbo.tMyTable"; 

        var table = new DataTable(); 
        var props = TypeDescriptor.GetProperties(typeof(tBFFormularyStatusList)) 
         //Dirty hack to make sure we only have system data types 
         //i.e. filter out the relationships/collections 
               .Cast<PropertyDescriptor>() 
               .Where(propertyInfo => propertyInfo.PropertyType.Namespace.Equals("System")) 
               .ToArray(); 
        foreach (var propertyInfo in props) 
        { 
         bulkCopy.ColumnMappings.Add(propertyInfo.Name, propertyInfo.Name); 
         table.Columns.Add(propertyInfo.Name, Nullable.GetUnderlyingType(propertyInfo.PropertyType) ?? propertyInfo.PropertyType); 
        } 

        var values = new object[props.Length]; 
        foreach (var item in myGenericList) 
        { 
         for (var i = 0; i < values.Length; i++) 
         { 
          values[i] = props[i].GetValue(item); 
         } 

         table.Rows.Add(values); 
        } 

        bulkCopy.WriteToServer(table); 
       } 

ngoại lệ

Property accessor 'ID' on object 'ProcessFlatFiles.DetailsClass' threw the following exception:'Object does not match target type.' 

Tôi cũng đã cố gắng

using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepIdentity)) 
       { 

Trả lời

13

Cuối cùng tôi đã làm việc này theo cách này

using (var bulkCopy = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString, SqlBulkCopyOptions.KeepNulls & SqlBulkCopyOptions.KeepIdentity)) 
       { 
        bulkCopy.BatchSize = (int)DetailLines; 
        bulkCopy.DestinationTableName = "dbo.myTable"; 
        bulkCopy.ColumnMappings.Clear(); 
        bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName"); 
        bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName"); 
        bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName"); 
        bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName"); 
        . 
        . 
        . 
        . 
        bulkCopy.ColumnMappings.Add("SourceColumnName", "DestinationColumnName"); 

        bulkCopy.WriteToServer(datatable); 
       } 
+0

Bài đăng này đã tạo ngày của tôi =) – Schuere

0

Tôi biết đây là một câu hỏi cũ, nhưng tôi nghĩ rằng nó là giá trị bổ sung thay thế này: (Nếu bạn đã có lược đồ chính xác, có thể bỏ qua 1,2,3)

  1. Thực hiện đơn giản TOP 1 chọn từ bảng để trả về giá trị dữ liệu thứ i schema bảng điểm của phương pháp Clone
  2. Sử dụng DataTable để tạo ra một DataTable với cùng một lược đồ và không có dữ liệu
  3. Chèn dữ liệu vào bảng này
  4. Thực hiện SqlBulkCopy của WriteToServer (nếu đơn đặt hàng cột match thì giá trị bản sắc có thể được đọc . Nếu tùy chọn không được cung cấp trong hàm tạo của SqlBulkCopy thì mặc định là bỏ qua các giá trị này và để cho đích cung cấp chúng).

Điểm quan trọng là nếu bạn có các cột theo đúng thứ tự (bao gồm cả cột nhận dạng), mọi thứ được xử lý cho bạn.