Ehsan Ghanbari

Experience, DotNet, Solutions

Introducing sqlbulktools

In my previews blog post I wrote about Z.bulkOperation. I migrated to sqlbulktools just because it's free with the same ability! (to be frank). Now to see the reason for my migration, fire up visual studio and Install the latest version of the library using the following command in console package manager:

 static void Main(string[] args)

        {



            var list = new List<User>();



            for (int i = 0; i < 100000; i++)

            {

                list.Add(new User

                {

                    Id = Guid.NewGuid(),

                    Name = "Ehsan",

                    Email = "[email protected]"

                });

               

            }



            Insert(list);



            Update(list);



            Delete(list);



        }



        public static void Insert(IList<User> users )

        {

            var bulkOperation = new BulkOperations();



            using (var trans = new TransactionScope())

            {

                using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))

                {

                    bulkOperation.Setup<User>()

                        .ForCollection(users)

                        .WithTable("Users")

                        .AddAllColumns()

                        .BulkInsert()

                       // .SetIdentityColumn(x => x.Id, ColumnDirection.Output)

                        .Commit(connection);

                }



                trans.Complete();

            }

        }



        public static void Update(IList<User> users)

        {

            var bulkOperation = new BulkOperations();



            using (var trans = new TransactionScope())

            {

                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))

                {

                    bulkOperation.Setup<User>()

                        .ForCollection(users)

                        .WithTable("Users")

                        .AddAllColumns()

                        .BulkUpdate()

                        .MatchTargetOn(x => x.Id)

                        .Commit(conn);

                }



                trans.Complete();

            }

        }



        public static void Delete(List<User> list)

        {

            var bulkOperation = new BulkOperations();



            using (var trans = new TransactionScope())

            {

                using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))

                {

                    bulkOperation.Setup<User>()

                        .ForCollection(list)

                        .WithTable("Users")

                        .AddColumn(x => x.Id)

                        .BulkDelete()

                        .MatchTargetOn(x => x.Id)

                        .Commit(conn);

                }



                trans.Complete();

            }

        }

Install-Package sqlbulktools

And get started!

Simply copy and paste the above code in your console application and run it. The result of CRUD operation with sqlbulktools  on 100000 of records in my machine would be:

insert   5,066ms

update   12,372ms

delete   2,0305ms

It could be different in every machine. But I think it's a good idea to use it in an enterprise application. Cheers!

About Me

Ehsan Ghanbari

Hi! my name is Ehsan. I'm a developer, passionate technologist, and fan of clean code. I'm interested in enterprise and large-scale applications architecture and design patterns and I'm spending a lot of my time on architecture subject. Since 2008, I've been as a developer for companies and organizations and I've been focusing on Microsoft ecosystem all the time. During the&nb Read More

Post Tags
Pending Blog Posts
Strategic design
Factory Pattern
time out pattern in ajax
Selectors in Jquery
using Log4net in asp.net MVC4
How to use PagedList In asp.net MVC
Redis as a cache server
Domain driven design VS model driven architecture
What's the DDD-lite?
Multiple submit buttons in asp.net MVC
comments powered by Disqus