Click here to Skip to main content
13,595,986 members
Click here to Skip to main content
Add your own
alternative version


29 bookmarked
Posted 9 Dec 2011
Licenced CPOL

Using SQL bulk copy with your LINQ-to-SQL datacontext

, 25 Jan 2012
Rate this:
Please Sign up or sign in to vote.
How to use bulk insert with your LINQ-to-SQL datacontext

Linq-to-SQL is great for many things but it doesn't do very well with huge amounts of inserts. Extending the data context partial class with the following though will do the trick. I also wrote about this here. All you need to do is to create your LINQ entities just as you usually do and call the BulkInsertAll method on the datacontext. Also, do not use this datacontext for anything else before doing the bulk insert. The connection string of the datacontext that we use to create the SQLConnection here loses its password after its first use for some reason and that makes it, obviously, a bit difficult to connect.

partial class MyDataContext
    partial void OnCreated()
        CommandTimeout = 5 * 60;

    public void BulkInsertAll<T>(IEnumerable<T> entities)
        entities = entities.ToArray();

        string cs = Connection.ConnectionString;
        var conn = new SqlConnection(cs);

        Type t = typeof(T);

        var tableAttribute = (TableAttribute)t.GetCustomAttributes(
            typeof(TableAttribute), false).Single();
        var bulkCopy = new SqlBulkCopy(conn) { 
            DestinationTableName = tableAttribute.Name };

        var properties = t.GetProperties().Where(EventTypeFilter).ToArray();
        var table = new DataTable();
        foreach (var property in properties)
            Type propertyType = property.PropertyType;
            if (propertyType.IsGenericType &&
                propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                propertyType = Nullable.GetUnderlyingType(propertyType);

            table.Columns.Add(new DataColumn(property.Name, propertyType));

        foreach (var entity in entities)
              property => GetPropertyValue(
              property.GetValue(entity, null))).ToArray());


    private bool EventTypeFilter(System.Reflection.PropertyInfo p)
        var attribute = Attribute.GetCustomAttribute(p, 
            typeof (AssociationAttribute)) as AssociationAttribute;

        if (attribute == null) return true;
        if (attribute.IsForeignKey == false) return true; 
        return false;

    private object GetPropertyValue(object o)
        if (o == null)
            return DBNull.Value;
        return o;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Måns Tånneryd
Software Developer (Senior) Tånneryd IT AB
Sweden Sweden
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralReason for my vote of 5 nice Pin
prabhakamaji11-Dec-11 18:50
memberprabhakamaji11-Dec-11 18:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180621.3 | Last Updated 25 Jan 2012
Article Copyright 2011 by Måns Tånneryd
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid