Skip to content

Instantly share code, notes, and snippets.

@nikolay-pshenichny
Created October 2, 2015 00:16
Show Gist options
  • Save nikolay-pshenichny/0817bd181ad95bce64b3 to your computer and use it in GitHub Desktop.
Save nikolay-pshenichny/0817bd181ad95bce64b3 to your computer and use it in GitHub Desktop.
Bulk Insert with LINQ to SQL
using DataAccess.Extensions;
using System.Collections.Generic;
namespace DataAccess.Repositories
{
public class Repository : IRepository
{
private readonly System.Data.Linq.DataContext _dataContext;
public Repository(System.Data.Linq.DataContext dataContext)
{
_dataContext = dataContext;
}
public void BulkInsertEntities<TModelEntity>(IList<TModelEntity> modelEntities) where TModelEntity : class
{
var table = _dataContext.GetTable<TModelEntity>();
table.BulkInsert(modelEntities);
}
public void Dispose()
{
_dataContext.Dispose();
}
}
public interface IRepository
{
void BulkInsertEntities<TModelEntity>(IList<TModelEntity> modelEntities) where TModelEntity : class;
}
}
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
namespace DataAccess.Extensions
{
public static class TableExtensions
{
public static void BulkInsert<TModelEntity>(this System.Data.Linq.Table<TModelEntity> table, IList<TModelEntity> entities)
where TModelEntity : class
{
if ((entities == null) || (!entities.Any()))
{
return;
}
var modelEntityType = typeof(TModelEntity);
// Retrieve a TableAttribute that is assigned to the class generated by LINQ to SQL (MSLinqToSQLGenerator)
var tableAttribute =
modelEntityType.GetCustomAttributes(typeof(System.Data.Linq.Mapping.TableAttribute), false)
.Cast<System.Data.Linq.Mapping.TableAttribute>()
.Single();
// Retrieve all properties with ColumnAttribute
var columnProperties =
modelEntityType.GetProperties()
.Where(p => p.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute), false).Any());
// Create a DataTable and initialize columns
var dataTable = new DataTable(tableAttribute.Name);
foreach (var columnProperty in columnProperties)
{
var type = columnProperty.PropertyType;
if ((type.IsGenericType) && (type.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
var underlyingType = Nullable.GetUnderlyingType(columnProperty.PropertyType);
var column = new DataColumn(columnProperty.Name, underlyingType);
dataTable.Columns.Add(column);
}
else
{
dataTable.Columns.Add(new DataColumn(columnProperty.Name, type));
}
}
// Add data (rows) to the created DataTable
foreach (var modelEntity in entities)
{
var values = columnProperties.Select(p => p.GetValue(modelEntity, null)).ToArray();
dataTable.Rows.Add(values);
}
// Bulk insert data
using (SqlConnection sqlConnection = new SqlConnection(table.Context.Connection.ConnectionString))
{
sqlConnection.Open();
SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(sqlConnection, SqlBulkCopyOptions.TableLock, null)
{
DestinationTableName = tableAttribute.Name
};
sqlBulkCopy.WriteToServer(dataTable);
sqlConnection.Close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment