2012-06-26 26 views
1

我在SQl服務器中設置了許多表 我已將所有這些轉移到正常LinqToSql使用的dbml文件中。覆蓋IEnumerable以允許SqlBulkCopy更快轉移(在LinqToSql上)

我想知道是否有可能改變的代碼只有類選擇表的列,而不是鏈接表

也就是說,如果我想的類傳遞給方法,而不鏈接表性能我怎麼會去這樣做

更多信息,基本上我試圖使用擴展我已經寫了使用SqlBulkCopy的,而不是context.SubmitChanges但碰到映射問題,由於額外的屬性

我意識到我可以使用匿名類型,但認爲會覆蓋Ext的任何好處外掛方法。 另一種選擇是更改ToDataTable擴展。

 public static void SqlBulkInsert<T>(this IEnumerable<T> source, string connectionString, string tableName) 
    { 
     using (SqlConnection conn = new SqlConnection(connectionString)) 
     { 
      var bulkCopy = new SqlBulkCopy(conn) {DestinationTableName = tableName}; 
      conn.Open(); 
      var item = source.ToDataTable(); 
      bulkCopy.WriteToServer(item); 
      conn.Close(); 
     } 
    } 
    public static DataTable ToDataTable<T>(this IEnumerable<T> source) 
    { 
     using (var dt = new DataTable()) 
     { 
      var toList = source.ToList(); 

      for (var index = 0; index < typeof(T).GetProperties().Length; index++) 
      { 
       var info = typeof(T).GetProperties()[index]; 
       dt.Columns.Add(new DataColumn(info.Name, info.PropertyType)); 
      } 

      for (var index = 0; index < toList.Count; index++) 
      { 
       var t = toList[index]; 
       var row = dt.NewRow(); 
       foreach (var info in typeof(T).GetProperties()) 
       { 
        row[info.Name] = info.GetValue(t, null); 
       } 
       dt.Rows.Add(row); 
      } 

      return dt; 
     } 
    } 

回答

0

右,所以我解決它

用法示例

_list = new List<Item>(); 
//Fill _list 
_list.SqlBulkInsert(SettingsFile.ConnectionString,"Table"); 

我不得不定義一個靜態列表,只處理包含列表

一個collegue建議中的那些屬性類型使用propertyType上的isclass屬性,但這有問題 當然 我會很高興的一些批評如果有任何其他方法或我可能錯過的東西

希望這可以幫助別人!

即這樣

 private static List<Type> Types 
    { 
     get 
     { 
      return new List<Type> 
        { 
         typeof (String), 
         typeof (int?), 
         typeof (Guid?), 
         typeof (double?), 
         typeof (decimal?), 
         typeof (float?), 
         typeof (Single?), 
         typeof (bool?), 
         typeof (DateTime?), 
         typeof (int), 
         typeof (Guid), 
         typeof (double), 
         typeof (decimal), 
         typeof (float), 
         typeof (Single), 
         typeof (bool), 
         typeof (DateTime), 
         typeof (DBNull) 
        }; 
     } 
    } 

public static DataTable ToDataTable<T>(this IEnumerable<T> source) 
    { 
     using (var dt = new DataTable()) 
     { 
      var toList = source.ToList(); 

      for (var index = 0; index < typeof(T).GetProperties().Count(); index++) 
      { 
       var info = typeof(T).GetProperties()[index]; 
       if (Types.Contains(info.PropertyType)) 
       { 
        dt.Columns.Add(new DataColumn(info.Name, info.PropertyType)); 
       } 
      } 

      for (var index = 0; index < toList.Count; index++) 
      { 
       var t = toList[index]; 
       var row = dt.NewRow(); 
       foreach (var info in typeof(T).GetProperties()) 
       { 
        if (Types.Contains(info.PropertyType)) 
        { 
         row[info.Name] = info.GetValue(t, null); 
        } 
       } 
       dt.Rows.Add(row); 
      } 

      return dt; 
     } 
    } 
+0

僅供參考,我發現類似'ToDataTable'extension方法,在那裏,使用IQueryable的,從而讓你選擇你想要什麼列包括。 – Ocelot20

+0

@ Ocelot20:小心分享一些鏈接? – RobIII