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 of my migration, fire up visual studio and Install the latest version of the library using the following command in console package manager:

Install-Package sqlbulktools

 

And get started!

 

  1.  static void Main(string[] args)
  2.         {
  3.  
  4.             var list = new List<User>();
  5.  
  6.             for (int i = 0; i < 100000; i++)
  7.             {
  8.                 list.Add(new User
  9.                 {
  10.                     Id = Guid.NewGuid(),
  11.                     Name = "Ehsan",
  12.                     Email = "mail@ehsanghanbari.com"
  13.                 });
  14.                
  15.             }
  16.  
  17.             Insert(list);
  18.  
  19.             Update(list);
  20.  
  21.             Delete(list);
  22.  
  23.         }
  24.  
  25.  
  26.         public static void Insert(IList<User> users )
  27.         {
  28.             var bulkOperation = new BulkOperations();
  29.  
  30.             using (var trans = new TransactionScope())
  31.             {
  32.                 using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))
  33.                 {
  34.                     bulkOperation.Setup<User>()
  35.                         .ForCollection(users)
  36.                         .WithTable("Users")
  37.                         .AddAllColumns()
  38.                         .BulkInsert()
  39.                        // .SetIdentityColumn(x => x.Id, ColumnDirection.Output)
  40.                         .Commit(connection);
  41.                 }
  42.  
  43.                 trans.Complete();
  44.             }
  45.         }
  46.  
  47.  
  48.         public static void Update(IList<User> users)
  49.         {
  50.             var bulkOperation = new BulkOperations();
  51.  
  52.             using (var trans = new TransactionScope())
  53.             {
  54.                 using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))
  55.                 {
  56.                     bulkOperation.Setup<User>()
  57.                         .ForCollection(users)
  58.                         .WithTable("Users")
  59.                         .AddAllColumns()
  60.                         .BulkUpdate()
  61.                         .MatchTargetOn(x => x.Id)
  62.                         .Commit(conn);
  63.                 }
  64.  
  65.                 trans.Complete();
  66.             }
  67.         }
  68.  
  69.         public static void Delete(List<User> list)
  70.         {
  71.             var bulkOperation = new BulkOperations();
  72.  
  73.             using (var trans = new TransactionScope())
  74.             {
  75.                 using (var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString))
  76.                 {
  77.                     bulkOperation.Setup<User>()
  78.                         .ForCollection(list)
  79.                         .WithTable("Users")
  80.                         .AddColumn(x => x.Id)
  81.                         .BulkDelete()
  82.                         .MatchTargetOn(x => x.Id)
  83.                         .Commit(conn);
  84.                 }
  85.  
  86.                 trans.Complete();
  87.             }
  88.         }

 

Simply copy and past 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!


Tags: C# , Libraries