SQLBulkCopy with Identity Insert in destination table

天大地大妈咪最大 提交于 2019-12-10 14:30:46

问题


I am trying to insert a Generic list to SQL Server with SQLBulkCopy,

And i have trouble wit Identity Field

I wan t my destination table to generate identity field How should i handle this, here is my code

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);
                }

exception

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

I have also tried

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

回答1:


Finally I got this worked this way

    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);
                }



回答2:


I know this is an old question, but I thought it was worth adding this alternative: (If you already have correct schema, can skip 1,2,3)

  1. Perform a simple TOP 1 select from the table to return the a datatable with the destination table's schema
  2. Use DataTable's Clone method to generate a datatable with same schema and no data
  3. Insert your data into this table
  4. Perform SqlBulkCopy's WriteToServer (if column orders match, then the identity values can be read. If the option isn't provided in SqlBulkCopy's constructor then the default is to ignore these values and let the destination provide them).

The important point is that if you have the columns in the correct order (including identity columns), everything is handled for you.



来源:https://stackoverflow.com/questions/11084964/sqlbulkcopy-with-identity-insert-in-destination-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!