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;
}
}
僅供參考,我發現類似'ToDataTable'extension方法,在那裏,使用IQueryable的,從而讓你選擇你想要什麼列包括。 – Ocelot20
@ Ocelot20:小心分享一些鏈接? – RobIII