需添加一个using System.Linq; 引用

public void BulkInsert<T>(string connection, string tableName, IList<T> list) { using (var bulkCopy = new SqlBulkCopy(connection)) { bulkCopy.BatchSize = list.Count; bulkCopy.DestinationTableName = tableName; var table = new DataTable(); var props = TypeDescriptor.GetProperties(typeof(T)) .Cast<PropertyDescriptor>() .Where(propertyInfo => propertyInfo.PropertyType != null && propertyInfo.PropertyType.Namespace != null && 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 list) { for (var i = 0; i < values.Length; i++) { values[i] = props[i].GetValue(item); } table.Rows.Add(values); } bulkCopy.WriteToServer(table); } }

 

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2022-02-04
  • 2021-09-15
  • 2021-07-21
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-17
  • 2021-07-05
  • 2022-12-23
  • 2021-06-01
相关资源
相似解决方案