Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
0

SQL Bulk Copy

Posted by Nantharupan on 1:00 PM in , ,
Some times we faced situation when we need to insert single row for each millions of iterations. For example, we read from an Excel file which contains millions of students records and we want to add the details to the data base.  This can be done using the Entity Framework as follows,

                foreach (var student in students)
                {
                        using (var ctx = new dbContext())
                        {
                            ctx.Students.AddObject(student);
                            ctx.SaveChanges();
                        }              
                }

However this is inefficient. We can make use of SqlBulkCopy class to do the trick.

          string connString = "";
            // connect to SQL
            using (SqlConnection connection =  new SqlConnection(connString))
            {
                // make sure to enable triggers
                // more on triggers in next post
                SqlBulkCopy bulkCopy = new SqlBulkCopy(
                    connection,
                    SqlBulkCopyOptions.TableLock |
                    SqlBulkCopyOptions.FireTriggers |
                    SqlBulkCopyOptions.UseInternalTransaction,
                    null
                    );

                // set the destination table name
                bulkCopy.DestinationTableName = "tableName";
                connection.Open();

                // write the data in the "dataTable"
                bulkCopy.WriteToServer(dataTable);
                connection.Close();
            }
This would help to reduce the over head.

Visual Studio 2015 - Some useful Short Cuts

Posted by Nantharupan on 12:00 PM in , ,
I have started to working in Visual Studio again after a some time. I have noticed there are some new and useful short cuts, which would make developers life easier. So i listed down some of them. 


  • Alt+Up/Down - Move the code up or down
  • Clt+M+H - Clt+M+U to create a collapsible region
  • Clt+K+C - Clt+K+U to create a comment or un comment
  • Alt F12 to peek a definition from the code
  • Shift + F12 to find all references
  • Clt + Shift + F find in all files
  • Clt + G to go to a particular line number
  • F12 go to definition
  • Ctl \ + E to error list
  • Ctl + F4 close the window
  • Ctl + M + X expand all outline
  • Ctl+M + A Collapse all outline
  • ctrl + k, ctrl + d – Format Document

Popular posts

Copyright © 2009 On the way All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.