Click here to Skip to main content
Click here to Skip to main content

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);
        conn.Open();
 
        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)
        {
            table.Rows.Add(properties.Select(
              property => GetPropertyValue(
              property.GetValue(entity, null))).ToArray());
        }
 
        bulkCopy.WriteToServer(table);
        conn.Close();
    }
 
    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;
    }
}

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
SuggestionAdding BatchSize due to timeout issue Pinmembermparkuk11-Nov-13 0:09 
GeneralMy vote of 5 Pinmembershawn meggoe6-Sep-13 1:18 
GeneralRe: My vote of 5 PinmemberMåns Tånneryd8-Sep-13 21:50 
GeneralMy vote of 5 Pinmembermasarius24-Jul-13 5:51 
QuestionQuestion re: column order in table Pinmemberjkshay5-Jun-13 9:19 
Thank you for this post. It will help me tremendously once I get past my current issue.
 
I'm currently creating my SQL Server table using Linq attribute mapping, which (as far as I can tell) doesn't allow me to determine the order of the columns in the created server table. The table created by your code for use with the SqlBulkInsert.WriteToServer method is not in the same generated column order, which yields a "Cannot insert the value NULL into column 'ProposedClass', table 'ProfitPOS.dbo.DefaultClassValues'; column does not allow nulls. INSERT fails." error.
 
Am I missing something with my attributes that will allow me to specify the order of the columns? Or is there something I need to do to specify the column order in the table created in your code?
 
Any assistance would be appreciated.
Jonathan Shay
http://jkshay.com

AnswerRe: Question re: column order in table Pinmembermasarius24-Jul-13 6:15 
Generalnice Pinmemberprabhakamaji11-Dec-11 18:50 
GeneralReason for my vote of 5 nice Pinmemberprabhakamaji11-Dec-11 18:50 
GeneralReason for my vote of 5 Reason for my vote of 5 Nice Tips PinmemberRaviKrSingh9-Dec-11 20:29 
GeneralReason for my vote of 5 Nice! PinmvpMika Wendelius9-Dec-11 10:03 
GeneralReason for my vote of 5 lol... that is pretty slick! PinmemberHoyaSaxa939-Dec-11 7:05 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140814.1 | Last Updated 25 Jan 2012
Article Copyright 2011 by Måns Tånneryd
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid